001: /* *************************************************************************
002:
003: Millstone(TM)
004: Open Sourced User Interface Library for
005: Internet Development with Java
006:
007: Millstone is a registered trademark of IT Mill Ltd
008: Copyright (C) 2000-2005 IT Mill Ltd
009:
010: *************************************************************************
011:
012: This library is free software; you can redistribute it and/or
013: modify it under the terms of the GNU Lesser General Public
014: license version 2.1 as published by the Free Software Foundation.
015:
016: This library is distributed in the hope that it will be useful,
017: but WITHOUT ANY WARRANTY; without even the implied warranty of
018: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: Lesser General Public License for more details.
020:
021: You should have received a copy of the GNU Lesser General Public
022: License along with this library; if not, write to the Free Software
023: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024:
025: *************************************************************************
026:
027: For more information, contact:
028:
029: IT Mill Ltd phone: +358 2 4802 7180
030: Ruukinkatu 2-4 fax: +358 2 4802 7181
031: 20540, Turku email: info@itmill.com
032: Finland company www: www.itmill.com
033:
034: Primary source for MillStone information and releases: www.millstone.org
035:
036: ********************************************************************** */
037:
038: package org.millstone.base.data.validator;
039:
040: import org.millstone.base.data.*;
041:
042: /* This validator is used for validating properties that
043: * do or do not allow null values.
044: * @author IT Mill Ltd.
045: * @version 3.1.1
046: * @since 3.0
047: */
048: public class NullValidator implements Validator {
049:
050: private boolean allowNull;
051: private String errorMessage;
052:
053: /** Create a new NullValidator
054: * @param errorMessage - The error message to display on invalidation.
055: * @param allowNull - Are nulls allowed?
056: */
057: public NullValidator(String errorMessage, boolean allowNull) {
058: setErrorMessage(errorMessage);
059: setNullAllowed(allowNull);
060: }
061:
062: /** Validate the data given in value.
063: * @param value - The value to validate.
064: * @throws Validator.InvalidValueException - The value was invalid.
065: */
066: public void validate(Object value)
067: throws Validator.InvalidValueException {
068: if ((allowNull && value != null)
069: || (!allowNull && value == null))
070: throw new Validator.InvalidValueException(errorMessage);
071: }
072:
073: /** True of the value is valid.
074: * @param value - The value to validate.
075: */
076: public boolean isValid(Object value) {
077: return allowNull ? value == null : value != null;
078: }
079:
080: /** True if nulls are allowed.
081: */
082: public final boolean isNullAllowed() {
083: return allowNull;
084: }
085:
086: /** Sets if nulls are to be allowed.
087: * @param allowNull - Do we allow nulls?
088: */
089: public void setNullAllowed(boolean allowNull) {
090: this .allowNull = allowNull;
091: }
092:
093: /** Get the error message that is displayed in case the
094: * value is invalid.
095: */
096: public String getErrorMessage() {
097: return errorMessage;
098: }
099:
100: /** Set the error message to be displayed on invalid
101: * value.
102: */
103: public void setErrorMessage(String errorMessage) {
104: this.errorMessage = errorMessage;
105: }
106:
107: }
|