javax.faces.convert
Class EnumConverter

java.lang.Object
  extended by javax.faces.convert.EnumConverter
All Implemented Interfaces:
PartialStateHolder, StateHolder, Converter

public class EnumConverter
extends java.lang.Object
implements Converter, PartialStateHolder

Converter implementation for java.lang.Enum (and enum primitive) values.

Since:
1.2

Field Summary
static java.lang.String CONVERTER_ID
          The standard converter id for this converter.
static java.lang.String ENUM_ID
          The message identifier of the FacesMessage to be created if the conversion to Enum fails.
static java.lang.String ENUM_NO_CLASS_ID
          The message identifier of the FacesMessage to be created if the conversion to Enum fails and no target class has been provided.
 
Constructor Summary
EnumConverter()
           
EnumConverter(java.lang.Class targetClass)
           
 
Method Summary
 void clearInitialState()
          

Reset the PartialStateHolder to a non-delta tracking state.

 java.lang.Object getAsObject(FacesContext context, UIComponent component, java.lang.String value)
          Convert the value argument to one of the enum constants of the class provided in our constructor.
 java.lang.String getAsString(FacesContext context, UIComponent component, java.lang.Object value)
          Convert the enum constant given by the value argument into a String.
 boolean initialStateMarked()
          

Return true if delta state changes are being tracked, otherwise false

 boolean isTransient()
          If true, the Object implementing this interface must not participate in state saving or restoring.
 void markInitialState()
          

The runtime must ensure that the PartialStateHolder.markInitialState() method is called on each instance of this interface in the view at the appropriate time to indicate the component is in its initial state.

 void restoreState(FacesContext facesContext, java.lang.Object object)
          Perform any processing required to restore the state from the entries in the state Object.
 java.lang.Object saveState(FacesContext facesContext)
           Gets the state of the instance as a Serializable Object.
 void setTransient(boolean b)
          Denotes whether or not the Object implementing this interface must or must not participate in state saving or restoring.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CONVERTER_ID

public static final java.lang.String CONVERTER_ID

The standard converter id for this converter.

See Also:
Constant Field Values

ENUM_ID

public static final java.lang.String ENUM_ID

The message identifier of the FacesMessage to be created if the conversion to Enum fails. The message format string for this message may optionally include the following placeholders:

See Also:
Constant Field Values

ENUM_NO_CLASS_ID

public static final java.lang.String ENUM_NO_CLASS_ID

The message identifier of the FacesMessage to be created if the conversion to Enum fails and no target class has been provided. The message format string for this message may optionally include the following placeholders:

See Also:
Constant Field Values
Constructor Detail

EnumConverter

public EnumConverter()

EnumConverter

public EnumConverter(java.lang.Class targetClass)
Method Detail

getAsObject

public java.lang.Object getAsObject(FacesContext context,
                                    UIComponent component,
                                    java.lang.String value)

Convert the value argument to one of the enum constants of the class provided in our constructor. If no target class argument has been provided to the constructor of this instance, throw a ConverterException containing the ENUM_NO_CLASS_ID message with proper parameters. If the value argument is null or it has a length of zero, return null. Otherwise, perform the equivalent of Enum.valueOf using target class and value and return the Object. If the conversion fails, throw a ConverterException containing the ENUM_ID message with proper parameters.

Specified by:
getAsObject in interface Converter
Parameters:
context - the FacesContext for this request.
component - the UIComponent to which this value will be applied.
value - the String value to be converted to Object.
Returns:
null if the value to convert is null, otherwise the result of the conversion
Throws:
ConverterException - if conversion cannot be successfully performed
java.lang.NullPointerException - if context or component is null

getAsString

public java.lang.String getAsString(FacesContext context,
                                    UIComponent component,
                                    java.lang.Object value)

Convert the enum constant given by the value argument into a String. If no target class argument has been provided to the constructor of this instance, throw a ConverterException containing the ENUM_NO_CLASS_ID message with proper parameters. If the value argument is null, return null. If the value is an instance of the provided target class, return its string value by casting it to a java.lang.Enum and returning the result of calling the name() method. Otherwise, throw a ConverterException containing the ENUM_ID message with proper parameters.

Specified by:
getAsString in interface Converter
Parameters:
context - FacesContext for the request being processed
component - UIComponent with which this model object value is associated
value - Model object value to be converted (may be null)
Returns:
a zero-length String if value is null, otherwise the result of the conversion
Throws:
ConverterException - if conversion cannot be successfully performed
java.lang.NullPointerException - if context or component is null

restoreState

public void restoreState(FacesContext facesContext,
                         java.lang.Object object)
Description copied from interface: StateHolder

Perform any processing required to restore the state from the entries in the state Object.

If the class that implements this interface has references to instances that also implement StateHolder (such as a UIComponent with event handlers, validators, etc.) this method must call the StateHolder.restoreState(javax.faces.context.FacesContext, java.lang.Object) method on all those instances as well.

If the state argument is null, take no action and return.

Specified by:
restoreState in interface StateHolder

saveState

public java.lang.Object saveState(FacesContext facesContext)
Description copied from interface: StateHolder

Gets the state of the instance as a Serializable Object.

If the class that implements this interface has references to instances that implement StateHolder (such as a UIComponent with event handlers, validators, etc.) this method must call the StateHolder.saveState(javax.faces.context.FacesContext) method on all those instances as well. This method must not save the state of children and facets. That is done via the StateManager

This method must not alter the state of the implementing object. In other words, after executing this code:

 Object state = component.saveState(facesContext);
 

component should be the same as before executing it.

The return from this method must be Serializable

Specified by:
saveState in interface StateHolder

setTransient

public void setTransient(boolean b)
Description copied from interface: StateHolder

Denotes whether or not the Object implementing this interface must or must not participate in state saving or restoring.

Specified by:
setTransient in interface StateHolder
Parameters:
b - boolean pass true if this Object will not participate in state saving or restoring, otherwise pass false.

isTransient

public boolean isTransient()
Description copied from interface: StateHolder

If true, the Object implementing this interface must not participate in state saving or restoring.

Specified by:
isTransient in interface StateHolder

markInitialState

public void markInitialState()
Description copied from interface: PartialStateHolder

The runtime must ensure that the PartialStateHolder.markInitialState() method is called on each instance of this interface in the view at the appropriate time to indicate the component is in its initial state. The implementor of the interface must ensure that PartialStateHolder.initialStateMarked() returns true from the time markInitialState() is called until PartialStateHolder.clearInitialState() is called, after which time initialStateMarked() must return false. Also, during the time that the instance returns true from initialStateMarked(), the implementation must return only the state that has changed in its implementation of StateHolder.saveState(javax.faces.context.FacesContext).

Specified by:
markInitialState in interface PartialStateHolder

initialStateMarked

public boolean initialStateMarked()
Description copied from interface: PartialStateHolder

Return true if delta state changes are being tracked, otherwise false

Specified by:
initialStateMarked in interface PartialStateHolder

clearInitialState

public void clearInitialState()
Description copied from interface: PartialStateHolder

Reset the PartialStateHolder to a non-delta tracking state.

Specified by:
clearInitialState in interface PartialStateHolder


Copyright 2002-2010 Oracle America Inc, Inc. All Rights Reserved.