org.jmythapi.utils
Class GenericEnumUtils

java.lang.Object
  extended by org.jmythapi.utils.GenericEnumUtils
Direct Known Subclasses:
EnumUtils, EnumUtils

public class GenericEnumUtils
extends Object

This class provides enumeration related utility function.

This class provides methods to:

See the subclasses of this class for additional methods regarding database- or protocol-related enumeration properties.
See the javadoc of the provided methods for some usage examples of this class.


Field Summary
protected static Logger logger
          For logging
 
Constructor Summary
GenericEnumUtils()
           
 
Method Summary
static
<E extends Enum<E>,S extends IPropertyAware<E>,F extends Enum<F>,T extends IPropertyAware<F>>
Map<E,Set<F>>
copyEnumValues(S source, T target)
          A function to copy properties from a source- to a target-object.
static
<E extends Enum<E>,S extends IPropertyAware<E>,F extends Enum<F>,T extends IPropertyAware<F>>
Map<E,Set<F>>
copyEnumValues(S source, T target, boolean checkType, boolean replaceNullWithDefault, boolean timeFieldSupport)
          A function to copy properties from a source- to a target-object.
static
<E extends Enum<E>>
List<String>
getDefaultValuesList(Class<E> enumClass, EnumSet<E> enumProps)
          Gets the default values for all constants of the given enumeration-constants.
static
<T,E extends Enum<E>>
Class<T>
getEnumDataType(E enumProp)
          Gets the datatype of a enumeration property.
static
<E extends Enum<E>>
String
getEnumDefaultValue(Class<E> enumClass, Enum<E> enumProp)
          Gets the default value for a given enumeration-constant.
static
<E extends Enum<E>>
String
getEnumDefaultValue(Enum<E> enumProp)
          Gets the default value for a given enumeration-constant.
static
<S,T,E extends Enum<E>,F extends Enum<F>>
Map<E,Set<F>>
getEnumMapping(EnumSet<E> sourceProps, EnumSet<F> targetProps, boolean checkType, boolean timeFieldSupport)
          A function to determine equivalent parameters of a source and target object.
static
<E extends Enum<E>>
Map<String,E>
getEnumNameMap(EnumSet<E> props)
          Creates a map containing the names and enumeration-constants for a given enumeration-set.
static
<E extends Enum<E>>
Map<String,E>
getEnumNameMap(IPropertyAware<E> props)
          Creates a map containing the names and enumeration-constants for all properties of the property-aware object.
static
<T,E extends Enum<E>>
Class<T>
getEnumStringType(Class<E> enumClass, Enum<E> enumProp)
          Gets the string-data-type for a given enumeration-constant.
static
<T,E extends Enum<E>>
Class<T>
getEnumStringType(E enumProp)
          Gets the string-data-type for a given enumeration-constant.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

logger

protected static final Logger logger
For logging

Constructor Detail

GenericEnumUtils

public GenericEnumUtils()
Method Detail

getEnumDataType

public static <T,E extends Enum<E>> Class<T> getEnumDataType(E enumProp)
Gets the datatype of a enumeration property.

This function determines the datatype, the value of a given property should be converted to, whereas the datatype of a property is defined with the MythParameterType annotation.

The result of this function is required for the string to object conversion of property values. (See: EncodingUtils#decodeString(Class, ProtocolVersion, int, String)).

Usage example:


   public interface IProgramInfo {
     public static enum Props {
       ...
       @MythParameterType(ProgramRecordingStatus.class)
       REC_STATUS,
       ...
     }
   ...
   // the following will return ProgramRecordingStatus.class
   Class clazz = EnumUtils.getEnumDataType(IProgramInfo.Props.REC_STATUS);
 }
 

Type Parameters:
T - the property value data type
E - the property type
Parameters:
enumProp - the Enum property.
Returns:
the class of the type or null if unknown

getEnumStringType

public static <T,E extends Enum<E>> Class<T> getEnumStringType(E enumProp)
Gets the string-data-type for a given enumeration-constant.

This function determines the string data-type for an enumeration-constant. This "string-type" is e.g. used if a Boolean value should be converted into a String. In this case the "string-type" specifies if the Boolean value should be converted into 0 or 1, or into true or false.

The string data-type for an enumeration constant is defined via MythParameterType.stringType().

Type Parameters:
T - the string data-type
E - the enumeration type
Parameters:
enumProp - the enumeration constant
Returns:
the string data-type or null

getEnumStringType

public static <T,E extends Enum<E>> Class<T> getEnumStringType(Class<E> enumClass,
                                                               Enum<E> enumProp)
Gets the string-data-type for a given enumeration-constant.

This function determines the string data-type for an enumeration-constant. This "string-type" is e.g. used if a Boolean value should be converted into a String. In this case the "string-type" specifies if the Boolean value should be converted into 0 or 1, or into true or false.

The string data-type for an enumeration constant is defined via MythParameterType.stringType().

Usage example:


    // the following will return ProgramRecordingStatus.class
    Class clazz = EnumUtils.getEnumDataType(IProgramInfo.Props.REC_STATUS);
 

Type Parameters:
T - the string data-type
E - the enumeration type
Parameters:
enumClass - the enumeration class
enumProp - the enumeration constant
Returns:
the string data-type or null

getDefaultValuesList

public static <E extends Enum<E>> List<String> getDefaultValuesList(Class<E> enumClass,
                                                                    EnumSet<E> enumProps)
Gets the default values for all constants of the given enumeration-constants.

This function uses the MythParameterDefaultValue annotation to determine the default value for the enumeration-constant. If no annotation can be found, null is returned.

Type Parameters:
E - the type of the enumeration constant
Parameters:
enumClass - the enum class
enumProps - a set of enumeration constants
Returns:
a list containing the default values for all enumeration-constants.

getEnumDefaultValue

public static <E extends Enum<E>> String getEnumDefaultValue(Enum<E> enumProp)
Gets the default value for a given enumeration-constant.

This function uses the MythParameterDefaultValue annotation to determine the default value for a given enumeration-constant. If no annotation can be found, null is returned.

Usage Example:


    String defaultValue = GenericEnumUtils.getEnumDefaultValue(IProgramInfo.Props.STORAGE_GROUP);
    System.out.println(defaultValue);
 

Type Parameters:
E - the type of the enumeration constant
Parameters:
enumProp - the enumeration constant to inspect
Returns:
the default value or null.

getEnumDefaultValue

public static <E extends Enum<E>> String getEnumDefaultValue(Class<E> enumClass,
                                                             Enum<E> enumProp)
Gets the default value for a given enumeration-constant.

This function uses the MythParameterDefaultValue annotation to determine the default value for a given enumeration-constant. If no annotation can be found, null is returned.

This function is mainly used when creating a new IPropertyAware object, to init the object with default parameter values.

Type Parameters:
E - the type of the enumeration constant
Parameters:
enumClass - the enumeration class
enumProp - the enumeration constant to inspect
Returns:
the default value or null.

getEnumNameMap

public static <E extends Enum<E>> Map<String,E> getEnumNameMap(IPropertyAware<E> props)
Creates a map containing the names and enumeration-constants for all properties of the property-aware object.

Type Parameters:
E - the type of the enumeration constants
Parameters:
props - the properties to process
Returns:
a map containing the names of all properties as key and the enumeration-constants itself as values.

getEnumNameMap

public static <E extends Enum<E>> Map<String,E> getEnumNameMap(EnumSet<E> props)
Creates a map containing the names and enumeration-constants for a given enumeration-set.

Type Parameters:
E - the type of the enumeration constants
Parameters:
props - the properties to process
Returns:
a map containing the names of all properties as key and the enumeration-constants itself as values.

getEnumMapping

public static <S,T,E extends Enum<E>,F extends Enum<F>> Map<E,Set<F>> getEnumMapping(EnumSet<E> sourceProps,
                                                                                     EnumSet<F> targetProps,
                                                                                     boolean checkType,
                                                                                     boolean timeFieldSupport)
A function to determine equivalent parameters of a source and target object.

This function determines the properties of a source- and target-object and compares them by name. Properties with the same name are handled as equivalent. Optionally the specified parameter-types can be taken into account.
If the timeFieldSupport is set, source-properties with the name XXX_DATE_TIME are handled as equivalent to target-properties with the names XXX_DATE and XXX_TIME.


copyEnumValues

public static final <E extends Enum<E>,S extends IPropertyAware<E>,F extends Enum<F>,T extends IPropertyAware<F>> Map<E,Set<F>> copyEnumValues(S source,
                                                                                                                                               T target)
A function to copy properties from a source- to a target-object.

This function determines the properties of the source- and target-object and copies the values of properties with the same name from source to target.
The source- and target-object must be IPropertyAware.

Usage example:


   // the source object
   RecorderNextProgramInfo nextProgram = ....;
 	
   // the target object
   ProgramInfo programInfo = new ProgramInfo(nextProgram.getVersionNr());
 	
   // copy properties
   GenericEnumUtils.copyEnumValues(nextProgram,programInfo);
 

Type Parameters:
E - the type of the source properties
S - the type of the source object
F - the type of the target properties
T - the type of the target object
Parameters:
source - the source object
target - the target object

copyEnumValues

public static final <E extends Enum<E>,S extends IPropertyAware<E>,F extends Enum<F>,T extends IPropertyAware<F>> Map<E,Set<F>> copyEnumValues(S source,
                                                                                                                                               T target,
                                                                                                                                               boolean checkType,
                                                                                                                                               boolean replaceNullWithDefault,
                                                                                                                                               boolean timeFieldSupport)
A function to copy properties from a source- to a target-object.

This function determines the properties of the source- and target-object and copies the values of properties with the same name from source to target.
The source- and target-object must be IPropertyAware.

Usage example:


   // the source object
   RecorderNextProgramInfo nextProgram = ....;
 	
   // the target object
   ProgramInfo programInfo = new ProgramInfo(nextProgram.getVersionNr());
 	
   // copy properties
   GenericEnumUtils.copyEnumValues(nextProgram,programInfo, true, true, true);
 

Type Parameters:
E - the type of the source properties
S - the type of the source object
F - the type of the target properties
T - the type of the target object
Parameters:
source - the source object
target - the target object
checkType - if the source- and target- property must have the same datatype.
replaceNullWithDefault - if null values of source-properties should be replaced with the default-values specified for target-properties.
timeFieldSupport - if source-properties with the name XXX_DATE_TIME should be used to set the value of target-properties with the name XXX_DATE and XXX_TIME


Copyright © 2008-2013. All Rights Reserved.