KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > util > validators > IntegerValidator


1 /* ===============================================================================
2  *
3  * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4  *
5  * ===============================================================================
6  *
7  * Copyright (C)
8  *
9  * This program is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License version 2, as published by the
11  * Free Software Foundation. See the file LICENSE.html for more information.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20  *
21  * ===============================================================================
22  */

23
24 package org.infoglue.cms.util.validators;
25
26 import org.infoglue.cms.exception.ConstraintException;
27 import org.infoglue.cms.util.ConstraintExceptionBuffer;
28
29 /**
30  * <todo>
31  * Time is running out, the illusion fades away...
32  * This package will be refactored/extended after iteration 1.
33  * - move to com.holigrow.yoda.util.validators ?
34  * - interfaces + factory
35  * - constructor madness (setXXX instead of N constructors?)
36  * - more validators
37  * - more fun
38  * </todo>
39  *
40  * @@author <a HREF="meat_for_the_butcher@@yahoo.com">Patrik Nyborg</a>
41  */

42 public class IntegerValidator extends AbstractValidator
43 {
44   // --- [Constants] -----------------------------------------------------------
45

46   // error codes
47
private static final String JavaDoc ILLEGAL_VALUE_ERROR_CODE = "306";
48
49
50
51   // --- [Attributes] ----------------------------------------------------------
52

53   //
54
private Range valueSpace;
55
56   
57
58   // --- [Static] --------------------------------------------------------------
59
// --- [Constructors] --------------------------------------------------------
60

61   /**
62    *
63    */

64   IntegerValidator(String JavaDoc fieldName, boolean isRequired, int lowerLimit, int upperLimit) {
65     super(fieldName, isRequired);
66     this.valueSpace = new Range(lowerLimit, upperLimit);
67   }
68
69
70
71   // --- [Public] --------------------------------------------------------------
72

73   /**
74    *
75    */

76   public void validate(Integer JavaDoc value) throws ConstraintException {
77     validateIsRequired(value);
78     if(value == null) { // no validation needed + no need for further null checking
79
return;
80     }
81     validateValueSpace(value);
82     failIfAnyExceptionsFound();
83   }
84
85   /**
86    */

87   public void validate(Integer JavaDoc value, ConstraintExceptionBuffer ceb) {
88     try {
89       validate(value);
90     } catch(ConstraintException e) {
91       ceb.add(e);
92     }
93   }
94
95   // --- [X implementation] ----------------------------------------------------
96
// --- [X Overrides] ---------------------------------------------------------
97
// --- [Package protected] ---------------------------------------------------
98
// --- [Private] -------------------------------------------------------------
99

100   /**
101    *
102    */

103   private void validateValueSpace(Integer JavaDoc value) {
104     if(!this.valueSpace.isWithinLimits(value.intValue())) {
105       addConstraintException(ILLEGAL_VALUE_ERROR_CODE);
106     }
107   }
108
109
110
111   // --- [Protected] -----------------------------------------------------------
112
// --- [Inner classes] -------------------------------------------------------
113
}
Popular Tags