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

Untitled

By: a guest on Aug 7th, 2012  |  syntax: None  |  size: 0.59 KB  |  hits: 8  |  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. The right and wrong approach to writing Java Enums
  2. public interface Base<T> {
  3.  
  4.     public T fromValue(String v);
  5.  
  6. }
  7.        
  8. public enum AddressType implements Base<AddressType> {
  9.  
  10.     NotSpecified("Not Specified."),
  11.  
  12.     Physical("Physical"),
  13.  
  14.     Postal("Postal");
  15.  
  16.     private final String label;
  17.  
  18.     private AddressType(String label) {
  19.         this.label = label;
  20.     }
  21.  
  22.     public String getLabel() {
  23.         return this.label;
  24.     }
  25.  
  26.     @Override
  27.     public AddressType fromValue(String v) {
  28.         return valueOf(v);
  29.     }
  30. }
  31.        
  32. AddressType type = AddressType.Postal.valueFrom("Physical");