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
ValidationResult
interface.Instances of this class store a collection of validation error messages. If no messages exist with a
ValidationMessageLevel
ofERROR
, 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
Builder
class. 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 ofDefaultValidationResult
can be made final. Therefore objects of this class are thread-safe (note that is not true for theBuilder
objects).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 class
DefaultValidationResult.Builder
A builder class for creating instances ofDefaultValidationResult
.
-
Field Summary
Fields Modifier and Type Field Description static ValidationResult
VALID
Constant 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 ValidationResult
createValidationErrorResult(TransformerContext context, String key, Object... params)
Creates aValidationResult
object with the specified error message.static ValidationMessage
createValidationMessage(TransformerContext context, String key, Object... params)
Creates aValidationMessage
object for the specified error message.boolean
equals(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 theValidationMessage
objects with the specifiedValidationMessageLevel
.int
hashCode()
Returns a hash code for this object.boolean
hasMessages(ValidationMessageLevel level)
Returns a flag whether messages of the specified level are available.boolean
isValid()
Returns a flag whether the validation was successful.static ValidationResult
merge(ValidationResult vr1, ValidationResult vr2)
Combines two validation result objects to a combined result.String
toString()
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:
isValid
in interfaceValidationResult
- Returns:
- a flag if validation was successful
-
getValidationMessages
public Collection<ValidationMessage> getValidationMessages()
Returns an unmodifiable collection with error messages.- Specified by:
getValidationMessages
in interfaceValidationResult
- Returns:
- a collection with the error messages
-
getValidationMessages
public Collection<ValidationMessage> getValidationMessages(ValidationMessageLevel level)
Returns an unmodifiable collection with theValidationMessage
objects with the specifiedValidationMessageLevel
.- Specified by:
getValidationMessages
in 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:
hasMessages
in interfaceValidationResult
- Parameters:
level
- theValidationMessageLevel
in 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 newValidationResult
object 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 aValidationMessage
object for the specified error message. This is a convenience method that obtains theValidationMessageHandler
from theTransformerContext
and obtains aValidationMessage
for 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
ValidationResult
object with the specified message - Throws:
IllegalArgumentException
- if theTransformerContext
is null
-
createValidationErrorResult
public static ValidationResult createValidationErrorResult(TransformerContext context, String key, Object... params)
Creates aValidationResult
object with the specified error message. This is a convenience method for the frequent case that aValidationResult
object with exactly one error message has to be created. It obtains theValidationMessageHandler
from theTransformerContext
, obtains aValidationMessage
for the specified parameters, and creates aValidationResult
object 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
ValidationResult
object with this error message - Throws:
IllegalArgumentException
- if theTransformerContext
is null
-
-