1 package polyglot.util; 2 3 import java.io.Serializable ; 4 import java.io.ObjectInputStream ; 5 import java.io.ObjectOutputStream ; 6 import java.io.IOException ; 7 import java.util.*; 8 9 10 public class Enum implements Serializable 11 { 12 13 private String name; 14 15 16 private static Map cache = new HashMap(); 17 18 protected Enum(String name) { 19 this.name = name; 20 21 if (intern() != this) { 23 throw new InternalCompilerError("Duplicate enum \"" + name + "\""); 24 } 25 } 26 27 28 private Enum() { } 29 30 public int hashCode() { 31 return name.hashCode(); 32 } 33 34 public boolean equals(Object o) { 35 return this == o; 36 } 37 38 public String toString() { 39 return name; 40 } 41 42 private static class EnumKey { 43 Enum e; 44 45 EnumKey(Enum e) { 46 this.e = e; 47 } 48 49 public boolean equals(Object o) { 50 return o instanceof EnumKey 51 && e.name.equals(((EnumKey) o).e.name) 52 && e.getClass() == ((EnumKey) o).e.getClass(); 53 } 54 55 public int hashCode() { 56 return e.getClass().hashCode() ^ e.name.hashCode(); 57 } 58 59 public String toString() { 60 return e.toString(); 61 } 62 } 63 64 public Enum intern() { 65 EnumKey k = new EnumKey(this); 66 67 Enum e = (Enum ) cache.get(k); 68 69 if (e == null) { 70 cache.put(k, this); 71 return this; 72 } 73 74 return e; 75 } 76 } 77 | Popular Tags |