com.jgoodies.validation.ValidationResult Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jgoodies-validation Show documentation
Show all versions of jgoodies-validation Show documentation
The JGoodies Validation helps you validate user input
in Swing apps and report validation errors and warnings.
It has been designed to work with different architectures and
programming flavors.
The newest version!
/*
* Copyright (c) 2003-2014 JGoodies Software GmbH. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* o Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* o Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* o Neither the name of JGoodies Software GmbH nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.jgoodies.validation;
import static com.jgoodies.common.base.Preconditions.checkArgument;
import static com.jgoodies.common.base.Preconditions.checkNotNull;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import com.jgoodies.common.base.Strings;
import com.jgoodies.validation.message.SimpleValidationMessage;
/**
* 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.
*
* @author Karsten Lentzsch
* @version $Revision: 1.24 $
*
* @see ValidationMessage
* @see Validator
*/
public final class ValidationResult implements Iterable,
Serializable {
/**
* A constant for an empty and unmodifiable validation result.
*/
public static final ValidationResult EMPTY =
new ValidationResult(Collections.emptyList(), false);
/**
* Holds a List of ValidationMessages.
*/
private final List messageList;
/**
* Describes if this result can be modified or not.
*/
private final boolean modifiable;
// Instance Creation ******************************************************
/**
* Constructs an empty modifiable ValidationResult.
*/
public ValidationResult() {
this(new ArrayList(), true);
}
/**
* Constructs a ValidationResult on the given message list.
* Used for constructing the {@code EMPTY} validation result.
*
* @param messageList an initial message list
* @param modifiable true to allow modifications, false to prevent them
*/
private ValidationResult(List messageList, boolean modifiable) {
this.messageList = messageList;
this.modifiable = modifiable;
}
/**
* 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 {@code UnsupportedOperationException}.
*
* @param validationResult the result for which an unmodifiable view is to be returned
* @return an unmodifiable view of the specified validation result
*/
public static ValidationResult unmodifiableResult(ValidationResult validationResult) {
return validationResult.modifiable
? new ValidationResult(new ArrayList(validationResult.messageList),
false)
: validationResult;
}
// Adding Messages ********************************************************
/**
* Creates and adds an error message to the list of validation messages
* using the given text.
*
* @param text the error text to add
*
* @throws NullPointerException if the message text {@code null}
* @throws UnsupportedOperationException if the result is unmodifiable
*
* @see #add(ValidationMessage)
* @see #addWarning(String)
*/
public ValidationResult addError(String text) {
return addError((Object) null, text);
}
/**
* Creates and adds an error message to the list of validation messages
* using the given text and validation message key.
*
* @param text the error text to add
* @param key the optional messages key
*
* @throws NullPointerException if the message text is {@code null}
* @throws UnsupportedOperationException if the result is unmodifiable
*
* @see #add(ValidationMessage)
* @see #addWarning(String)
*
* @since 2.3
*/
public ValidationResult addError(String text, Object key) {
return addError(key, text);
}
/**
* Creates and adds an error message to the list of validation messages
* using the given validation message key, (format) text and optional
* format arguments.
*
* @param key the key used to associate messages and components
* @param text the format text to add
* @param args optional text format arguments
*
*
* @throws NullPointerException if the message text is {@code null}
* @throws IllegalArgumentException if the message text is empty or whitespace
* @throws UnsupportedOperationException if the result is unmodifiable
*
* @see #add(ValidationMessage)
* @see #addWarning(String)
*
* @since 2.6
*/
private ValidationResult addError(Object key, String text, Object... args) {
checkModifiable();
add(new SimpleValidationMessage(Strings.get(text, args), Severity.ERROR, key));
return this;
}
/**
* Creates and adds a warning message to the list of validation messages
* using the given text.
*
* @param text the warning text to add
*
* @throws NullPointerException if the message text {@code null}
* @throws UnsupportedOperationException if the result is unmodifiable
*
* @see #add(ValidationMessage)
* @see #addError(String)
*/
public ValidationResult addWarning(String text) {
return addWarning((Object) null, text);
}
/**
* Creates and adds a warning message to the list of validation messages
* using the given text.
*
* @param text the warning text to add
* @param key the optional message key
*
* @throws NullPointerException if the message text {@code null}
* @throws UnsupportedOperationException if the result is unmodifiable
*
* @see #add(ValidationMessage)
* @see #addError(String)
*
* @since 2.3
*/
public ValidationResult addWarning(String text, Object key) {
return addWarning(key, text);
}
/**
* Creates and adds a warning message to the list of validation messages
* using the given validation message key, (format) text and optional
* format arguments.
*
* @param key the key used to associate messages and components
* @param text the format text to add
* @param args optional text format arguments
*
*
* @throws NullPointerException if the message text is {@code null}
* @throws IllegalArgumentException if the message text is empty or whitespace
* @throws UnsupportedOperationException if the result is unmodifiable
*
* @see #add(ValidationMessage)
* @see #addError(String)
*
* @since 2.6
*/
private ValidationResult addWarning(Object key, String text, Object... args) {
checkModifiable();
add(new SimpleValidationMessage(Strings.get(text, args), Severity.WARNING, key));
return this;
}
/**
* Creates and adds an info message to the list of validation messages
* using the given text.
*
* @param text the info text to add
*
* @throws NullPointerException if the message text {@code null}
* @throws UnsupportedOperationException if the result is unmodifiable
*
* @see #add(ValidationMessage)
*/
public ValidationResult addInfo(String text) {
return addInfo((Object) null, text);
}
/**
* Creates and adds an info message to the list of validation messages
* using the given text and key.
*
* @param text the info text to add
* @param key the optional message key
*
* @throws NullPointerException if the message text {@code null}
* @throws UnsupportedOperationException if the result is unmodifiable
*
* @see #add(ValidationMessage)
*/
public ValidationResult addInfo(String text, Object key) {
return addInfo(key, text);
}
/**
* Creates and adds an info message to the list of validation messages
* using the given validation message key, (format) text and optional
* format arguments.
*
* @param key the key used to associate messages and components
* @param text the format text to add
* @param args optional text format arguments
*
*
* @throws NullPointerException if the message text is {@code null}
* @throws IllegalArgumentException if the message text is empty or whitespace
* @throws UnsupportedOperationException if the result is unmodifiable
*
* @see #add(ValidationMessage)
* @see #addError(String)
*
* @since 2.6
*/
private ValidationResult addInfo(Object key, String text, Object... args) {
checkModifiable();
add(new SimpleValidationMessage(Strings.get(text, args), Severity.INFO, key));
return this;
}
// Adding Messages ********************************************************
/**
* Adds a new ValidationMessage to the list of messages.
*
* @param validationMessage the message to add
*
* @return this validation result
*
* @throws NullPointerException if the message is {@code null}
* @throws UnsupportedOperationException if the result is unmodifiable
* @throws IllegalArgumentException if the severity is {@code OK}
*
* @see #addError(String)
* @see #addWarning(String)
*/
public ValidationResult add(ValidationMessage validationMessage) {
checkModifiable();
checkNotNull(validationMessage, "The validation message must not be null.");
checkArgument(validationMessage.severity() != Severity.OK,
"You must not add a validation message with severity OK.");
messageList.add(validationMessage);
return this;
}
/**
* Adds all messages from the given list to this validation result.
*
* @param messages the messages to be added
*
* @throws NullPointerException if the messages list is {@code null}
* @throws UnsupportedOperationException if the result is unmodifiable
* @throws IllegalArgumentException if the messages list contains
* a message with severity {@code OK}
*
* @see #addAllFrom(ValidationResult)
*/
public void addAll(List messages) {
checkModifiable();
checkNotNull(messages, "The messages list must not be null.");
for (ValidationMessage message : messages) {
checkArgument(message.severity() != Severity.OK,
"You must not add a validation message with severity OK.");
}
messageList.addAll(messages);
}
/**
* Adds all messages from the given ValidationResult
* to the list of messages that this validation result holds.
*
* @param validationResult the validation result to add messages from
*
* @throws NullPointerException if the validation result is {@code null}
* @throws UnsupportedOperationException if the result is unmodifiable
*
* @see #addAll(List)
*/
public void addAllFrom(ValidationResult validationResult) {
checkModifiable();
checkNotNull(validationResult, "The validation result to add must not be null.");
addAll(validationResult.messageList);
}
// List Operations ********************************************************
/**
* Checks and answers whether this validation result contains no messages.
*
* @return true if this validation result contains no messages
*
* @see #hasErrors()
* @see #hasWarnings()
*/
public boolean isEmpty() {
return messageList.isEmpty();
}
/**
* Returns the number of messages in this result.
*
* @return the number of elements in this list
*/
public int size() {
return messageList.size();
}
/**
* Checks and answers whether this result contains the specified message.
* More formally, returns {@code true} if and only if this result
* contains at least one message {@code m} such that
* {@code (message.equals(m))}.
*
* @param message message whose presence in this result is to be tested
* @return {@code true} if this result contains the specified message
* @throws NullPointerException if the specified message is
* {@code null}
*/
public boolean contains(ValidationMessage message) {
return messageList.contains(message);
}
/**
* Returns the message at the specified position in this result.
*
* @param index index of the message to return.
* @return 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
*/
public ValidationMessage get(int index) {
return messageList.get(index);
}
/**
* Returns an iterator over the validation messages in this result
* in proper sequence.
*
* @return an iterator over the ValidationMessages in this result
* in proper sequence.
*
* @since 2.1.0
*/
@Override
public Iterator iterator() {
return messageList.iterator();
}
/**
* Returns an unmodifiable view of the portion of this result between
* the specified {@code fromIndex}, inclusive, and {@code toIndex},
* exclusive.
* (If {@code fromIndex} and {@code 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.
*
* @param fromIndex low end point (inclusive) of the subResult
* @param toIndex high end point (exclusive) of the subResult
* @return 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 #subResult(Object)
*/
public ValidationResult subResult(int fromIndex, int toIndex) {
List messages = messageList.subList(fromIndex, toIndex);
return new ValidationResult(messages, false);
}
/**
* Returns an unmodifiable sub result of this result that consists of
* all messages that share the specified message key. If the specified key
* is {@code null}, this method returns an empty result.
* The returned result is a copy, so changes in this result won't affect it.
*
* @param messageKey the key to look for, can be {@code null}
* @return a sub result containing all messages that share the
* specified key, or the empty result if the key is {@code null}
*
* @see #subResult(int, int)
*/
public ValidationResult subResult(Object messageKey) {
if (messageKey == null) {
return EMPTY;
}
List messages = new ArrayList();
for (ValidationMessage message : messageList) {
if (messageKey.equals(message.key())) {
messages.add(message);
}
}
return new ValidationResult(messages, false);
}
/**
* 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 {@code null}, this method returns an empty result.
* The returned result is a copy, so changes in this result won't affect it.
*
* @param messageKeys the keys to look for, can be {@code null}
* @return a sub result containing all messages that share the specified
* keys, or the empty result if the key array is {@code null}
*
* @see #subResult(int, int)
*
* @since 1.4
*/
public ValidationResult subResult(Object[] messageKeys) {
if (messageKeys == null) {
return EMPTY;
}
List messages = new ArrayList();
for (ValidationMessage message : messageList) {
Object messageKey = message.key();
for (Object key : messageKeys) {
if (messageKey.equals(key)) {
messages.add(message);
}
}
}
return new ValidationResult(messages, false);
}
/**
* 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 {@code key} in the created map {@code map},
* {@code map.get(key)} returns a {@code ValidationResult}
* {@code result}, such that for each {@code ValidationMessage}
* {@code message} in {@code result} we have:
* {@code message.key().equals(key)}.
*
* @return a mapping from message key to an associated validation result
* that consist only of messages with this key
*
* @see ValidationMessage#key()
*/
public Map