001: /*
002: * Copyright (c) 2003-2007 JGoodies Karsten Lentzsch. All Rights Reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of JGoodies Karsten Lentzsch nor the names of
015: * its contributors may be used to endorse or promote products derived
016: * from this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
020: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
021: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
022: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
025: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
026: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
027: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
028: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030:
031: package com.jgoodies.validation.message;
032:
033: import com.jgoodies.validation.Severity;
034: import com.jgoodies.validation.ValidationMessage;
035: import com.jgoodies.validation.util.ValidationUtils;
036:
037: /**
038: * An implementation of {@link ValidationMessage} that just holds a text.
039: * It is the minimal validation message, not intended to be subclassed.
040: *
041: * @author Karsten Lentzsch
042: * @version $Revision: 1.7 $
043: */
044: public final class SimpleValidationMessage extends
045: AbstractValidationMessage {
046:
047: // Instance Creation ******************************************************
048:
049: /**
050: * Constructs a simple warning message for the given text.
051: *
052: * @param text a String that describes this warning
053: *
054: * @throws NullPointerException if the text is <code>null</code>.
055: */
056: public SimpleValidationMessage(String text) {
057: this (text, Severity.WARNING);
058: }
059:
060: /**
061: * Constructs a simple validation message for the given text
062: * and message severity.
063: *
064: * @param text describes this message
065: * @param severity the message severity, either error or warning
066: *
067: * @throws IllegalArgumentException if severity is <code>Severity.OK</code>
068: * @throws NullPointerException if the text is <code>null</code>.
069: */
070: public SimpleValidationMessage(String text, Severity severity) {
071: this (text, severity, null);
072: }
073:
074: /**
075: * Constructs a simple validation message for the given text,
076: * message severity, and message key.
077: *
078: * @param text describes this message
079: * @param severity the message severity, either error or warning
080: * @param key the message's key
081: *
082: * @throws IllegalArgumentException if severity is <code>Severity.OK</code>
083: * @throws NullPointerException if the text is <code>null</code>.
084: */
085: public SimpleValidationMessage(String text, Severity severity,
086: Object key) {
087: super (text, severity, key);
088: if (text == null) {
089: throw new NullPointerException("The text must not be null");
090: }
091: }
092:
093: // Comparison and Hashing *************************************************
094:
095: /**
096: * Compares the specified object with this validation message for equality.
097: * Returns <code>true</code> if and only if the specified object is also
098: * a simple validation message, both messages have the same severity,
099: * key, and formatted text. In other words, two simple validation messages
100: * are defined to be equal if and only if they behave one like the other.<p>
101: *
102: * This implementation first checks if the specified object is this
103: * a simple validation message. If so, it returns <code>true</code>;
104: * if not, it checks if the specified object is a simple validation message.
105: * If not, it returns <code>false</code>; if so, it checks and returns
106: * if the severities, keys and formatted texts of both messages are equal.
107: *
108: * @param o the object to be compared for equality with this validation message.
109: *
110: * @return <code>true</code> if the specified object is equal
111: * to this validation message.
112: *
113: * @see Object#equals(java.lang.Object)
114: */
115: @Override
116: public boolean equals(Object o) {
117: if (o == this ) {
118: return true;
119: }
120: if (!(o instanceof SimpleValidationMessage)) {
121: return false;
122: }
123: SimpleValidationMessage other = (SimpleValidationMessage) o;
124: return severity().equals(other.severity())
125: && ValidationUtils.equals(key(), other.key())
126: && ValidationUtils.equals(formattedText(), other
127: .formattedText());
128: }
129:
130: /**
131: * Returns the hash code value for this validation message.
132: * This implementation computes and returns the hash based
133: * on the hash code values of this messages' severity, key,
134: * and text.
135: *
136: * @return the hash code value for this validation message.
137: *
138: * @see Object#hashCode()
139: */
140: @Override
141: public int hashCode() {
142: String formattedText = formattedText();
143: int result = 17;
144: result = 37 * result + severity().hashCode();
145: result = 37 * result + (key() == null ? 0 : key().hashCode());
146: result = 37
147: * result
148: + (formattedText == null ? 0 : formattedText.hashCode());
149: return result;
150: }
151:
152: }
|