KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > vfs > metadata > range > 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.vfs.metadata.range;
20
21 import org.openharmonise.commons.xml.*;
22 import org.openharmonise.vfs.metadata.*;
23 import org.openharmonise.vfs.metadata.value.*;
24 import org.w3c.dom.Element JavaDoc;
25 import org.w3c.dom.Node JavaDoc;
26 import org.w3c.dom.NodeList JavaDoc;
27 import org.w3c.dom.Text JavaDoc;
28
29
30 /**
31  * This is the range for float type properties.
32  *
33  * @author Matthew Large
34  * @version $Revision: 1.1 $
35  *
36  */

37 public class FloatRange extends AbstractRange implements Range {
38
39     /**
40      * Minimum inclusive value.
41      */

42     private Float JavaDoc m_fMinInclusive = null;
43
44     /**
45      * Maximum inclusive value.
46      */

47     private Float JavaDoc m_fMaxInclusive = null;
48
49     /**
50      * Minimum exclusive value.
51      */

52     private Float JavaDoc m_fMinExclusive = null;
53
54     /**
55      * Maximum exclusive value.
56      */

57     private Float JavaDoc m_fMaxExclusive = null;
58
59     /**
60      *
61      */

62     public FloatRange() {
63         super();
64     }
65     
66     /**
67      * Returns the minimum float value, will use minimum <i>inclusive</i> value if
68      * there is one, else it returns the minimum <i>exclusive</i> value.
69      *
70      * @return Minimum float value
71      */

72     public Float JavaDoc getMinimum() {
73         if(this.m_fMinInclusive!=null) {
74             return this.m_fMinInclusive;
75         } else {
76             return this.m_fMinExclusive;
77         }
78     }
79     
80     /**
81      * Returns the maximum float value, will use maximum <i>inclusive</i> value if
82      * there is one, else it returns the maximum <i>exclusive</i> value.
83      *
84      * @return Maximum float value
85      */

86     public Float JavaDoc getMaximum() {
87         if(this.m_fMaxInclusive!=null) {
88             return this.m_fMaxInclusive;
89         } else {
90             return this.m_fMaxExclusive;
91         }
92     }
93     
94     /**
95      * Sets the minimum inclusive float value.
96      *
97      * @param fl Value
98      */

99     public void setMinimum(Float JavaDoc fl) {
100         this.m_fMinInclusive = fl;
101     }
102     
103     /**
104      * Sets the minimum inclusive float value.
105      *
106      * @param fl Value
107      */

108     public void setMinimum(float fl) {
109         this.m_fMinInclusive = new Float JavaDoc(fl);
110     }
111     
112     /**
113      * Sets the maximum inclusive float value.
114      *
115      * @param fl Value
116      */

117     public void setMaximum(Float JavaDoc fl) {
118         this.m_fMaxInclusive = fl;
119     }
120     
121     /**
122      * Sets the maximum inclusive float value.
123      *
124      * @param fl Value
125      */

126     public void setMaximum(float fl) {
127         this.m_fMaxInclusive = new Float JavaDoc(fl);
128     }
129     
130
131     /* (non-Javadoc)
132      * @see com.simulacramedia.vfs.metadata.range.AbstractRange#validate(java.lang.String)
133      */

134     public ValidationResult validate(ValueInstance value) {
135         float fVal = ((FloatValue)value).getValue();
136         boolean bIsValid = true;
137         
138         if(m_fMinInclusive != null) {
139             bIsValid = m_fMinInclusive.floatValue() <= fVal;
140         } else if(m_fMinExclusive != null) {
141             bIsValid = m_fMinExclusive.floatValue() < fVal;
142         }
143     
144         if(bIsValid == true) {
145             if(m_fMaxInclusive != null) {
146                 bIsValid = m_fMaxInclusive.floatValue() >= fVal;
147             } else if(m_fMaxExclusive != null) {
148                 bIsValid = m_fMaxExclusive.floatValue() > fVal;
149             }
150         }
151         
152         return new ValidationResult(bIsValid,"");
153     }
154
155     /* (non-Javadoc)
156      * @see com.simulacramedia.vfs.metadata.Range#instantiate(org.w3c.dom.Element)
157      */

158     public void instantiate(Element JavaDoc elRange) {
159         Element JavaDoc elRestriction = XMLUtils.getFirstElementChild(elRange);
160         NodeList JavaDoc nl = elRestriction.getChildNodes();
161         for(int i=0; i<nl.getLength();i++) {
162             Node JavaDoc node = nl.item(i);
163             if(node.getNodeType()==Node.ELEMENT_NODE) {
164                 Element JavaDoc element = (Element JavaDoc)node;
165                 if(element.getLocalName().equalsIgnoreCase("minInclusive")) {
166                     Node JavaDoc node2 = element.getFirstChild();
167                     if( node2.getNodeType()==Node.TEXT_NODE) {
168                         this.m_fMinInclusive = Float.valueOf( ((Text JavaDoc)node2).getNodeValue() );
169                     }
170                 } else if(element.getLocalName().equalsIgnoreCase("maxInclusive")) {
171                     Node JavaDoc node2 = element.getFirstChild();
172                     if( node2.getNodeType()==Node.TEXT_NODE) {
173                         this.m_fMaxInclusive = Float.valueOf( ((Text JavaDoc)node2).getNodeValue() );
174                     }
175                 } else if(element.getLocalName().equalsIgnoreCase("minExclusive")) {
176                     Node JavaDoc node2 = element.getFirstChild();
177                     if( node2.getNodeType()==Node.TEXT_NODE) {
178                         this.m_fMinExclusive = Float.valueOf( ((Text JavaDoc)node2).getNodeValue() );
179                     }
180                 } else if(element.getLocalName().equalsIgnoreCase("maxExclusive")) {
181                     Node JavaDoc node2 = element.getFirstChild();
182                     if( node2.getNodeType()==Node.TEXT_NODE) {
183                         this.m_fMaxExclusive = Float.valueOf( ((Text JavaDoc)node2).getNodeValue() );
184                     }
185                 }
186             }
187         }
188     }
189
190     public String JavaDoc toString() {
191         StringBuffer JavaDoc sBuff = new StringBuffer JavaDoc();
192         
193         sBuff.append("FloatRange:\n");
194         if(this.m_fMinInclusive!=null) {
195             sBuff.append("minInclusive: ").append(this.m_fMinInclusive).append("\n");
196         }
197         if(this.m_fMaxInclusive!=null) {
198             sBuff.append("maxInclusive: ").append(this.m_fMaxInclusive).append("\n");
199         }
200         if(this.m_fMinExclusive!=null) {
201             sBuff.append("minExclusive: ").append(this.m_fMinExclusive).append("\n");
202         }
203         if(this.m_fMaxExclusive!=null) {
204             sBuff.append("maxExclusive: ").append(this.m_fMaxExclusive).append("\n");
205         }
206         
207         return sBuff.toString();
208     }
209
210 }
211
Popular Tags