1 16 17 package org.apache.axis.constants; 18 19 import org.apache.axis.components.logger.LogFactory; 20 import org.apache.axis.utils.Messages; 21 import org.apache.commons.logging.Log; 22 23 import java.util.Hashtable ; 24 25 26 32 public abstract class Enum implements java.io.Serializable { 33 private static final Hashtable types = new Hashtable (13); 34 35 protected static Log log = 36 LogFactory.getLog(Enum .class.getName()); 37 38 private final Type type; 39 public final int value; 40 public final String name; 41 42 protected Enum(Type type, int value, String name) { 43 this.type = type; 44 this.value = value; 45 this.name = name.intern(); 46 } 47 48 public final int getValue() { return value; } 49 public final String getName() { return name; } 50 public final Type getType() { return type; } 51 52 public String toString() { 53 return name; 54 } 55 56 public final boolean equals(Object obj) { 57 return (obj != null && obj instanceof Enum ) 58 ? _equals((Enum )obj) 59 : false; 60 } 61 62 public int hashCode() { 63 return value; 64 } 65 66 public final boolean equals(Enum obj) { 67 return (obj != null) ? _equals(obj) : false; 68 } 69 70 76 private final boolean _equals(Enum obj) { 77 return ( obj.type == type && 79 obj.value == value); 80 } 81 82 public abstract static class Type implements java.io.Serializable { 83 private final String name; 84 private final Enum [] enums; 85 private Enum dephault = null; 86 87 protected Type(String name, Enum [] enums) { 88 this.name = name.intern(); 89 this.enums = enums; 90 synchronized (types) { 91 types.put(name, this); 92 } 93 } 94 95 public void setDefault(Enum dephault) { 96 this.dephault = dephault; 97 } 98 99 public Enum getDefault() { 100 return dephault; 101 } 102 103 public final String getName() { 104 return name; 105 } 106 107 public final boolean isValid(String enumName) { 108 for (int enumElt = 0; enumElt < enums.length; enumElt++) { 109 if (enums[enumElt].getName().equalsIgnoreCase(enumName)) 110 return true; 111 } 112 113 return false; 114 } 115 116 public final int size() { 117 return enums.length; 118 } 119 120 123 public final String [] getEnumNames() { 124 String [] nms = new String [ size() ]; 125 126 for (int idx = 0; idx < enums.length; idx++) 127 nms[idx] = enums[idx].getName(); 128 129 return nms; 130 } 131 132 135 public final Enum getEnum(int enumElt) { 136 return (enumElt >= 0 && enumElt < enums.length) ? enums[enumElt] : null; 137 } 138 139 142 public final Enum getEnum(String enumName) { 143 Enum e = getEnum(enumName, null); 144 145 if (e == null) { 146 log.error(Messages.getMessage("badEnum02", name, enumName)); 147 } 148 149 return e; 150 } 151 152 159 public final Enum getEnum(String enumName, Enum dephault) { 160 if (enumName != null && enumName.length() > 0) { 161 for (int enumElt = 0; enumElt < enums.length; enumElt++) { 162 Enum e = enums[enumElt]; 163 if (e.getName().equalsIgnoreCase(enumName)) 164 return e; 165 } 166 } 167 168 return dephault; 169 } 170 171 private Object readResolve() throws java.io.ObjectStreamException { 172 Object type = types.get(name); 173 if (type == null) { 174 type = this; 175 types.put(name, type); 176 } 177 return type; 178 } 179 } 180 } 181 | Popular Tags |