KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > applications > workflowtool > util > RangeCheck


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 package org.infoglue.cms.applications.workflowtool.util;
24
25 /**
26  * Utility class for checking if an integer is within a (possible open-ended) range.
27  */

28 public class RangeCheck
29 {
30     /**
31      * Return code if the checked value is inside the range.
32      */

33     public static final int OK = 0;
34     
35     /**
36      * Return code if the checked value is outside the range and the range is [x,x].
37      */

38     public static final int EXACTLY = 1;
39     
40     /**
41      * Return code if the checked value is outside the range and the range is [1,1].
42      */

43     public static final int EXACTLY_ONE = 2;
44     
45     /**
46      * Return code if the checked value is outside the range and the range is ],x].
47      */

48     public static final int LESS_THAN = 3;
49     
50     /**
51      * Return code if the checked value is outside the range and the range is [x,[.
52      */

53     public static final int GREATER_THAN = 4;
54
55     /**
56      * Return code if the checked value is outside the range and the range is [1,[.
57      */

58     public static final int GREATER_THAN_ONE = 5;
59     
60     /**
61      * Return code if the checked value is outside the range and the range is [x,y].
62      */

63     public static final int BETWEEN = 6;
64     
65     /**
66      * Return code if the checked value is outside the range and the range is [1,y].
67      */

68     public static final int BETWEEN_ONE_AND_MANY = 7;
69     
70     /**
71      * The lower limit (a null value indicates an open end).
72      */

73     private final Integer JavaDoc min;
74     
75     /**
76      * The upper limit (a null value indicates an open end).
77      */

78     private final Integer JavaDoc max;
79     
80     /**
81      * Constructs an object with the specified limits.
82      *
83      * @param min the lower limit; a null value indicates an open end.
84      * @param min the upper limit; a null value indicates an open end.
85      */

86     public RangeCheck(final Integer JavaDoc min, final Integer JavaDoc max)
87     {
88         this.min = min;
89         this.max = max;
90     }
91     
92     /**
93      * Returns the lower limit or null if open-ended.
94      *
95      * @return the lower limit or null if open-ended.
96      */

97     public final Integer JavaDoc getMin()
98     {
99         return min;
100     }
101     
102     /**
103      * Returns the upper limit or null if open-ended.
104      *
105      * @return the upper limit or null if open-ended.
106      */

107     public final Integer JavaDoc getMax()
108     {
109         return max;
110     }
111     
112     /**
113      * Checks if the specified value is within the range.
114      *
115      * @param value the value to check.
116      * @return a code representing the result.
117      */

118     public final int check(final int value)
119     {
120         boolean one = (min != null && min.intValue() == 1);
121         if(min == null && max == null)
122         {
123             return OK;
124         }
125         if(min == null)
126         {
127             return (value > max.intValue()) ? LESS_THAN : OK;
128         }
129         if(max == null)
130         {
131             return (value < min.intValue()) ? (one ? GREATER_THAN_ONE : GREATER_THAN) : OK;
132         }
133         if(min.equals(max) && value != min.intValue())
134         {
135             return one ? EXACTLY_ONE : EXACTLY;
136         }
137         if(value < min.intValue() || value > max.intValue())
138         {
139             return one ? BETWEEN_ONE_AND_MANY : BETWEEN;
140         }
141         return OK;
142     }
143 }
144
Popular Tags