001 package org.apache.fulcrum.intake.validator;
002
003 /*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements. See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership. The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License. You may obtain a copy of the License at
011 *
012 * http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied. See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022 import java.util.Map;
023
024 import org.apache.commons.lang.StringUtils;
025
026 /**
027 * Validates Longs with the following constraints in addition to those
028 * listed in NumberValidator and DefaultValidator.
029 *
030 * <table>
031 * <tr><th>Name</th><th>Valid Values</th><th>Default Value</th></tr>
032 * <tr><td>minValue</td><td>greater than Long.MIN_VALUE</td>
033 * <td> </td></tr>
034 * <tr><td>maxValue</td><td>less than Long.MAX_VALUE</td>
035 * <td> </td></tr>
036 * <tr><td>invalidNumberMessage</td><td>Some text</td>
037 * <td>Entry was not a valid number</td></tr>
038 * </table>
039 *
040 * @author <a href="mailto:jmcnally@collab.net">John McNally</a>
041 * @author <a href="mailto:Colin.Chalmers@maxware.nl">Colin Chalmers</a>
042 * @author <a href="mailto:jh@byteaction.de">Jürgen Hoffmann</a>
043 * @version $Id: LongValidator.java 812827 2009-09-09 08:46:27Z tv $
044 */
045 public class LongValidator
046 extends NumberValidator
047 {
048 /* Init the minValue to that for a Long */
049 private long minValue = Long.MIN_VALUE;
050
051 /* Init the maxValue to that for a Long */
052 private long maxValue = Long.MAX_VALUE;
053
054 /**
055 * Constructor to use when initialising Object
056 *
057 * @param paramMap
058 * @throws InvalidMaskException
059 */
060 public LongValidator(Map paramMap)
061 throws InvalidMaskException
062 {
063 this();
064 init(paramMap);
065 }
066
067 /**
068 * Default Constructor
069 */
070 public LongValidator()
071 {
072 invalidNumberMessage = "Entry was not a valid Long";
073 }
074
075 /**
076 * Method to initialise Object
077 *
078 * @param paramMap
079 * @throws InvalidMaskException
080 */
081 public void init(Map paramMap)
082 throws InvalidMaskException
083 {
084 super.init(paramMap);
085
086 Constraint constraint = (Constraint) paramMap.get(MIN_VALUE_RULE_NAME);
087 if (constraint != null)
088 {
089 String param = constraint.getValue();
090 minValue = Long.parseLong(param);
091 minValueMessage = constraint.getMessage();
092 }
093
094 constraint = (Constraint) paramMap.get(MAX_VALUE_RULE_NAME);
095 if (constraint != null)
096 {
097 String param = constraint.getValue();
098 maxValue = Long.parseLong(param);
099 maxValueMessage = constraint.getMessage();
100 }
101 }
102
103 /**
104 * Determine whether a testValue meets the criteria specified
105 * in the constraints defined for this validator
106 *
107 * @param testValue a <code>String</code> to be tested
108 * @exception ValidationException containing an error message if the
109 * testValue did not pass the validation tests.
110 */
111 public void assertValidity(String testValue)
112 throws ValidationException
113 {
114 super.assertValidity(testValue);
115
116 if (required || StringUtils.isNotEmpty(testValue))
117 {
118 long l = 0L;
119 try
120 {
121 l = Long.parseLong(testValue);
122 }
123 catch (RuntimeException e)
124 {
125 errorMessage = invalidNumberMessage;
126 throw new ValidationException(invalidNumberMessage);
127 }
128
129 if (l < minValue)
130 {
131 errorMessage = minValueMessage;
132 throw new ValidationException(minValueMessage);
133 }
134 if (l > maxValue)
135 {
136 errorMessage = maxValueMessage;
137 throw new ValidationException(maxValueMessage);
138 }
139 }
140 }
141
142
143 // ************************************************************
144 // ** Bean accessor methods **
145 // ************************************************************
146
147 /**
148 * Get the value of minValue.
149 *
150 * @return value of minValue.
151 */
152 public long getMinValue()
153 {
154 return minValue;
155 }
156
157 /**
158 * Set the value of minValue.
159 *
160 * @param minValue Value to assign to minValue.
161 */
162 public void setMinValue(long minValue)
163 {
164 this.minValue = minValue;
165 }
166
167 /**
168 * Get the value of maxValue.
169 *
170 * @return value of maxValue.
171 */
172 public long getMaxValue()
173 {
174 return maxValue;
175 }
176
177 /**
178 * Set the value of maxValue.
179 *
180 * @param maxValue Value to assign to maxValue.
181 */
182 public void setMaxValue(long maxValue)
183 {
184 this.maxValue = maxValue;
185 }
186 }