KickJava   Java API By Example, From Geeks To Geeks.

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


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 <code>Float</code> values.
31  *
32  * @author Michael Bell
33  * @version $Revision: 1.2 $
34  *
35  */

36 public class FloatRange extends NumberRange {
37     
38     /**
39      * Float range tag name
40      */

41     public final static String JavaDoc TAG_FLOAT_RANGE = "FloatRange";
42     
43     /**
44      * The maximum float value for this range
45      */

46     private float m_fMax = -1;
47     
48     /**
49      * The minimum float value for this range
50      */

51     private float m_fMin = -1;
52
53     /**
54      * The separator character used to seperate 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>FloatRange</code>
61      */

62     public FloatRange() {
63         super(Float JavaDoc.class.getName());
64     }
65
66     /* (non-Javadoc)
67      * @see java.lang.Object#equals(java.lang.Object)
68      */

69     public boolean equals(Object JavaDoc obj) {
70         boolean bResult = false;
71
72         if (obj instanceof NumberRange) {
73             if (super.equals(obj) == true) {
74                 NumberRange nRange = (NumberRange) obj;
75
76                 if (hasMin() == nRange.hasMin()
77                     && hasMax() == nRange.hasMax()) {
78                     bResult = true;
79                 }
80             }
81         }
82
83         return bResult;
84     }
85
86     /* (non-Javadoc)
87      * @see org.openharmonise.rm.resources.metadata.properties.ranges.Range#isValid(java.lang.Object)
88      */

89     public boolean isValid(Object JavaDoc obj) {
90         boolean bIsValid = false;
91
92         if (obj instanceof Float JavaDoc) {
93             bIsValid = true;
94             float fVal = ((Float JavaDoc) obj).floatValue();
95
96             if (hasMin() == true) {
97                 float fMin = m_fMin;
98
99                 if (m_bMinExclusive == true) {
100                     fMin = fMin + 1;
101                 }
102
103                 if (fVal <= fMin) {
104                     bIsValid = false;
105                 }
106             }
107
108             if (hasMax() == true && bIsValid == true) {
109                 float fMax = m_fMax;
110
111                 if (m_bMinExclusive == true) {
112                     fMax = fMax - 1;
113                 }
114
115                 if (fVal >= fMax) {
116                     bIsValid = false;
117                 }
118             }
119         }
120
121         return bIsValid;
122     }
123
124     /**
125      * Sets the maximum value for this range and whether it's an exclusive limit.
126      *
127      * @param fMax the maximum float value for this range
128      * @param bIsExclusive <code>true</code> if the maximum limit is exclusive,
129      * otherwise <code>false</code>
130      */

131     public void setMax(float fMax, boolean bIsExclusive) {
132         m_fMax = fMax;
133         m_bMaxExclusive = bIsExclusive;
134         hasMax(true);
135         isChanged(true);
136     }
137
138     /**
139      * Sets the minimum value for this range and whether it's an exclusive limit.
140      *
141      * @param fMin the minimum float value for this range
142      * @param bIsExclusive <code>true</code> if the minimum limit is exclusive,
143      * otherwise <code>false</code>
144      */

145     public void setMin(float fMin, boolean bIsExclusive) {
146         m_fMin = fMin;
147         m_bMinExclusive = bIsExclusive;
148         hasMin(true);
149         isChanged(true);
150     }
151
152     /**
153      * Returns the minimum value this range will allow.
154      *
155      * @return the minimum value this range will allow
156      */

157     public float getMin() {
158         return m_fMin;
159     }
160
161
162     /**
163      * Returns the maximum value this range will allow.
164      *
165      * @return the maximum value this range will allow
166      */

167     public float getMax() {
168         return m_fMax;
169     }
170
171     /* (non-Javadoc)
172      * @see org.openharmonise.rm.resources.metadata.properties.ranges.Range#getDetails()
173      */

174     public String JavaDoc getDetails() {
175
176         if (isChanged()) {
177             StringBuffer JavaDoc strbuf = new StringBuffer JavaDoc();
178
179             if (hasMin() == true) {
180                 strbuf.append(m_fMin).append(SEPARATOR).append(
181                     this.m_bMinExclusive);
182             } else {
183                 strbuf.append(SEPARATOR);
184             }
185             strbuf.append(SEPARATOR);
186             if (hasMax() == true) {
187                 strbuf.append(m_fMax).append(SEPARATOR).append(
188                     this.m_bMaxExclusive);
189             } else {
190                 strbuf.append(SEPARATOR);
191             }
192             super.setDetails(strbuf.toString());
193         }
194
195         return super.getDetails();
196     }
197
198     /* (non-Javadoc)
199      * @see org.openharmonise.rm.resources.metadata.properties.ranges.Range#setDetails(java.lang.String)
200      */

201     public void setDetails(String JavaDoc sDetails) {
202         if (sDetails != null) {
203             StringTokenizer JavaDoc tokenizer =
204                 new StringTokenizer JavaDoc(sDetails, SEPARATOR);
205
206             int i = 0;
207             float fTmp = 0;
208             while (tokenizer.hasMoreTokens()) {
209                 String JavaDoc token = tokenizer.nextToken();
210
211                 if (token != null && token.length() > 0) {
212                     if (i == 0 || i == 2) {
213                         fTmp = Float.parseFloat(token);
214
215                         if (i == 0) {
216                             m_fMin = fTmp;
217                             hasMin(true);
218                             
219                         } else if (i == 2) {
220                             m_fMax = fTmp;
221                             hasMax(true);
222                         }
223                     } else {
224                         if (i == 1) {
225                             m_bMinExclusive = Boolean.getBoolean(token);
226                         } else if (i == 3) {
227                             m_bMaxExclusive = Boolean.getBoolean(token);
228                         }
229                     }
230
231                 }
232                 i++;
233             }
234         }
235
236         super.setDetails(sDetails);
237     }
238     
239     /* (non-Javadoc)
240      * @see org.openharmonise.rm.publishing.Publishable#getTagName()
241      */

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

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

271     private float getChildFloatValue(Element JavaDoc el) {
272         float fVal = 0;
273         
274         String JavaDoc sVal = el.getFirstChild().getNodeValue();
275         
276         if(sVal != null) {
277             fVal = Float.parseFloat(sVal);
278         }
279         
280         return fVal;
281     }
282
283 }
284
Popular Tags