This class can be used to parse other classes containing constant definitions
in public static final members. The
methods of this class
allow these constant values to be accessed via their string names.
This class is ideal for use in PropertyEditors, enabling them to
recognize the same names as the constants themselves, and freeing them
from maintaining their own mapping.
| Method from org.springframework.core.Constants Detail: |
public Number asNumber(String code) throws ConstantException {
Object obj = asObject(code);
if (!(obj instanceof Number)) {
throw new ConstantException(this.className, code, "not a Number");
}
return (Number) obj;
}
Return a constant value cast to a Number. |
public Object asObject(String code) throws ConstantException {
Assert.notNull(code, "Code must not be null");
String codeToUse = code.toUpperCase(Locale.ENGLISH);
Object val = this.fieldCache.get(codeToUse);
if (val == null) {
throw new ConstantException(this.className, codeToUse, "not found");
}
return val;
}
Parse the given String (upper or lower case accepted) and return
the appropriate value if it's the name of a constant field in the
class that we're analysing. |
public String asString(String code) throws ConstantException {
return asObject(code).toString();
}
Return a constant value as a String. |
public final String getClassName() {
return this.className;
}
Return the name of the analyzed class. |
protected final Map getFieldCache() {
return this.fieldCache;
}
Exposes the field cache to subclasses:
a Map from String field name to object value. |
public Set getNames(String namePrefix) {
String prefixToUse = (namePrefix != null ? namePrefix.trim().toUpperCase(Locale.ENGLISH) : "");
Set names = new HashSet();
for (Iterator it = this.fieldCache.keySet().iterator(); it.hasNext();) {
String code = (String) it.next();
if (code.startsWith(prefixToUse)) {
names.add(code);
}
}
return names;
}
Return all names of the given group of constants.
Note that this method assumes that constants are named
in accordance with the standard Java convention for constant
values (i.e. all uppercase). The supplied namePrefix
will be uppercased (in a locale-insensitive fashion) prior to
the main logic of this method kicking in. |
public Set getNamesForProperty(String propertyName) {
return getNames(propertyToConstantNamePrefix(propertyName));
}
Return all names of the group of constants for the
given bean property name. |
public Set getNamesForSuffix(String nameSuffix) {
String suffixToUse = (nameSuffix != null ? nameSuffix.trim().toUpperCase(Locale.ENGLISH) : "");
Set names = new HashSet();
for (Iterator it = this.fieldCache.keySet().iterator(); it.hasNext();) {
String code = (String) it.next();
if (code.endsWith(suffixToUse)) {
names.add(code);
}
}
return names;
}
Return all names of the given group of constants.
Note that this method assumes that constants are named
in accordance with the standard Java convention for constant
values (i.e. all uppercase). The supplied nameSuffix
will be uppercased (in a locale-insensitive fashion) prior to
the main logic of this method kicking in. |
public final int getSize() {
return this.fieldCache.size();
}
Return the number of constants exposed. |
public Set getValues(String namePrefix) {
String prefixToUse = (namePrefix != null ? namePrefix.trim().toUpperCase(Locale.ENGLISH) : "");
Set values = new HashSet();
for (Iterator it = this.fieldCache.keySet().iterator(); it.hasNext();) {
String code = (String) it.next();
if (code.startsWith(prefixToUse)) {
values.add(this.fieldCache.get(code));
}
}
return values;
}
Return all values of the given group of constants.
Note that this method assumes that constants are named
in accordance with the standard Java convention for constant
values (i.e. all uppercase). The supplied namePrefix
will be uppercased (in a locale-insensitive fashion) prior to
the main logic of this method kicking in. |
public Set getValuesForProperty(String propertyName) {
return getValues(propertyToConstantNamePrefix(propertyName));
}
Return all values of the group of constants for the
given bean property name. |
public Set getValuesForSuffix(String nameSuffix) {
String suffixToUse = (nameSuffix != null ? nameSuffix.trim().toUpperCase(Locale.ENGLISH) : "");
Set values = new HashSet();
for (Iterator it = this.fieldCache.keySet().iterator(); it.hasNext();) {
String code = (String) it.next();
if (code.endsWith(suffixToUse)) {
values.add(this.fieldCache.get(code));
}
}
return values;
}
Return all values of the given group of constants.
Note that this method assumes that constants are named
in accordance with the standard Java convention for constant
values (i.e. all uppercase). The supplied nameSuffix
will be uppercased (in a locale-insensitive fashion) prior to
the main logic of this method kicking in. |
public String propertyToConstantNamePrefix(String propertyName) {
StringBuffer parsedPrefix = new StringBuffer();
for(int i = 0; i < propertyName.length(); i++) {
char c = propertyName.charAt(i);
if (Character.isUpperCase(c)) {
parsedPrefix.append("_");
parsedPrefix.append(c);
}
else {
parsedPrefix.append(Character.toUpperCase(c));
}
}
return parsedPrefix.toString();
}
Convert the given bean property name to a constant name prefix.
Uses a common naming idiom: turning all lower case characters to
upper case, and prepending upper case characters with an underscore.
Example: "imageSize" -> "IMAGE_SIZE"
Example: "imagesize" -> "IMAGESIZE".
Example: "ImageSize" -> "_IMAGE_SIZE".
Example: "IMAGESIZE" -> "_I_M_A_G_E_S_I_Z_E" |
public String toCode(Object value,
String namePrefix) throws ConstantException {
String prefixToUse = (namePrefix != null ? namePrefix.trim().toUpperCase(Locale.ENGLISH) : null);
for (Iterator it = this.fieldCache.entrySet().iterator(); it.hasNext();) {
Map.Entry entry = (Map.Entry) it.next();
String key = (String) entry.getKey();
if (key.startsWith(prefixToUse) && entry.getValue().equals(value)) {
return key;
}
}
throw new ConstantException(this.className, prefixToUse, value);
}
|
public String toCodeForProperty(Object value,
String propertyName) throws ConstantException {
return toCode(value, propertyToConstantNamePrefix(propertyName));
}
Look up the given value within the group of constants for
the given bean property name. Will return the first match. |
public String toCodeForSuffix(Object value,
String nameSuffix) throws ConstantException {
String suffixToUse = (nameSuffix != null ? nameSuffix.trim().toUpperCase(Locale.ENGLISH) : null);
for (Iterator it = this.fieldCache.entrySet().iterator(); it.hasNext();) {
Map.Entry entry = (Map.Entry) it.next();
String key = (String) entry.getKey();
if (key.endsWith(suffixToUse) && entry.getValue().equals(value)) {
return key;
}
}
throw new ConstantException(this.className, suffixToUse, value);
}
|