📄 canonicalutilities.java
字号:
/* @LICENSE_COPYRIGHT@ */package net.sf.irunninglog.canonical;import java.util.ArrayList;import java.util.List;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import net.sf.irunninglog.util.ResourceManager;/** * Utility class used to lookup values related to canonical objects' fields. * The type of values that can be retrieved include: field labels, max/min * lengths. max/min values, and valid values. The methods included in this * class use the canonical ID and field name to lookup the desired values from * the application's property files. * * @author <a href="mailto:allan_e_lewis@yahoo.com">Allan Lewis</a> * @version $Revision: 1.2 $ $Date: 2005/06/29 02:48:09 $ * @since iRunningLog 1.0 */public final class CanonicalUtilities { /** <code>Log</code> instance for this class. */ private static final Log LOG = LogFactory.getLog(CanonicalUtilities.class); /** Delimiter used in building lookup keys. */ private static final String DELIMITER = "."; /** Suffix used to build keys for label lookups. */ private static final String LABEL = "label"; /** Suffix used to build keys for min length lookups. */ private static final String MIN_LENGTH = "minLength"; /** Suffix used to build keys for max length lookups. */ private static final String MAX_LENGTH = "maxLength"; /** Suffix used to build keys for min value lookups. */ private static final String MIN_VALUE = "minValue"; /** Suffix used to build keys for max value lookups. */ private static final String MAX_VALUE = "maxValue"; /** Suffix used to build keys for number of valid value lookups. */ private static final String NUM_VALID_VALUES = "numValidValues"; /** Suffix used to build keys for valid value lookups. */ private static final String VALID_VALUE = "validValue"; /** The <code>ResourceManager</code> used to look up values. */ private static final ResourceManager MANAGER; static { MANAGER = ResourceManager.getInstance(); } /** This is a utility class - does not expose a constructor. */ private CanonicalUtilities() { } /** * Get the label value for a canonical object's field. * * @param canonicalId The canonical ID of the object containing the field * @param fieldName The name of the field whose label we want * @return The label value for the field */ public static String getLabel(String canonicalId, String fieldName) { String key = getResourceKey(canonicalId, fieldName, LABEL); String label = getString(key); if (LOG.isDebugEnabled()) { LOG.debug("getLabel: Label for canonicalId=" + canonicalId + ", fieldName=" + fieldName + " is '" + label + "'"); } return label; } /** * Get the minimum length value for a canonical object's field. * * @param canonicalId The canonical ID of the object containing the field * @param fieldName The name of the field whose minimum length we want * @return The minimum length value for the field */ public static int getMinLength(String canonicalId, String fieldName) { String key = getResourceKey(canonicalId, fieldName, MIN_LENGTH); int minLength = Integer.parseInt(getString(key)); if (LOG.isDebugEnabled()) { LOG.debug("getMinLength: Min length for canonicalId=" + canonicalId + ", fieldName=" + fieldName + " is '" + minLength + "'"); } return minLength; } /** * Get the maximum length value for a canonical object's field. * * @param canonicalId The canonical ID of the object containing the field * @param fieldName The name of the field whose maximum length we want * @return The maximum length value for the field */ public static int getMaxLength(String canonicalId, String fieldName) { String key = getResourceKey(canonicalId, fieldName, MAX_LENGTH); int maxLength = Integer.parseInt(getString(key)); if (LOG.isDebugEnabled()) { LOG.debug("getMaxLength: Max length for canonicalId=" + canonicalId + ", fieldName=" + fieldName + " is '" + maxLength + "'"); } return maxLength; } /** * Get the minimum value for a canonical object's field. * * @param canonicalId The canonical ID of the object containing the field * @param fieldName The name of the field whose minimum value we want * @return The minimum value for the field */ public static double getMinValue(String canonicalId, String fieldName) { String key = getResourceKey(canonicalId, fieldName, MIN_VALUE); double minValue = Double.parseDouble(getString(key)); if (LOG.isDebugEnabled()) { LOG.debug("getMinValue: Min value for canonicalId=" + canonicalId + ", fieldName=" + fieldName + " is '" + minValue + "'"); } return minValue; } /** * Get the maximum value for a canonical object's field. * * @param canonicalId The canonical ID of the object containing the field * @param fieldName The name of the field whose maximum value we want * @return The maximum value for the field */ public static double getMaxValue(String canonicalId, String fieldName) { String key = getResourceKey(canonicalId, fieldName, MAX_VALUE); double maxValue = Double.parseDouble(getString(key)); if (LOG.isDebugEnabled()) { LOG.debug("getMaxValue: Max value for canonicalId=" + canonicalId + ", fieldName=" + fieldName + " is '" + maxValue + "'"); } return maxValue; } /** * Get the list of valid (allowed) values for a canonical object's field. * * @param canonicalId The canonical ID of the object containing the field * @param fieldName The name of the field whose valid values we want * @return A list of the valid values for the field */ public static List getValidValues(String canonicalId, String fieldName) { String key = getResourceKey(canonicalId, fieldName, NUM_VALID_VALUES); int numValidVals = Integer.parseInt(getString(key)); if (LOG.isDebugEnabled()) { LOG.debug("getValidValues: Number of valid values for canonicalId=" + canonicalId + ", fieldName=" + fieldName + " is " + numValidVals); } List validValues = new ArrayList(); String value = null; for (int i = 0; i < numValidVals; i++) { key = getResourceKey(canonicalId, fieldName, VALID_VALUE + i); value = getString(key); validValues.add(value); if (LOG.isDebugEnabled()) { LOG.debug("getValidValues: valid value " + i + " for " + "canonicalId=" + canonicalId + ", fieldName=" + fieldName + " is '" + value + "'"); } } return validValues; } /** * Construct and return the key that will be used to lookup a property * for a canonical object's field. * * @param canonicalId The canonical ID of the object containing the field * @param fieldName The name of the field * @param type The type of field property to be looked up * @return A key that can be used to locate the desired value from the * application's resource bundle(s) * */ private static String getResourceKey(String canonicalId, String fieldName, String type) { return canonicalId + DELIMITER + fieldName + DELIMITER + type; } /** * Retrieve a value from the application's resource bundle(s). This * method can handle the actual work of using the * <code>ResourceManager</code> to get the desired value. * * @param key The key identifying the value to be retrieved * @return The value identified by the key * @see ResourceManager */ private static String getString(String key) { return MANAGER.getString(key); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -