Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 28th, 2012  |  syntax: None  |  size: 1.94 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How to use enum with values in Java
  2. public static enum DataValueModifier {
  3.     EQUAL("="),
  4.     GREATER_THAN(">"),
  5.     GREATER_EUQAL(">="),
  6.     LESS_THAN("<"),
  7.     LESS_EQUAL("<="),
  8.     APPRROXIMATE("~"),
  9.     NOT_DETERMINED("ND");
  10.     private String value;
  11.     private DataValueModifier(String value) {
  12.         this.value = value;
  13.     }
  14.     public String getValue() {
  15.         return value;
  16.     }
  17. }
  18.        
  19. if (dataValue.contains(DataValueModifier.EQUAL.getValue())) {
  20. ...
  21. }
  22.        
  23. public static enum DataValueModifier {
  24.  
  25.     ...
  26.  
  27.     public boolean isModifier( String modifierString )
  28.     {
  29.        return modifierString != null && value.equals(modifierString);
  30.     }
  31. }
  32.        
  33. if (DataValueModifier.EQUAL.isModifier(dataValue))
  34. {
  35.   //...
  36. }
  37.        
  38. if ( dataValueEnum == DataValueModifier.EQUAL ) {
  39. {
  40.   //...
  41. }
  42.        
  43. public static DataValueModifier toDataValueModifier( String dataValue ) {
  44.     if( EQUAL.isModifier( dataValue ) {
  45.        return EQUAL;
  46.     } else if( GREATER_THAN.isModifier( dataValue ) {
  47.        return GREATER_THAN;
  48.     } else if...
  49.        // Do this for all possible values
  50.     } else {
  51.        return UNKNOWN;
  52.        // Also, add an UNKNOWN to your list of enum values.
  53.     }
  54. }
  55.        
  56. DataValueModifier dataValueEnum = DataValueModifier.toDataValueModifier(dataValue);
  57. if (dataValueEnum == DataValueModifier.EQUAL) {
  58.    ...
  59. }
  60.        
  61. switch( DataValueModifier.toDataValueModifier(dataValue) ) {
  62.     case EQUAL:
  63.         // ...
  64.         break;
  65.     case GREATER_THAN:
  66.         // ...
  67.         break;
  68.     case GREATER_EQUAL:
  69.         // ...
  70.         break;
  71.     // ... define all the cases you want
  72.     case UNKNOWN:
  73.     default:
  74.          // ...
  75. }
  76.        
  77. if (EQUAL.containedIn(dataValue)) {
  78. ...
  79. }
  80.        
  81. package mypackage;
  82.  
  83. public enum DataValueModifier
  84. {
  85.   //your enum code
  86. }
  87.        
  88. import static mypackage.DataValueModifier.*;
  89.  
  90. public MyClass
  91. {
  92.  
  93.   // code blah blah blah
  94.  
  95.   public void doIt()
  96.   {
  97.     // more code blah blah
  98.     if (dataValue.contains(EQUAL.getValue()))
  99.     {
  100.       //...
  101.     }
  102.   }
  103. }