类 Constants


  • public class Constants
    extends Object
    This class can be used to parse other classes containing constant definitions in public static final members. The asXXXX methods of this class allow these constant values to be accessed via their string names.

    Consider class Foo containing public final static int CONSTANT1 = 66; An instance of this class wrapping Foo.class will return the constant value of 66 from its asNumber method given the argument "CONSTANT1".

    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.

    从以下版本开始:
    16.03.2003
    作者:
    Rod Johnson, Juergen Hoeller
    • 构造器详细资料

      • Constants

        public Constants​(Class<?> clazz)
        Create a new Constants converter class wrapping the given class.

        All public static final variables will be exposed, whatever their type.

        参数:
        clazz - the class to analyze
        抛出:
        IllegalArgumentException - if the supplied clazz is null
    • 方法详细资料

      • getSize

        public final int getSize()
        Return the number of constants exposed.
      • getFieldCache

        protected final Map<String,​ObjectgetFieldCache()
        Exposes the field cache to subclasses: a Map from String field name to object value.
      • asObject

        public Object asObject​(String code)
                        throws ConstantException
        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.
        参数:
        code - the name of the field (never null)
        返回:
        the Object value
        抛出:
        ConstantException - if there's no such field
      • getNames

        public Set<StringgetNames​(String namePrefix)
        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.

        参数:
        namePrefix - prefix of the constant names to search (may be null)
        返回:
        the set of constant names
      • getNamesForSuffix

        public Set<StringgetNamesForSuffix​(String nameSuffix)
        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.

        参数:
        nameSuffix - suffix of the constant names to search (may be null)
        返回:
        the set of constant names
      • getValues

        public Set<ObjectgetValues​(String namePrefix)
        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.

        参数:
        namePrefix - prefix of the constant names to search (may be null)
        返回:
        the set of values
      • getValuesForSuffix

        public Set<ObjectgetValuesForSuffix​(String nameSuffix)
        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.

        参数:
        nameSuffix - suffix of the constant names to search (may be null)
        返回:
        the set of values
      • toCode

        public String toCode​(Object value,
                             String namePrefix)
                      throws ConstantException
        Look up the given value within the given group of constants.

        Will return the first match.

        参数:
        value - constant value to look up
        namePrefix - prefix of the constant names to search (may be null)
        返回:
        the name of the constant field
        抛出:
        ConstantException - if the value wasn't found
      • toCodeForSuffix

        public String toCodeForSuffix​(Object value,
                                      String nameSuffix)
                               throws ConstantException
        Look up the given value within the given group of constants.

        Will return the first match.

        参数:
        value - constant value to look up
        nameSuffix - suffix of the constant names to search (may be null)
        返回:
        the name of the constant field
        抛出:
        ConstantException - if the value wasn't found