com.jgoodies.validation
Class ValidationResult

java.lang.Object
  extended by com.jgoodies.validation.ValidationResult
All Implemented Interfaces:
Serializable, Iterable<ValidationMessage>

public final class ValidationResult
extends Object
implements Iterable<ValidationMessage>, Serializable

Describes a validation result as a list of ValidationMessages. You can add single validation messages, single text messages, lists of messages, and all messages from another ValidationResult.

Version:
$Revision: 1.24 $
Author:
Karsten Lentzsch
See Also:
ValidationMessage, Validator, Serialized Form

Field Summary
static ValidationResult EMPTY
          A constant for an empty and unmodifiable validation result.
 
Constructor Summary
ValidationResult()
          Constructs an empty modifiable ValidationResult.
 
Method Summary
 void add(ValidationMessage validationMessage)
          Adds a new ValidationMessage to the list of messages.
 void addAll(List<ValidationMessage> messages)
          Adds all messages from the given list to this validation result.
 void addAllFrom(ValidationResult validationResult)
          Adds all messages from the given ValidationResult to the list of messages that this validation result holds.
 void addError(String text)
          Creates and adds an error message to the list of validation messages using the given text.
 void addError(String text, String key)
          Creates and adds an error message to the list of validation messages using the given text and validation message key.
 void addWarning(String text)
          Creates and adds a warning message to the list of validation messages using the given text.
 void addWarning(String text, String key)
          Creates and adds a warning message to the list of validation messages using the given text.
 boolean contains(ValidationMessage message)
          Checks and answers whether this result contains the specified message.
 boolean equals(Object o)
          Compares the specified object with this validation result for equality.
 ValidationMessage get(int index)
          Returns the message at the specified position in this result.
 List<ValidationMessage> getErrors()
          Returns an unmodifiable List of the validation messages that indicate errors.
 List<ValidationMessage> getMessages()
          Returns an unmodifiable List of all validation messages.
 String getMessagesText()
          Returns a string representation of the message list.
 Severity getSeverity()
          Returns the highest severity of this result's messages, Severity.OK if there are no messages.
 List<ValidationMessage> getWarnings()
          Returns an unmodifiable List of the validation messages that indicate warnings.
 boolean hasErrors()
          Checks and answers whether this validation result contains a message of type ERROR.
 int hashCode()
          Returns the hash code value for this validation result.
 boolean hasMessages()
          Checks and answers whether this validation result has messages or not.
 boolean hasWarnings()
          Checks and answers whether this validation result contains a message of type WARNING.
 boolean isEmpty()
          Checks and answers whether this validation result contains no messages.
 boolean isModifiable()
          Returns if this validation result is modifiable or not.
 Iterator<ValidationMessage> iterator()
          Returns an iterator over the validation messages in this result in proper sequence.
 Map<Object,ValidationResult> keyMap()
          Creates and returns an unmodifiable Map that maps the message keys of this validation result to unmodifiable sub results that share the key.
 int size()
          Returns the number of messages in this result.
 ValidationResult subResult(int fromIndex, int toIndex)
          Returns an unmodifiable view of the portion of this result between the specified fromIndex, inclusive, and toIndex, exclusive.
 ValidationResult subResult(Object messageKey)
          Returns an unmodifiable sub result of this result that consists of all messages that share the specified message key.
 ValidationResult subResult(Object[] messageKeys)
          Returns an unmodifiable sub result of this result that consists of all messages that share the specified message keys.
 String toString()
          Returns a string representation intended for debugging purposes.
static ValidationResult unmodifiableResult(ValidationResult validationResult)
          Returns an unmodifiable view of the given ValidationResult.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

EMPTY

public static final ValidationResult EMPTY
A constant for an empty and unmodifiable validation result.

Constructor Detail

ValidationResult

public ValidationResult()
Constructs an empty modifiable ValidationResult.

Method Detail

unmodifiableResult

public static ValidationResult unmodifiableResult(ValidationResult validationResult)
Returns an unmodifiable view of the given ValidationResult. Useful to provide users with "read-only" access to internal results, or to indicate to other validation result processors that a result is not intended to be modified. Attempts to modify the returned validation result throw an UnsupportedOperationException.

Parameters:
validationResult - the result for which an unmodifiable view is to be returned
Returns:
an unmodifiable view of the specified validation result

add

public void add(ValidationMessage validationMessage)
Adds a new ValidationMessage to the list of messages.

Parameters:
validationMessage - the message to add
Throws:
NullPointerException - if the message is null
UnsupportedOperationException - if the result is unmodifiable
IllegalArgumentException - if the severity is OK
See Also:
addError(String), addWarning(String)

addError

public void addError(String text)
Creates and adds an error message to the list of validation messages using the given text.

Parameters:
text - the error text to add
Throws:
NullPointerException - if the message text null
UnsupportedOperationException - if the result is unmodifiable
See Also:
add(ValidationMessage), addWarning(String)

addError

public void addError(String text,
                     String key)
Creates and adds an error message to the list of validation messages using the given text and validation message key.

Parameters:
text - the error text to add
key - the optional messages key
Throws:
NullPointerException - if the message text null
UnsupportedOperationException - if the result is unmodifiable
Since:
2.3
See Also:
add(ValidationMessage), addWarning(String)

addWarning

public void addWarning(String text)
Creates and adds a warning message to the list of validation messages using the given text.

Parameters:
text - the warning text to add
Throws:
NullPointerException - if the message text null
UnsupportedOperationException - if the result is unmodifiable
See Also:
add(ValidationMessage), addError(String)

addWarning

public void addWarning(String text,
                       String key)
Creates and adds a warning message to the list of validation messages using the given text.

Parameters:
text - the warning text to add
key - the optional message key
Throws:
NullPointerException - if the message text null
UnsupportedOperationException - if the result is unmodifiable
Since:
2.3
See Also:
add(ValidationMessage), addError(String)

addAll

public void addAll(List<ValidationMessage> messages)
Adds all messages from the given list to this validation result.

Parameters:
messages - the messages to be added
Throws:
NullPointerException - if the messages list is null
UnsupportedOperationException - if the result is unmodifiable
IllegalArgumentException - if the messages list contains a message with severity OK
See Also:
addAllFrom(ValidationResult)

addAllFrom

public void addAllFrom(ValidationResult validationResult)
Adds all messages from the given ValidationResult to the list of messages that this validation result holds.

Parameters:
validationResult - the validation result to add messages from
Throws:
NullPointerException - if the validation result is null
UnsupportedOperationException - if the result is unmodifiable
See Also:
addAll(List)

isEmpty

public boolean isEmpty()
Checks and answers whether this validation result contains no messages.

Returns:
true if this validation result contains no messages
See Also:
hasErrors(), hasWarnings()

size

public int size()
Returns the number of messages in this result.

Returns:
the number of elements in this list

contains

public boolean contains(ValidationMessage message)
Checks and answers whether this result contains the specified message. More formally, returns true if and only if this result contains at least one message m such that (message.equals(m)).

Parameters:
message - message whose presence in this result is to be tested
Returns:
true if this result contains the specified message
Throws:
NullPointerException - if the specified message is null

get

public ValidationMessage get(int index)
Returns the message at the specified position in this result.

Parameters:
index - index of the message to return.
Returns:
the ValidationMessage at the specified position in this result's underlying message list.
Throws:
IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size()).
Since:
2.1.0

iterator

public Iterator<ValidationMessage> iterator()
Returns an iterator over the validation messages in this result in proper sequence.

Specified by:
iterator in interface Iterable<ValidationMessage>
Returns:
an iterator over the ValidationMessages in this result in proper sequence.
Since:
2.1.0

subResult

public ValidationResult subResult(int fromIndex,
                                  int toIndex)
Returns an unmodifiable view of the portion of this result between the specified fromIndex, inclusive, and toIndex, exclusive. (If fromIndex and toIndex are equal, the returned result is empty.) The returned result is a copy, so changes in the returned result won't affect this result, and vice-versa.

Parameters:
fromIndex - low end point (inclusive) of the subResult
toIndex - high end point (exclusive) of the subResult
Returns:
a view of the specified range within this result.
Throws:
IndexOutOfBoundsException - for an illegal end point index value (fromIndex < 0 || toIndex > size || fromIndex > toIndex).
See Also:
subResult(Object)

subResult

public ValidationResult subResult(Object messageKey)
Returns an unmodifiable sub result of this result that consists of all messages that share the specified message key. If the specified key is null, this method returns an empty result. The returned result is a copy, so changes in this result won't affect it.

Parameters:
messageKey - the key to look for, can be null
Returns:
a sub result containing all messages that share the specified key, or the empty result if the key is null
See Also:
subResult(int, int)

subResult

public ValidationResult subResult(Object[] messageKeys)
Returns an unmodifiable sub result of this result that consists of all messages that share the specified message keys. If the array of keys is null, this method returns an empty result. The returned result is a copy, so changes in this result won't affect it.

Parameters:
messageKeys - the keys to look for, can be null
Returns:
a sub result containing all messages that share the specified keys, or the empty result if the key array is null
Since:
1.4
See Also:
subResult(int, int)

keyMap

public Map<Object,ValidationResult> keyMap()
Creates and returns an unmodifiable Map that maps the message keys of this validation result to unmodifiable sub results that share the key.

More formally: for each key key in the created map map, map.get(key) returns a ValidationResult result, such that for each ValidationMessage message in result we have: message.key().equals(key).

Returns:
a mapping from message key to an associated validation result that consist only of messages with this key
See Also:
ValidationMessage.key()

getSeverity

public Severity getSeverity()
Returns the highest severity of this result's messages, Severity.OK if there are no messages.

Returns:
the highest severity of this result's messages, Severity.OK if there are no messages
See Also:
hasMessages(), hasErrors(), hasWarnings()

hasMessages

public boolean hasMessages()
Checks and answers whether this validation result has messages or not.

Returns:
true if there are messages, false if not
See Also:
getSeverity(), hasErrors(), hasWarnings()

hasErrors

public boolean hasErrors()
Checks and answers whether this validation result contains a message of type ERROR.

Returns:
true if there are error messages, false if not
See Also:
getSeverity(), hasMessages(), hasWarnings()

hasWarnings

public boolean hasWarnings()
Checks and answers whether this validation result contains a message of type WARNING.

Note that this method checks for warning messages only. It'll return false, if there are errors but no warnings. If you want to test whether this result contains warning and/or errors, use #hasMessages instead.

Returns:
true if there are warnings, false if not
See Also:
getSeverity(), hasMessages(), hasErrors()

getMessages

public List<ValidationMessage> getMessages()
Returns an unmodifiable List of all validation messages.

Returns:
the List of all validation messages
See Also:
getErrors(), getWarnings()

getErrors

public List<ValidationMessage> getErrors()
Returns an unmodifiable List of the validation messages that indicate errors.

Returns:
the List of error validation messages
See Also:
getMessages(), getWarnings()

getWarnings

public List<ValidationMessage> getWarnings()
Returns an unmodifiable List of the validation messages that indicate warnings.

Returns:
the List of validation warnings
See Also:
getMessages(), getErrors()

isModifiable

public boolean isModifiable()
Returns if this validation result is modifiable or not. Can be used to cache data from unmodifiable result.

Returns:
true if modifiable, false if unmodifiable

getMessagesText

public String getMessagesText()
Returns a string representation of the message list.

Returns:
a string representation of the message list

toString

public String toString()
Returns a string representation intended for debugging purposes.

Overrides:
toString in class Object
Returns:
a string representation intended for debugging
See Also:
Object.toString()

equals

public boolean equals(Object o)
Compares the specified object with this validation result for equality. Returns true if and only if the specified object is also a validation result, both results have the same size, and all corresponding pairs of validation messages in the two validation results are equal. (Two validation messages m1 and m2 are equal if (m1==null ? m2==null : m1.equals(m2)).) In other words, two validation results are defined to be equal if and only if they contain the same validation messages in the same order.

This implementation first checks if the specified object is this validation result. If so, it returns true; if not, it checks if the specified object is a validation result. If not, it returns false; if so, it checks and returns if the lists of messages in both results are equal.

Overrides:
equals in class Object
Parameters:
o - the object to be compared for equality with this validation result.
Returns:
true if the specified object is equal to this validation result.
See Also:
List.equals(java.lang.Object), Object.equals(java.lang.Object)

hashCode

public int hashCode()
Returns the hash code value for this validation result. This implementation returns the hash from the List of messages.

Overrides:
hashCode in class Object
Returns:
the hash code value for this validation result.
See Also:
List.hashCode(), Object.hashCode()


Copyright © 2003-2011 JGoodies Karsten Lentzsch. All Rights Reserved.