Use cases of Java enumeration

  sonic0002        2013-01-05 09:15:30       3,615        0    

JDK 1.5 introduces a new type -- enumeration. In Java, it's just a small feature, but it can bring us much convenience.Here we summarize some use cases of Java enumeration.

1. Constant

Prior to JDK 1.5, we can define constant as public static final..., now we can use enumeration to group all constants in one enum variable and it also provides some useful functions.

  1. public enum Color {  
  2.   RED, GREEN, BLANK, YELLOW  

2.In Switch

Prior to JDK 1.6, only int,char and enum are supported in switch statement, with enum, we can write more readable codes.

  1. enum Signal {  
  2.     GREEN, YELLOW, RED  
  3. }  
  4. public class TrafficLight {  
  5.     Signal color = Signal.RED;  
  6.     public void change() {  
  7.         switch (color) {  
  8.         case RED:  
  9.             color = Signal.GREEN;  
  10.             break;  
  11.         case YELLOW:  
  12.             color = Signal.RED;  
  13.             break;  
  14.         case GREEN:  
  15.             color = Signal.YELLOW;  
  16.             break;  
  17.         }  
  18.     }  

3. Add new methods in enum

If we want to define our own methods, then we have to add a semicolon after the implementation of enum variable.

  1. public enum Color {  
  2.     RED("RED"1), GREEN("GREEN"2), BLANK("WHITE"3), YELLO("YELLOW"4);  
  3.     // Member property
  4.     private String name;  
  5.     private int index;  
  6.     // Constructor
  7.     private Color(String name, int index) {  
  8.         this.name = name;  
  9.         this.index = index;  
  10.     }  
  11.     // Method
  12.     public static String getName(int index) {  
  13.         for (Color c : Color.values()) {  
  14.             if (c.getIndex() == index) {  
  15.                 return c.name;  
  16.             }  
  17.         }  
  18.         return null;  
  19.     }  
  20.     // get set methods
  21.     public String getName() {  
  22.         return name;  
  23.     }  
  24.     public void setName(String name) {  
  25.         this.name = name;  
  26.     }  
  27.     public int getIndex() {  
  28.         return index;  
  29.     }  
  30.     public void setIndex(int index) {  
  31.         this.index = index;  
  32.     }  

4. Override methods in enum

Here is an example of overriding toString() method.

  1. public enum Color {  
  2.     RED("RED"1), GREEN("GREEN"2), BLANK("WHITE"3), YELLO("YELLOW"4);  
  3.     // Member property
  4.     private String name;  
  5.     private int index;  
  6.     // Constructor
  7.     private Color(String name, int index) {  
  8.         this.name = name;  
  9.         this.index = index;  
  10.     }  
  11.     //Override method 
  12.     @Override  
  13.     public String toString() {  
  14.         return this.index+"_"+this.name;  
  15.     }  

5. Implement interface

All enum objects are inherited from java.lang.Enum class. Since Java doesn't support multiple inheritance, so enum cannot inherit other classes.

  1. public interface Behaviour {  
  2.     void print();  
  3.     String getInfo();  
  4. }  
  5. public enum Color implements Behaviour{  
  6.     RED("RED"1), GREEN("GREEN"2), BLANK("WHITE"3), YELLO("YELLOW"4);  
  7.     // Member property
  8.     private String name;  
  9.     private int index;  
  10.     // Constructor
  11.     private Color(String name, int index) {  
  12.         this.name = name;  
  13.         this.index = index;  
  14.     }  
  15.     //Interface method
  16.     @Override  
  17.     public String getInfo() {  
  18.         return this.name;  
  19.     }  
  20.     //Interface method
  21.     @Override  
  22.     public void print() {  
  23.         System.out.println(this.index+":"+this.name);  
  24.     }  

6. Use interface to organize enumerations

  1. public interface Food {  
  2.     enum Coffee implements Food{  
  3.         BLACK_COFFEE,DECAF_COFFEE,LATTE,CAPPUCCINO  
  4.     }  
  5.     enum Dessert implements Food{  
  6.         FRUIT, CAKE, GELATO  
  7.     }  

7. About use of enum collection

java.util.EnumSet and java.util.EnumMap are two enumeration collections. EnumSet will guarantee the element in the set is unique. key in EnumMap are enum type, and value can be any data type.

Source : http://softbeta.iteye.com/blog/1185573

 

JAVA  ENUMERATION  ENUM 

       

  RELATED


  0 COMMENT


No comment for this article.



  RANDOM FUN

Delegation?