KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > rm > resources > metadata > properties > ranges > IntegerRange


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19 package org.openharmonise.rm.resources.metadata.properties.ranges;
20
21 import java.util.StringTokenizer JavaDoc;
22
23 import org.openharmonise.rm.PopulateException;
24 import org.openharmonise.rm.publishing.State;
25 import org.w3c.dom.Element JavaDoc;
26
27
28 /**
29  * Class which represents a <code>Property</code> range which is
30  * restricted to integer values.
31  *
32  * @author Michael Bell
33  * @version $Revision: 1.2 $
34  *
35  */

36 public class IntegerRange extends NumberRange {
37
38     /**
39      * Integer range tag name
40      */

41     public final static String JavaDoc TAG_INTEGER_RANGE = "IntegerRange";
42
43     /**
44      * The maximum limit for this range
45      */

46     private int m_nMax = -1;
47     
48     /**
49      * The minumum limit for this range
50      */

51     private int m_nMin = -1;
52
53     /**
54      * The separator character used to separate range restrictions when compiling
55      * the <code>String</code> representation for the database details field.
56      */

57     static final private String JavaDoc SEPARATOR = ":";
58
59     /**
60      * Constructs a new <code>IntegerRange</code>
61      */

62     public IntegerRange() {
63         super(Integer JavaDoc.class.getName());
64     }
65
66     /* (non-Javadoc)
67      * @see org.openharmonise.rm.resources.metadata.properties.ranges.Range#isValid(java.lang.Object)
68      */

69     public boolean isValid(Object JavaDoc obj) {
70         boolean bIsValid = false;
71
72         if (obj instanceof Integer JavaDoc) {
73             bIsValid = true;
74             int nVal = ((Integer JavaDoc) obj).intValue();
75
76             if (hasMin() == true) {
77                 int nMin = m_nMin;
78
79                 if (m_bMinExclusive == true) {
80                     nMin = nMin + 1;
81                 }
82
83                 if (nVal <= nMin) {
84                     bIsValid = false;
85                 }
86             }
87
88             if (hasMax() == true && bIsValid == true) {
89                 int nMax = m_nMax;
90
91                 if (m_bMinExclusive == true) {
92                     nMax = nMax - 1;
93                 }
94
95                 if (nVal >= nMax) {
96                     bIsValid = false;
97                 }
98             }
99         }
100
101         return bIsValid;
102     }
103
104     /**
105      * Sets the maximum value for this range and whether it's an exclusive limit.
106      *
107      * @param nMax the maximum integer limit for this range
108      * @param bIsExclusive <code>true</code> if the maximum limit is exclusive,
109      * otherwise <code>false</code>
110      */

111     public void setMax(int nMax, boolean bIsExclusive) {
112         m_nMax = nMax;
113         m_bMaxExclusive = bIsExclusive;
114         hasMax(true);
115         isChanged(true);
116     }
117
118     /**
119      * Sets the minimum value for this range and whether it's an exclusive limit.
120      *
121      * @param nMax the minimum integer value for this range
122      * @param bIsExclusive <code>true</code> if the minimum limit is exclusive,
123      * otherwise <code>false</code>
124      */

125     public void setMin(int nMin, boolean bIsExclusive) {
126         m_nMin = nMin;
127         m_bMinExclusive = bIsExclusive;
128         hasMin(true);
129         isChanged(true);
130     }
131
132     /**
133      * Returns the minimum value this range will allow.
134      *
135      * @return the minimum value this range will allow
136      */

137     public int getMin() {
138         return m_nMin;
139     }
140
141     /**
142      * Returns the maximum value this range will allow.
143      *
144      * @return the maximum value this range will allow
145      */

146     public int getMax() {
147         return m_nMax;
148     }
149
150     /* (non-Javadoc)
151      * @see org.openharmonise.rm.resources.metadata.properties.ranges.Range#getDetails()
152      */

153     public String JavaDoc getDetails() {
154
155         if (isChanged()) {
156             StringBuffer JavaDoc strbuf = new StringBuffer JavaDoc();
157
158             if (hasMin() == true) {
159                 strbuf.append(m_nMin).append(SEPARATOR).append(
160                     this.m_bMinExclusive);
161             } else {
162                 strbuf.append(SEPARATOR);
163             }
164             strbuf.append(SEPARATOR);
165             if (hasMax() == true) {
166                 strbuf.append(m_nMax).append(SEPARATOR).append(
167                     this.m_bMaxExclusive);
168             } else {
169                 strbuf.append(SEPARATOR);
170             }
171             super.setDetails(strbuf.toString());
172         }
173
174         return super.getDetails();
175     }
176
177     /* (non-Javadoc)
178      * @see org.openharmonise.rm.resources.metadata.properties.ranges.Range#setDetails(java.lang.String)
179      */

180     public void setDetails(String JavaDoc sDetails) {
181         if (sDetails != null) {
182             StringTokenizer JavaDoc tokenizer =
183                 new StringTokenizer JavaDoc(sDetails, SEPARATOR);
184
185             int i = 0;
186             int nTmp = 0;
187             while (tokenizer.hasMoreTokens()) {
188                 String JavaDoc token = tokenizer.nextToken();
189
190                 if (token != null && token.length() > 0) {
191                     if (i == 0 || i == 2) {
192                         nTmp = Integer.parseInt(token);
193
194                         if (i == 0) {
195                             m_nMin = nTmp;
196                             hasMin(true);
197
198                         } else if (i == 2) {
199                             m_nMax = nTmp;
200                             hasMax(true);
201                         }
202                     } else {
203                         if (i == 1) {
204                             m_bMinExclusive = Boolean.getBoolean(token);
205                         } else if (i == 3) {
206                             m_bMaxExclusive = Boolean.getBoolean(token);
207                         }
208                     }
209
210                 }
211                 i++;
212             }
213         }
214
215         super.setDetails(sDetails);
216     }
217
218     /* (non-Javadoc)
219      * @see java.lang.Object#equals(java.lang.Object)
220      */

221     public boolean equals(Object JavaDoc obj) {
222         boolean bResult = false;
223
224         if (obj instanceof IntegerRange) {
225             if (super.equals(obj) == true) {
226                 IntegerRange iRange = (IntegerRange) obj;
227
228                 if (m_nMin == iRange.getMin() && m_nMax == iRange.getMax()) {
229                     bResult = true;
230                 }
231             }
232         }
233
234         return bResult;
235     }
236
237     /* (non-Javadoc)
238      * @see org.openharmonise.rm.publishing.Publishable#getTagName()
239      */

240     public String JavaDoc getTagName() {
241         return TAG_INTEGER_RANGE;
242     }
243     
244     /* (non-Javadoc)
245      * @see org.openharmonise.rm.publishing.Publishable#populate(org.w3c.dom.Element, org.openharmonise.rm.publishing.State)
246      */

247     public void populate(Element JavaDoc xmlElement, State state)
248         throws PopulateException {
249     
250         String JavaDoc sTagname = xmlElement.getTagName();
251     
252         if(sTagname.equals(TAG_MAX)) {
253             setMax(getChildIntValue(xmlElement),isExclusive(xmlElement));
254         } else if (sTagname.equals(TAG_MIN)) {
255             setMin(getChildIntValue(xmlElement),isExclusive(xmlElement));
256         } else {
257             super.populate(xmlElement, state);
258         }
259     
260     }
261
262     /**
263      * Returns the <code>int</code> value taken from the first child node
264      * of the specified XML element.
265      *
266      * @param xmlElement the XML element
267      * @return the <code>int</code> value
268      */

269     private int getChildIntValue(Element JavaDoc el) {
270         int nVal = 0;
271         
272         String JavaDoc sVal = el.getFirstChild().getNodeValue();
273         
274         if(sVal != null) {
275             nVal = Integer.parseInt(sVal);
276         }
277         
278         return nVal;
279     }
280 }
281
Popular Tags