1 16 17 package org.springframework.core; 18 19 import java.lang.reflect.Field ; 20 import java.util.HashMap ; 21 import java.util.HashSet ; 22 import java.util.Iterator ; 23 import java.util.Map ; 24 import java.util.Set ; 25 26 import org.springframework.util.Assert; 27 import org.springframework.util.ReflectionUtils; 28 import org.springframework.util.StringUtils; 29 30 47 public class Constants { 48 49 private final String className; 50 51 52 private final Map fieldCache = new HashMap (); 53 54 55 62 public Constants(Class clazz) { 63 Assert.notNull(clazz); 64 this.className = clazz.getName(); 65 Field [] fields = clazz.getFields(); 66 for (int i = 0; i < fields.length; i++) { 67 Field field = fields[i]; 68 if (ReflectionUtils.isPublicStaticFinal(field)) { 69 String name = field.getName(); 70 try { 71 Object value = field.get(null); 72 this.fieldCache.put(name, value); 73 } 74 catch (IllegalAccessException ex) { 75 } 77 } 78 } 79 } 80 81 84 public final String getClassName() { 85 return className; 86 } 87 88 91 public final int getSize() { 92 return this.fieldCache.size(); 93 } 94 95 99 protected final Map getFieldCache() { 100 return fieldCache; 101 } 102 103 104 112 public Number asNumber(String code) throws ConstantException { 113 Object obj = asObject(code); 114 if (!(obj instanceof Number )) { 115 throw new ConstantException(this.className, code, "not a Number"); 116 } 117 return (Number ) obj; 118 } 119 120 128 public String asString(String code) throws ConstantException { 129 return asObject(code).toString(); 130 } 131 132 138 public Object asObject(String code) throws ConstantException { 139 String codeToUse = code.toUpperCase(); 140 Object val = this.fieldCache.get(codeToUse); 141 if (val == null) { 142 throw new ConstantException(this.className, codeToUse, "not found"); 143 } 144 return val; 145 } 146 147 159 public Set getValues(String namePrefix) { 160 String prefixToUse = StringUtils.hasText(namePrefix) 161 ? namePrefix.trim().toUpperCase() : ""; 162 Set values = new HashSet (); 163 for (Iterator it = this.fieldCache.keySet().iterator(); it.hasNext();) { 164 String code = (String ) it.next(); 165 if (code.startsWith(prefixToUse)) { 166 values.add(this.fieldCache.get(code)); 167 } 168 } 169 return values; 170 } 171 172 179 public Set getValuesForProperty(String propertyName) { 180 return getValues(propertyToConstantNamePrefix(propertyName)); 181 } 182 183 191 public String toCode(Object value, String namePrefix) throws ConstantException { 192 String prefixToUse = namePrefix.toUpperCase(); 193 for (Iterator it = this.fieldCache.entrySet().iterator(); it.hasNext();) { 194 Map.Entry entry = (Map.Entry ) it.next(); 195 String key = (String ) entry.getKey(); 196 if (key.startsWith(prefixToUse) && entry.getValue().equals(value)) { 197 return key; 198 } 199 } 200 throw new ConstantException(this.className, prefixToUse, value); 201 } 202 203 212 public String toCodeForProperty(Object value, String propertyName) throws ConstantException { 213 return toCode(value, propertyToConstantNamePrefix(propertyName)); 214 } 215 216 229 public String propertyToConstantNamePrefix(String propertyName) { 230 StringBuffer parsedPrefix = new StringBuffer (); 231 for(int i = 0; i < propertyName.length(); i++) { 232 char c = propertyName.charAt(i); 233 if (Character.isUpperCase(c)) { 234 parsedPrefix.append("_"); 235 parsedPrefix.append(c); 236 } 237 else { 238 parsedPrefix.append(Character.toUpperCase(c)); 239 } 240 } 241 return parsedPrefix.toString(); 242 } 243 244 } 245 | Popular Tags |