Package net.sf.jguiraffe.gui.forms
Class DefaultFormValidatorResults
- java.lang.Object
-
- net.sf.jguiraffe.gui.forms.DefaultFormValidatorResults
-
- All Implemented Interfaces:
FormValidatorResults
public class DefaultFormValidatorResults extends Object implements FormValidatorResults
Default implementation of the
FormValidatorResultsinterface.This class provides a fully functional implementation of the
FormValidatorResultsinterface. Instances are initialized with a map that contains the names of the validated fields and their correspondingValidationResultobjects. They are immutable and thus can be shared between multiple threads.- Version:
- $Id: DefaultFormValidatorResults.java 205 2012-01-29 18:29:57Z oheger $
- Author:
- Oliver Heger
-
-
Constructor Summary
Constructors Constructor Description DefaultFormValidatorResults(Map<String,? extends ValidationResult> fieldData)Creates a new instance ofDefaultFormValidatorResultsand initializes it with a map holding information about fields and their validation status.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static ValidationResultcreateValidationErrorResult(Form form, String key, Object... params)Creates aValidationResultobject with an error message for a validation message.static ValidationMessagecreateValidationMessage(Form form, String key, Object... params)Obtains aValidationMessageobject for the specified key.booleanequals(Object obj)Compares this object with another one.Set<String>getErrorFieldNames()Returns a set with the names of those fields that are invalid.Set<String>getFieldNames()Returns a set with all defined field names.ValidationResultgetResultsFor(String field)Returns the validation results for the specified field or null if this field does not exist.inthashCode()Determines a hash code for this object.booleanisValid()Checks whether form validation was successful.static FormValidatorResultsmerge(FormValidatorResults res1, FormValidatorResults res2)Returns aFormValidatorResultsobject with the combined information of the specifiedFormValidatorResultsobjects.StringtoString()Returns a string representation for this object.static Map<String,ValidationResult>validResultMapForForm(Form form)Creates ajava.util.Mapwith validation information for all fields of the specifiedForm.static DefaultFormValidatorResultsvalidResultsForForm(Form form)Creates a validDefaultFormValidatorResultsfor the fields of the specifiedForm.
-
-
-
Constructor Detail
-
DefaultFormValidatorResults
public DefaultFormValidatorResults(Map<String,? extends ValidationResult> fieldData)
Creates a new instance ofDefaultFormValidatorResultsand initializes it with a map holding information about fields and their validation status.- Parameters:
fieldData- the map with the field data (must not be null- Throws:
IllegalArgumentException- if the map is null or contains null values
-
-
Method Detail
-
validResultMapForForm
public static Map<String,ValidationResult> validResultMapForForm(Form form)
Creates ajava.util.Mapwith validation information for all fields of the specifiedForm. Each field is assigned a validValidationResultobject. This method can be used by customFormValidatorimplementations to initialize a map with data about all form fields. Then, during validation, fields that were detected to be invalid can be updated in this map. Finally, aDefaultFormValidatorResultsobject can be created based on this map.- Parameters:
form- theForm(must not be null)- Returns:
- a map with valid validation data for all fields of the form
- Throws:
IllegalArgumentException- if theFormis null
-
validResultsForForm
public static DefaultFormValidatorResults validResultsForForm(Form form)
Creates a validDefaultFormValidatorResultsfor the fields of the specifiedForm. The resulting object stores a validValidationResultobject for all fields that belong to the form.- Parameters:
form- theForm(must not be null)- Returns:
- a valid
DefaultFormValidatorResultsobject for this form - Throws:
IllegalArgumentException- if theFormis null
-
createValidationMessage
public static ValidationMessage createValidationMessage(Form form, String key, Object... params)
Obtains aValidationMessageobject for the specified key. The message is obtained from the currentValidationMessageHandler(which can be retrieved from the form'sTransformerContext).- Parameters:
form- theForm(must not be null)key- the key of the validation error messageparams- additional parameters for the validation error message- Returns:
- a
ValidationMessageobject for the specified error message - Throws:
IllegalArgumentException- if theFormis null
-
createValidationErrorResult
public static ValidationResult createValidationErrorResult(Form form, String key, Object... params)
Creates aValidationResultobject with an error message for a validation message. This is a convenience method that obtains the validation error message from the currentValidationMessageHandlerand creates the correspondingValidationResultobject. It can be used for instance by aFormValidatorthat determines an incorrect field.- Parameters:
form- theForm(must not be null)key- the key of the validation error messageparams- additional parameters for the validation error message- Returns:
- a
ValidationResultobject initialized with this error message - Throws:
IllegalArgumentException- if theFormis null
-
merge
public static FormValidatorResults merge(FormValidatorResults res1, FormValidatorResults res2)
Returns aFormValidatorResultsobject with the combined information of the specifiedFormValidatorResultsobjects. If one of the passed in objects is null, the other one is returned. Otherwise a newFormValidatorResultsinstance is created and populated with theValidationResultobjects contained in both parameter objects. If necessary, theValidationResultobjects are also merged, so that the resulting object actually contains a union of all validation messages.- Parameters:
res1- the firstFormValidatorResultsobjectres2- the secondFormValidatorResultsobject- Returns:
- the merged results
-
isValid
public boolean isValid()
Checks whether form validation was successful.- Specified by:
isValidin interfaceFormValidatorResults- Returns:
- a flag if all fields are valid
-
getFieldNames
public Set<String> getFieldNames()
Returns a set with all defined field names.- Specified by:
getFieldNamesin interfaceFormValidatorResults- Returns:
- a set with the field names
-
getErrorFieldNames
public Set<String> getErrorFieldNames()
Returns a set with the names of those fields that are invalid.- Specified by:
getErrorFieldNamesin interfaceFormValidatorResults- Returns:
- a set with the names of the invalid fields
-
getResultsFor
public ValidationResult getResultsFor(String field)
Returns the validation results for the specified field or null if this field does not exist.- Specified by:
getResultsForin interfaceFormValidatorResults- Parameters:
field- the name of the desired field- Returns:
- the validation result for this field
-
equals
public boolean equals(Object obj)
Compares this object with another one. Two form validator result objects are considered equal if and only if they contain the same set of fields with corresponding result objects.
-
hashCode
public int hashCode()
Determines a hash code for this object.
-
toString
public String toString()
Returns a string representation for this object. This string contains the string representation for the internal map with the fields and their validation result objects. Provided that theValidationResultobjects have propertoString()implementations, the string generated by the map is exactly what is needed to see which field has which validation status.
-
-