1 55 56 package org.jboss.axis.enums; 57 58 import org.jboss.axis.utils.Messages; 59 import org.jboss.logging.Logger; 60 61 import java.util.Hashtable ; 62 63 64 70 public abstract class Enum 71 { 72 private static final Hashtable types = new Hashtable (13); 73 74 private static Logger log = Logger.getLogger(Enum .class.getName()); 75 76 private final Type type; 77 public final int value; 78 public final String name; 79 80 protected Enum(Type type, int value, String name) 81 { 82 this.type = type; 83 this.value = value; 84 this.name = name.intern(); 85 } 86 87 public final int getValue() 88 { 89 return value; 90 } 91 92 public final String getName() 93 { 94 return name; 95 } 96 97 public final Type getType() 98 { 99 return type; 100 } 101 102 public String toString() 103 { 104 return name; 105 } 106 107 public final boolean equals(Object obj) 108 { 109 return (obj != null && obj instanceof Enum ) 110 ? _equals((Enum )obj) 111 : false; 112 } 113 114 public final boolean equals(Enum obj) 115 { 116 return (obj != null) ? _equals(obj) : false; 117 } 118 119 125 private final boolean _equals(Enum obj) 126 { 127 return ( obj.type == type && 129 obj.value == value); 130 } 131 132 public abstract static class Type 133 { 134 private final String name; 135 private final Enum [] enumArr; 136 private Enum dephault = null; 137 138 protected Type(String name, Enum [] enums) 139 { 140 this.name = name.intern(); 141 this.enumArr = enums; 142 synchronized (types) 143 { 144 types.put(name, this); 145 } 146 } 147 148 protected void setDefault(Enum dephault) 149 { 150 this.dephault = dephault; 151 } 152 153 public Enum getDefault() 154 { 155 return dephault; 156 } 157 158 public final String getName() 159 { 160 return name; 161 } 162 163 public final boolean isValid(String enumName) 164 { 165 for (int en = 0; en < enumArr.length; en++) 166 { 167 if (enumArr[en].getName().equalsIgnoreCase(enumName)) 168 return true; 169 } 170 171 return false; 172 } 173 174 public final int size() 175 { 176 return enumArr.length; 177 } 178 179 182 public final String [] getEnumNames() 183 { 184 String [] nms = new String [size()]; 185 186 for (int idx = 0; idx < enumArr.length; idx++) 187 nms[idx] = enumArr[idx].getName(); 188 189 return nms; 190 } 191 192 195 public final Enum getEnum(int en) 196 { 197 return (en >= 0 && en < enumArr.length) ? enumArr[en] : null; 198 } 199 200 203 public final Enum getEnum(String enumName) 204 { 205 Enum e = getEnum(enumName, null); 206 207 if (e == null) 208 { 209 log.error(Messages.getMessage("badEnum02", name, enumName)); 210 } 211 212 return e; 213 } 214 215 222 public final Enum getEnum(String enumName, Enum dephault) 223 { 224 if (enumName != null && enumName.length() > 0) 225 { 226 for (int en = 0; en < enumArr.length; en++) 227 { 228 Enum e = enumArr[en]; 229 if (e.getName().equalsIgnoreCase(enumName)) 230 return e; 231 } 232 } 233 234 return dephault; 235 } 236 } 237 } 238 | Popular Tags |