Skip navigation links


org.apache.myfaces.trinidad.util
Class Enums

java.lang.Object
  extended by org.apache.myfaces.trinidad.util.Enums


public final class Enums
extends Object

Enum-related utilities.


Nested Class Summary
static interface Enums.EnumParser<E extends Enum>
          Single abstract method interface for parsing enum constant values from strings.
static interface Enums.StringProducer<E extends Enum>
          Single abstract method interface for converting Enum constant values to strings.

 

Constructor Summary
Enums()
           

 

Method Summary
static
<E extends Enum>
Map<String,E>
createDisplayNameMap(Class<E> enumClass)
          Convenience method for creating String -> enum map where the String keys are produced by calling the displayName() method on each enum constant.
static
<E extends Enum>
Map<String,E>
createStringKeyMap(Class<E> enumClass, Enums.StringProducer<E> keyProducer)
          Returns a map of String -> enum constant for all constants in the provided enum class.
static
<E extends Enum>
Enums.EnumParser<E>
displayNameEnumParser(Class<E> enumClass)
          Returns an EnumParser that converts String values to enum constants by calling valueOfDisplayName() on the enum class.
static
<E extends Enum>
Enums.StringProducer<E>
displayNameStringProducer(Class<E> enumClass)
          Returns a StringProducer that converts enum constants to String values by calling displayName() on each enum constant.
static
<E extends Enum>
Enums.EnumParser<E>
methodNameEnumParser(Class<E> enumClass, String methodName)
          Returns an EnumParser that uses reflection to invoke a method on an enum class to translate String values to enum constants.
static
<E extends Enum>
Enums.StringProducer<E>
methodNameStringProducer(Class<E> enumClass, String methodName)
          Returns a StringProducer uses reflection to produce Strings from enum constant values.
static
<E extends Enum>
Collection<E>
parseDisplayNameEnumRequestParameter(javax.faces.context.ExternalContext external, String paramName, Class<E> enumClass, E defaultValue)
          Convenience for Enums.parseEnumRequestParameter() that uses a display name-based EnumParser to parse values.
static
<E extends Enum>
Collection<E>
parseEnumRequestParameter(javax.faces.context.ExternalContext external, String paramName, Class<E> enumClass, Enums.EnumParser<E> enumParser, E defaultValue)
          Parses a request parameter into a collection of enum constant values.
static
<E extends Enum>
Collection<E>
parseEnumValues(Collection<String> valuesToParse, Class<E> enumClass, Enums.EnumParser<E> enumParser, E defaultValue)
          Parses a collection of strings into a corresponding collection of enum constant values.
static
<E extends Enum>
String
patternOf(Class<E> enumClass, Enums.StringProducer<E> patternProducer)
          Returns a string suitable for use in a regular expression pattern that matches all values of the specified enum.
static
<T extends Enum>
T
stringToEnum(Map<String,T> stringToEnumMap, String key, Class<T> enumClass)
          Looks up an Enum value in a String -> Enum map and throws an IllegalArgumentException if a value is not found.

 

Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

 

Constructor Detail

Enums

public Enums()

Method Detail

createStringKeyMap

public static <E extends Enum> Map<String,E> createStringKeyMap(Class<E> enumClass,
                                                                Enums.StringProducer<E> keyProducer)
Returns a map of String -> enum constant for all constants in the provided enum class. This is intended for use by Enum implementations that wish to provide Enum.valueOf(String)-like functionality, but for String values other than the canonical enum constant names. The returned Map is not synchronized but can be used in multithreaded environments as long as it is a) not modified and b) safely published.
Parameters:
enumClass - the enum class from which the map will be created
keyProducer - called for each enum constant to produce the string key for the map.

stringToEnum

public static <T extends Enum> T stringToEnum(Map<String,T> stringToEnumMap,
                                              String key,
                                              Class<T> enumClass)
Looks up an Enum value in a String -> Enum map and throws an IllegalArgumentException if a value is not found.
Parameters:
stringToEnumMap - the map containing String -> enum mappings
key - the string value to look up in the map
enumClass - the target enum class
Returns:
a non-null Enum value corresponding to the key.
Throws:
IllegalArgumentException - if no value is found.

patternOf

public static <E extends Enum> String patternOf(Class<E> enumClass,
                                                Enums.StringProducer<E> patternProducer)
Returns a string suitable for use in a regular expression pattern that matches all values of the specified enum.
Parameters:
enumClass - the enum class from which the pattern will be derived.
patternProducer - called for each enum constant value to produce the string representation to use in the pattern.

parseEnumValues

public static <E extends Enum> Collection<E> parseEnumValues(Collection<String> valuesToParse,
                                                             Class<E> enumClass,
                                                             Enums.EnumParser<E> enumParser,
                                                             E defaultValue)
                                                  throws EnumParseException
Parses a collection of strings into a corresponding collection of enum constant values. The returned Collection is not synchronized but can be used in multithreaded environments as long as it is a) not modified and b) safely published.
Parameters:
valuesToParse - string values to parse
enumClass - target enum class
defaultValue - an enum value to include in the parsed collection if valueToParse is empty.
enumParser - called for each string value to convert to an enum constant.
Throws:
EnumParseException - if the EnumParser fails to parse one of the values.

parseEnumRequestParameter

public static <E extends Enum> Collection<E> parseEnumRequestParameter(javax.faces.context.ExternalContext external,
                                                                       String paramName,
                                                                       Class<E> enumClass,
                                                                       Enums.EnumParser<E> enumParser,
                                                                       E defaultValue)
                                                            throws EnumParseException
Parses a request parameter into a collection of enum constant values. The returned Collection is not synchronized but can be used in multithreaded environments as long as it is a) not modified and b) safely published.
Parameters:
external - the ExternalContext that defines the request parameter values
paramName - the name of the request parameter to parse
enumClass - target enum class
defaultValue - an enum value to include in the parsed collection if valuesToParse is empty.
enumParser - called for each string value to convert to an enum constant.
Throws:
EnumParseException - if the EnumParser fails to parse one of the values.

createDisplayNameMap

public static <E extends Enum> Map<String,E> createDisplayNameMap(Class<E> enumClass)
Convenience method for creating String -> enum map where the String keys are produced by calling the displayName() method on each enum constant.
See Also:
createStringKeyMap(java.lang.Class<E>, org.apache.myfaces.trinidad.util.Enums.StringProducer<E>)

displayNameStringProducer

public static <E extends Enum> Enums.StringProducer<E> displayNameStringProducer(Class<E> enumClass)
                                                                      throws IllegalArgumentException
Returns a StringProducer that converts enum constants to String values by calling displayName() on each enum constant. This implementation is not especially efficient, as it uses method reflection, but should be sufficient for 1-time initialization cases.
Parameters:
enumClass - the class of the enum for which we want to produce strings. This class must provide a public displayName() method.
Throws:
IllegalArgumentException - if the enum class does not provide a publicly accessible displayName() method.

displayNameEnumParser

public static <E extends Enum> Enums.EnumParser<E> displayNameEnumParser(Class<E> enumClass)
Returns an EnumParser that converts String values to enum constants by calling valueOfDisplayName() on the enum class.
Parameters:
enumClass - the target class to which String values are parsed. This class must provide a public static valueOfDisplayName(String) method.
Throws:
IllegalArgumentException - if the enum class does not provide a publicly accessible valueOfDisplayName(String) method.

methodNameStringProducer

public static <E extends Enum> Enums.StringProducer<E> methodNameStringProducer(Class<E> enumClass,
                                                                                String methodName)
                                                                     throws IllegalArgumentException
Returns a StringProducer uses reflection to produce Strings from enum constant values.
Parameters:
enumClass - the enum class on which the StringProducer operates
methodName - the name of the method which the StringProducer invokes
Throws:
IllegalArgumentException - if the method specified by the methodName argument does not exist.

methodNameEnumParser

public static <E extends Enum> Enums.EnumParser<E> methodNameEnumParser(Class<E> enumClass,
                                                                        String methodName)
                                                             throws IllegalArgumentException
Returns an EnumParser that uses reflection to invoke a method on an enum class to translate String values to enum constants.
Parameters:
enumClass - the target enum class
methodName - the name of the method to invoke
Throws:
IllegalArgumentException - if the method specified by the methodName argument does not exist.

parseDisplayNameEnumRequestParameter

public static <E extends Enum> Collection<E> parseDisplayNameEnumRequestParameter(javax.faces.context.ExternalContext external,
                                                                                  String paramName,
                                                                                  Class<E> enumClass,
                                                                                  E defaultValue)
                                                                       throws EnumParseException
Convenience for Enums.parseEnumRequestParameter() that uses a display name-based EnumParser to parse values. In addition, a display name-based StringProducer is used to provide a more detailed error message in the event of a parse exception.
Throws:
EnumParseException
See Also:
parseEnumRequestParameter(javax.faces.context.ExternalContext, java.lang.String, java.lang.Class<E>, org.apache.myfaces.trinidad.util.Enums.EnumParser<E>, E), displayNameEnumParser(java.lang.Class<E>), displayNameStringProducer(java.lang.Class<E>)

Skip navigation links


Copyright © 2001-2013 The Apache Software Foundation. All Rights Reserved.