Class EnumConverter
- java.lang.Object
-
- jakarta.faces.convert.EnumConverter
-
- All Implemented Interfaces:
PartialStateHolder,StateHolder,Converter
public class EnumConverter extends Object implements Converter, PartialStateHolder
Converterimplementation forjava.lang.Enum(and enum primitive) values.- Since:
- 1.2
-
-
Field Summary
Fields Modifier and Type Field Description static StringCONVERTER_IDThe standard converter id for this converter.static StringENUM_IDThe message identifier of theFacesMessageto be created if the conversion toEnumfails.static StringENUM_NO_CLASS_IDThe message identifier of theFacesMessageto be created if the conversion toEnumfails and no target class has been provided.-
Fields inherited from interface jakarta.faces.convert.Converter
DATETIMECONVERTER_DEFAULT_TIMEZONE_IS_SYSTEM_TIMEZONE_PARAM_NAME
-
-
Constructor Summary
Constructors Constructor Description EnumConverter()For StateHolderEnumConverter(Class targetClass)Instantiates an enum converter with a class where enum constants are taken from.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclearInitialState()Reset the PartialStateHolder to a non-delta tracking state.ObjectgetAsObject(FacesContext context, UIComponent component, String value)Convert thevalueargument to one of the enum constants of the class provided in our constructor.StringgetAsString(FacesContext context, UIComponent component, Object value)Convert the enum constant given by thevalueargument into a String.booleaninitialStateMarked()Returntrueif delta state changes are being tracked, otherwisefalsebooleanisTransient()If true, the Object implementing this interface must not participate in state saving or restoring.voidmarkInitialState()The runtime must ensure that thePartialStateHolder.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.voidrestoreState(FacesContext facesContext, Object object)Perform any processing required to restore the state from the entries in the state Object.ObjectsaveState(FacesContext facesContext)Gets the state of the instance as aSerializableObject.voidsetTransient(boolean b)Denotes whether or not the Object implementing this interface must or must not participate in state saving or restoring.
-
-
-
Field Detail
-
CONVERTER_ID
public static final String CONVERTER_ID
The standard converter id for this converter.
- See Also:
- Constant Field Values
-
ENUM_ID
public static final String ENUM_ID
The message identifier of the
FacesMessageto be created if the conversion toEnumfails. The message format string for this message may optionally include the following placeholders:{0}replaced by the unconverted value.{1}replaced by one of the enum constants or the empty string if none can be found.{2}replaced by aStringwhose value is the label of the input component that produced this message.
- See Also:
- Constant Field Values
-
ENUM_NO_CLASS_ID
public static final String ENUM_NO_CLASS_ID
The message identifier of the
FacesMessageto be created if the conversion toEnumfails and no target class has been provided. The message format string for this message may optionally include the following placeholders:{0}replaced by the unconverted value.{1}replaced by aStringwhose value is the label of the input component that produced this message.
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
EnumConverter
public EnumConverter()
For StateHolder
-
EnumConverter
public EnumConverter(Class targetClass)
Instantiates an enum converter with a class where enum constants are taken from.- Parameters:
targetClass- Class where the enum constants are taken from by the converter methods.
-
-
Method Detail
-
getAsObject
public Object getAsObject(FacesContext context, UIComponent component, String value)
Convert the
valueargument 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 aConverterExceptioncontaining theENUM_NO_CLASS_IDmessage with proper parameters. If thevalueargument isnullor it has a length of zero, returnnull. Otherwise, perform the equivalent ofEnum.valueOfusing target class andvalueand return theObject. If the conversion fails, throw aConverterExceptioncontaining theENUM_IDmessage with proper parameters.- Specified by:
getAsObjectin interfaceConverter- Parameters:
context- theFacesContextfor this request.component- theUIComponentto which this value will be applied.value- the Stringvalueto be converted toObject.- Returns:
nullif the value to convert isnull, otherwise the result of the conversion- Throws:
ConverterException- if conversion cannot be successfully performedNullPointerException- ifcontextorcomponentisnull
-
getAsString
public String getAsString(FacesContext context, UIComponent component, Object value)
Convert the enum constant given by the
valueargument into a String. If no target class argument has been provided to the constructor of this instance, throw aConverterExceptioncontaining theENUM_NO_CLASS_IDmessage with proper parameters. If thevalueargument isnull, return the empty String. If the value is an instance of the provided target class, return its string value by casting it to ajava.lang.Enumand returning the result of calling thename()method. Otherwise, throw aConverterExceptioncontaining theENUM_IDmessage with proper parameters.- Specified by:
getAsStringin interfaceConverter- Parameters:
context-FacesContextfor the request being processedcomponent-UIComponentwith which this model object value is associatedvalue- Model object value to be converted (may benull)- Returns:
- a zero-length String if value is
null, otherwise the result of the conversion - Throws:
ConverterException- if conversion cannot be successfully performedNullPointerException- ifcontextorcomponentisnull
-
restoreState
public void restoreState(FacesContext facesContext, Object object)
Description copied from interface:StateHolderPerform 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
UIComponentwith event handlers, validators, etc.) this method must call theStateHolder.restoreState(jakarta.faces.context.FacesContext, java.lang.Object)method on all those instances as well.If the
stateargument isnull, take no action and return.- Specified by:
restoreStatein interfaceStateHolder- Parameters:
facesContext- the Faces context.object- the state.
-
saveState
public Object saveState(FacesContext facesContext)
Description copied from interface:StateHolderGets the state of the instance as a
SerializableObject.If the class that implements this interface has references to instances that implement StateHolder (such as a
UIComponentwith event handlers, validators, etc.) this method must call theStateHolder.saveState(jakarta.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 theStateManagerThis method must not alter the state of the implementing object. In other words, after executing this code:
Object state = component.saveState(facesContext);componentshould be the same as before executing it.The return from this method must be
Serializable- Specified by:
saveStatein interfaceStateHolder- Parameters:
facesContext- the Faces context.- Returns:
- the saved state.
-
setTransient
public void setTransient(boolean b)
Description copied from interface:StateHolderDenotes whether or not the Object implementing this interface must or must not participate in state saving or restoring.
- Specified by:
setTransientin interfaceStateHolder- Parameters:
b- boolean passtrueif this Object will not participate in state saving or restoring, otherwise passfalse.
-
isTransient
public boolean isTransient()
Description copied from interface:StateHolderIf true, the Object implementing this interface must not participate in state saving or restoring.
- Specified by:
isTransientin interfaceStateHolder- Returns:
trueif transient,falseotherwise.
-
markInitialState
public void markInitialState()
Description copied from interface:PartialStateHolderThe 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 thatPartialStateHolder.initialStateMarked()returnstruefrom the timemarkInitialState()is called untilPartialStateHolder.clearInitialState()is called, after which timeinitialStateMarked()must returnfalse. Also, during the time that the instance returnstruefrominitialStateMarked(), the implementation must return only the state that has changed in its implementation ofStateHolder.saveState(jakarta.faces.context.FacesContext).- Specified by:
markInitialStatein interfacePartialStateHolder
-
initialStateMarked
public boolean initialStateMarked()
Description copied from interface:PartialStateHolderReturn
trueif delta state changes are being tracked, otherwisefalse- Specified by:
initialStateMarkedin interfacePartialStateHolder- Returns:
trueif the initial state is marked,falseotherwise.
-
clearInitialState
public void clearInitialState()
Description copied from interface:PartialStateHolderReset the PartialStateHolder to a non-delta tracking state.
- Specified by:
clearInitialStatein interfacePartialStateHolder
-
-