1 8 package transport.packet; 9 10 import java.io.Serializable ; 11 import java.util.HashMap ; 12 import java.util.Map ; 13 14 17 public class Enum implements Serializable 18 { 19 private static final Map byName = new HashMap (); 20 private static final Map byValue = new HashMap (); 21 22 private String name; 23 private Integer value; 24 25 public Enum(String n, int v) 26 { 27 super(); 28 name = n; 29 value = new Integer (v); 30 31 if (byName.containsKey(name)) 32 { 33 throw new DuplicateEnumException(name); 34 } 35 if (byValue.containsKey(value)) 36 { 37 throw new DuplicateEnumException(value.toString()); 38 } 39 byName.put(name,this); 40 byValue.put(value,this); 41 } 42 43 public String getName() 44 { 45 return name; 46 } 47 48 public int getValue() 49 { 50 return value.intValue(); 51 } 52 53 public static Enum getByValue(int i) 54 { 55 return (Enum ) byValue.get(new Integer (i)); 56 } 57 58 public static Enum getByName(String n) 59 { 60 return (Enum ) byName.get(n); 61 } 62 63 public boolean equals(Object o) 64 { 65 if (! (o instanceof Enum )) 66 { 67 return false; 68 } 69 Enum e = (Enum ) o; 70 return e.name.equals(name) && e.value.equals(value); 71 } 72 73 public int hashCode() 74 { 75 return (name + value.toString()).hashCode(); 76 } 77 } 78 | Popular Tags |