KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > idaremedia > antx > condition > IsNumeric


1 /**
2  * $Id: IsNumeric.java 180 2007-03-15 12:56:38Z ssmc $
3  * Copyright 2002-2003 iDare Media, Inc. All rights reserved.
4  *
5  * Originally written by iDare Media, Inc. for release into the public domain. This
6  * library, source form and binary form, is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public License as published by the
8  * Free Software Foundation; either version 2.1 of the License, or (at your option) any
9  * later version.<p>
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU LGPL (GNU Lesser General Public License) for more details.<p>
14  *
15  * You should have received a copy of the GNU Lesser General Public License along with this
16  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite
17  * 330, Boston, MA 02111-1307 USA. The LGPL can be found online at
18  * http://www.fsf.org/copyleft/lesser.html<p>
19  *
20  * This product has been influenced by several projects within the open-source community.
21  * The JWare developers wish to acknowledge the open-source community's support. For more
22  * information regarding the open-source products used within JWare, please visit the
23  * JWare website.
24  *----------------------------------------------------------------------------------------*
25  * WEBSITE- http://www.jware.info EMAIL- inquiries@jware.info
26  *----------------------------------------------------------------------------------------*
27  **/

28
29 package com.idaremedia.antx.condition;
30
31 import com.idaremedia.antx.helpers.Tk;
32
33 /**
34  * Simple Is-Numeric condition check. IsNumeric can handle any number up to
35  * Long.MAX_VALUE which means it can be used for timestamps as well as simplier (smaller)
36  * numbers.
37  * <p>
38  * <b>Examples:</b><pre>
39  * &lt;isnumeric value="${build.number}"/&gt;
40  * &lt;isnumeric property="build.ITID"/&gt;
41  *
42  * &lt;isnumeric variable="_loop.count"/&gt;
43  * &lt;isnumeric variable="_try.count" gte="0" lt="3"/&gt;
44  *
45  * &lt;isnumeric variable="_totaltime" gte="0"/&gt;
46  *</pre>
47  *
48  * @since JWare/AntX 0.1
49  * @author ssmc, &copy;2002-2003 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
50  * @version 0.5
51  * @.safety single
52  * @.group api,infra
53  **/

54
55 public class IsNumeric extends SimpleFlexCondition
56 {
57     /**
58      * Initializes a new IsNumeric condition.
59      **/

60     public IsNumeric()
61     {
62         super();
63     }
64
65
66     /**
67      * Creates defined condition.
68      * @param value the value against which condition checks
69      **/

70     public IsNumeric(String JavaDoc value)
71     {
72         setValue(value);
73     }
74
75
76     /**
77      * Sets this condition to evaluate a literal value as-is.
78      * @param value the literal value to check
79      **/

80     public void setValue(String JavaDoc value)
81     {
82         require_(value!=null,"setValu- nonzro");
83         setLiteral(value);
84     }
85
86
87     /**
88      * Sets a "greater-than" limit requirement on checked
89      * value.
90      **/

91     public void setGT(long gt)
92     {
93         m_GT = gt;
94         m_isLimited = true;
95     }
96
97
98     /**
99      * Sets a "greater-than-or-equal" limit requirement on
100      * checked value.
101      **/

102     public void setGTE(long gte)
103     {
104         m_GTE = gte;
105         m_isLimited = true;
106     }
107
108
109     /**
110      * Sets a "less-than" limit requirement on checked
111      * value.
112      **/

113     public void setLT(long lt)
114     {
115         m_LT = lt;
116         m_isLimited = true;
117     }
118
119
120     /**
121      * Sets a "less-than-or-equal" limit requirement on checked
122      * value.
123      **/

124     public void setLTE(long lte)
125     {
126         m_LTE = lte;
127         m_isLimited = true;
128     }
129
130
131     /**
132      * Returns <i>true</i> if this condition's value is a valid
133      * number (and optionally within a specified range).
134      **/

135     public boolean eval()
136     {
137         String JavaDoc value = getValueHelper().getValue();
138
139         long l= Tk.longFrom(value,NO_VALU);
140
141         boolean ok = l!=NO_VALU;
142
143         if (ok && m_isLimited) { //painful to look at but it works
144
if (m_GTE!=NO_VALU) {
145                 if (l<m_GTE) {
146                     return false;
147                 }
148             }
149             if (m_GT!=NO_VALU) {
150                 if (l<=m_GT) {
151                     return false;
152                 }
153             }
154             if (m_LTE!=NO_VALU) {
155                 if (l>m_LTE) {
156                     return false;
157                 }
158             }
159             if (m_LT!=NO_VALU) {
160                 if (l>=m_LT) {
161                     return false;
162                 }
163             }
164         }
165
166         return ok;
167     }
168
169
170     private static final long NO_VALU=Long.MIN_VALUE;
171     private boolean m_isLimited;
172     private long m_GTE=NO_VALU,m_GT=NO_VALU;
173     private long m_LTE=NO_VALU,m_LT=NO_VALU;
174 }
175
176 /* end-of-IsNumeric.java */
177
Popular Tags