Class DefaultValidationResult
- java.lang.Object
-
- net.sf.jguiraffe.transform.DefaultValidationResult
-
- All Implemented Interfaces:
ValidationResult
public final class DefaultValidationResult extends Object implements ValidationResult
A default implementation of the
ValidationResultinterface.Instances of this class store a collection of validation error messages. If no messages exist with a
ValidationMessageLevelofERROR, the validation result is considered to be valid. It may contain other messages which are only warning messages.Clients do not create instances directly through the constructor, but use the nested
Builderclass. Once a builder is created, an arbitrary number of validation error messages can be added. If all messages have been added, thebuild()method creates an immutable instance ofDefaultValidationResult. This has the advantage that all fields ofDefaultValidationResultcan be made final. Therefore objects of this class are thread-safe (note that is not true for theBuilderobjects).There is also a constant representing a validation result for a successful validation. This field can be used in custom validator implementations rather than creating a new instance of this class.
- Version:
- $Id: DefaultValidationResult.java 205 2012-01-29 18:29:57Z oheger $
- Author:
- Oliver Heger
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classDefaultValidationResult.BuilderA builder class for creating instances ofDefaultValidationResult.
-
Field Summary
Fields Modifier and Type Field Description static ValidationResultVALIDConstant for a validation result object for a successful validation.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static ValidationResultcreateValidationErrorResult(TransformerContext context, String key, Object... params)Creates aValidationResultobject with the specified error message.static ValidationMessagecreateValidationMessage(TransformerContext context, String key, Object... params)Creates aValidationMessageobject for the specified error message.booleanequals(Object obj)Compares this object to another one.Collection<ValidationMessage>getValidationMessages()Returns an unmodifiable collection with error messages.Collection<ValidationMessage>getValidationMessages(ValidationMessageLevel level)Returns an unmodifiable collection with theValidationMessageobjects with the specifiedValidationMessageLevel.inthashCode()Returns a hash code for this object.booleanhasMessages(ValidationMessageLevel level)Returns a flag whether messages of the specified level are available.booleanisValid()Returns a flag whether the validation was successful.static ValidationResultmerge(ValidationResult vr1, ValidationResult vr2)Combines two validation result objects to a combined result.StringtoString()Returns a string representation for this object.
-
-
-
Field Detail
-
VALID
public static final ValidationResult VALID
Constant for a validation result object for a successful validation.
-
-
Method Detail
-
isValid
public boolean isValid()
Returns a flag whether the validation was successful. This implementation tests if error messages have been added to this object.- Specified by:
isValidin interfaceValidationResult- Returns:
- a flag if validation was successful
-
getValidationMessages
public Collection<ValidationMessage> getValidationMessages()
Returns an unmodifiable collection with error messages.- Specified by:
getValidationMessagesin interfaceValidationResult- Returns:
- a collection with the error messages
-
getValidationMessages
public Collection<ValidationMessage> getValidationMessages(ValidationMessageLevel level)
Returns an unmodifiable collection with theValidationMessageobjects with the specifiedValidationMessageLevel.- Specified by:
getValidationMessagesin interfaceValidationResult- Parameters:
level- theValidationMessageLevel- Returns:
- a collection with the requested messages
-
hasMessages
public boolean hasMessages(ValidationMessageLevel level)
Returns a flag whether messages of the specified level are available. These flags are initialized at construction time, so this method is pretty efficient.- Specified by:
hasMessagesin interfaceValidationResult- Parameters:
level- theValidationMessageLevelin question- Returns:
- a flag whether messages with this level are available
-
equals
public boolean equals(Object obj)
Compares this object to another one. Two validation result objects are considered equal if and only if they contain the same error messages.
-
hashCode
public int hashCode()
Returns a hash code for this object.
-
toString
public String toString()
Returns a string representation for this object. This string will also contain the string representations for all contained error messages.
-
merge
public static ValidationResult merge(ValidationResult vr1, ValidationResult vr2)
Combines two validation result objects to a combined result. If one of the passed in result objects is invalid, the resulting object will also be invalid. It will contain the union of all error messages. A newValidationResultobject is created only if necessary: If one of the arguments is valid and has no warning messages, the other argument is returned. The same is true for null arguments. This method returns null only if both arguments are null.- Parameters:
vr1- the first validation result objectvr2- the second validation result object- Returns:
- a combined validation result
-
createValidationMessage
public static ValidationMessage createValidationMessage(TransformerContext context, String key, Object... params)
Creates aValidationMessageobject for the specified error message. This is a convenience method that obtains theValidationMessageHandlerfrom theTransformerContextand obtains aValidationMessagefor the specified parameters.- Parameters:
context- theTransformerContext(must not be null)key- the key of the error messageparams- optional parameters for the error message- Returns:
- a
ValidationResultobject with the specified message - Throws:
IllegalArgumentException- if theTransformerContextis null
-
createValidationErrorResult
public static ValidationResult createValidationErrorResult(TransformerContext context, String key, Object... params)
Creates aValidationResultobject with the specified error message. This is a convenience method for the frequent case that aValidationResultobject with exactly one error message has to be created. It obtains theValidationMessageHandlerfrom theTransformerContext, obtains aValidationMessagefor the specified parameters, and creates aValidationResultobject with exactly this message.- Parameters:
context- theTransformerContext(must not be null)key- the key of the error messageparams- optional parameters for the error message- Returns:
- a
ValidationResultobject with this error message - Throws:
IllegalArgumentException- if theTransformerContextis null
-
-