001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019: package org.openharmonise.vfs.metadata.range;
020:
021: import org.openharmonise.commons.xml.*;
022: import org.openharmonise.vfs.metadata.*;
023: import org.openharmonise.vfs.metadata.value.*;
024: import org.w3c.dom.Element;
025: import org.w3c.dom.Node;
026: import org.w3c.dom.NodeList;
027: import org.w3c.dom.Text;
028:
029: /**
030: * This is the range for float type properties.
031: *
032: * @author Matthew Large
033: * @version $Revision: 1.1 $
034: *
035: */
036: public class FloatRange extends AbstractRange implements Range {
037:
038: /**
039: * Minimum inclusive value.
040: */
041: private Float m_fMinInclusive = null;
042:
043: /**
044: * Maximum inclusive value.
045: */
046: private Float m_fMaxInclusive = null;
047:
048: /**
049: * Minimum exclusive value.
050: */
051: private Float m_fMinExclusive = null;
052:
053: /**
054: * Maximum exclusive value.
055: */
056: private Float m_fMaxExclusive = null;
057:
058: /**
059: *
060: */
061: public FloatRange() {
062: super ();
063: }
064:
065: /**
066: * Returns the minimum float value, will use minimum <i>inclusive</i> value if
067: * there is one, else it returns the minimum <i>exclusive</i> value.
068: *
069: * @return Minimum float value
070: */
071: public Float getMinimum() {
072: if (this .m_fMinInclusive != null) {
073: return this .m_fMinInclusive;
074: } else {
075: return this .m_fMinExclusive;
076: }
077: }
078:
079: /**
080: * Returns the maximum float value, will use maximum <i>inclusive</i> value if
081: * there is one, else it returns the maximum <i>exclusive</i> value.
082: *
083: * @return Maximum float value
084: */
085: public Float getMaximum() {
086: if (this .m_fMaxInclusive != null) {
087: return this .m_fMaxInclusive;
088: } else {
089: return this .m_fMaxExclusive;
090: }
091: }
092:
093: /**
094: * Sets the minimum inclusive float value.
095: *
096: * @param fl Value
097: */
098: public void setMinimum(Float fl) {
099: this .m_fMinInclusive = fl;
100: }
101:
102: /**
103: * Sets the minimum inclusive float value.
104: *
105: * @param fl Value
106: */
107: public void setMinimum(float fl) {
108: this .m_fMinInclusive = new Float(fl);
109: }
110:
111: /**
112: * Sets the maximum inclusive float value.
113: *
114: * @param fl Value
115: */
116: public void setMaximum(Float fl) {
117: this .m_fMaxInclusive = fl;
118: }
119:
120: /**
121: * Sets the maximum inclusive float value.
122: *
123: * @param fl Value
124: */
125: public void setMaximum(float fl) {
126: this .m_fMaxInclusive = new Float(fl);
127: }
128:
129: /* (non-Javadoc)
130: * @see com.simulacramedia.vfs.metadata.range.AbstractRange#validate(java.lang.String)
131: */
132: public ValidationResult validate(ValueInstance value) {
133: float fVal = ((FloatValue) value).getValue();
134: boolean bIsValid = true;
135:
136: if (m_fMinInclusive != null) {
137: bIsValid = m_fMinInclusive.floatValue() <= fVal;
138: } else if (m_fMinExclusive != null) {
139: bIsValid = m_fMinExclusive.floatValue() < fVal;
140: }
141:
142: if (bIsValid == true) {
143: if (m_fMaxInclusive != null) {
144: bIsValid = m_fMaxInclusive.floatValue() >= fVal;
145: } else if (m_fMaxExclusive != null) {
146: bIsValid = m_fMaxExclusive.floatValue() > fVal;
147: }
148: }
149:
150: return new ValidationResult(bIsValid, "");
151: }
152:
153: /* (non-Javadoc)
154: * @see com.simulacramedia.vfs.metadata.Range#instantiate(org.w3c.dom.Element)
155: */
156: public void instantiate(Element elRange) {
157: Element elRestriction = XMLUtils.getFirstElementChild(elRange);
158: NodeList nl = elRestriction.getChildNodes();
159: for (int i = 0; i < nl.getLength(); i++) {
160: Node node = nl.item(i);
161: if (node.getNodeType() == Node.ELEMENT_NODE) {
162: Element element = (Element) node;
163: if (element.getLocalName().equalsIgnoreCase(
164: "minInclusive")) {
165: Node node2 = element.getFirstChild();
166: if (node2.getNodeType() == Node.TEXT_NODE) {
167: this .m_fMinInclusive = Float
168: .valueOf(((Text) node2).getNodeValue());
169: }
170: } else if (element.getLocalName().equalsIgnoreCase(
171: "maxInclusive")) {
172: Node node2 = element.getFirstChild();
173: if (node2.getNodeType() == Node.TEXT_NODE) {
174: this .m_fMaxInclusive = Float
175: .valueOf(((Text) node2).getNodeValue());
176: }
177: } else if (element.getLocalName().equalsIgnoreCase(
178: "minExclusive")) {
179: Node node2 = element.getFirstChild();
180: if (node2.getNodeType() == Node.TEXT_NODE) {
181: this .m_fMinExclusive = Float
182: .valueOf(((Text) node2).getNodeValue());
183: }
184: } else if (element.getLocalName().equalsIgnoreCase(
185: "maxExclusive")) {
186: Node node2 = element.getFirstChild();
187: if (node2.getNodeType() == Node.TEXT_NODE) {
188: this .m_fMaxExclusive = Float
189: .valueOf(((Text) node2).getNodeValue());
190: }
191: }
192: }
193: }
194: }
195:
196: public String toString() {
197: StringBuffer sBuff = new StringBuffer();
198:
199: sBuff.append("FloatRange:\n");
200: if (this .m_fMinInclusive != null) {
201: sBuff.append("minInclusive: ").append(this .m_fMinInclusive)
202: .append("\n");
203: }
204: if (this .m_fMaxInclusive != null) {
205: sBuff.append("maxInclusive: ").append(this .m_fMaxInclusive)
206: .append("\n");
207: }
208: if (this .m_fMinExclusive != null) {
209: sBuff.append("minExclusive: ").append(this .m_fMinExclusive)
210: .append("\n");
211: }
212: if (this .m_fMaxExclusive != null) {
213: sBuff.append("maxExclusive: ").append(this .m_fMaxExclusive)
214: .append("\n");
215: }
216:
217: return sBuff.toString();
218: }
219:
220: }
|