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.rm.resources.metadata.properties.ranges;
020:
021: import java.util.StringTokenizer;
022:
023: import org.openharmonise.rm.PopulateException;
024: import org.openharmonise.rm.publishing.State;
025: import org.w3c.dom.Element;
026:
027: /**
028: * Class which represents a <code>Property</code> range which is
029: * restricted to <code>Float</code> values.
030: *
031: * @author Michael Bell
032: * @version $Revision: 1.2 $
033: *
034: */
035: public class FloatRange extends NumberRange {
036:
037: /**
038: * Float range tag name
039: */
040: public final static String TAG_FLOAT_RANGE = "FloatRange";
041:
042: /**
043: * The maximum float value for this range
044: */
045: private float m_fMax = -1;
046:
047: /**
048: * The minimum float value for this range
049: */
050: private float m_fMin = -1;
051:
052: /**
053: * The separator character used to seperate range restrictions when compiling
054: * the <code>String</code> representation for the database details field
055: */
056: static final private String SEPARATOR = ":";
057:
058: /**
059: * Constructs a new <code>FloatRange</code>
060: */
061: public FloatRange() {
062: super (Float.class.getName());
063: }
064:
065: /* (non-Javadoc)
066: * @see java.lang.Object#equals(java.lang.Object)
067: */
068: public boolean equals(Object obj) {
069: boolean bResult = false;
070:
071: if (obj instanceof NumberRange) {
072: if (super .equals(obj) == true) {
073: NumberRange nRange = (NumberRange) obj;
074:
075: if (hasMin() == nRange.hasMin()
076: && hasMax() == nRange.hasMax()) {
077: bResult = true;
078: }
079: }
080: }
081:
082: return bResult;
083: }
084:
085: /* (non-Javadoc)
086: * @see org.openharmonise.rm.resources.metadata.properties.ranges.Range#isValid(java.lang.Object)
087: */
088: public boolean isValid(Object obj) {
089: boolean bIsValid = false;
090:
091: if (obj instanceof Float) {
092: bIsValid = true;
093: float fVal = ((Float) obj).floatValue();
094:
095: if (hasMin() == true) {
096: float fMin = m_fMin;
097:
098: if (m_bMinExclusive == true) {
099: fMin = fMin + 1;
100: }
101:
102: if (fVal <= fMin) {
103: bIsValid = false;
104: }
105: }
106:
107: if (hasMax() == true && bIsValid == true) {
108: float fMax = m_fMax;
109:
110: if (m_bMinExclusive == true) {
111: fMax = fMax - 1;
112: }
113:
114: if (fVal >= fMax) {
115: bIsValid = false;
116: }
117: }
118: }
119:
120: return bIsValid;
121: }
122:
123: /**
124: * Sets the maximum value for this range and whether it's an exclusive limit.
125: *
126: * @param fMax the maximum float value for this range
127: * @param bIsExclusive <code>true</code> if the maximum limit is exclusive,
128: * otherwise <code>false</code>
129: */
130: public void setMax(float fMax, boolean bIsExclusive) {
131: m_fMax = fMax;
132: m_bMaxExclusive = bIsExclusive;
133: hasMax(true);
134: isChanged(true);
135: }
136:
137: /**
138: * Sets the minimum value for this range and whether it's an exclusive limit.
139: *
140: * @param fMin the minimum float value for this range
141: * @param bIsExclusive <code>true</code> if the minimum limit is exclusive,
142: * otherwise <code>false</code>
143: */
144: public void setMin(float fMin, boolean bIsExclusive) {
145: m_fMin = fMin;
146: m_bMinExclusive = bIsExclusive;
147: hasMin(true);
148: isChanged(true);
149: }
150:
151: /**
152: * Returns the minimum value this range will allow.
153: *
154: * @return the minimum value this range will allow
155: */
156: public float getMin() {
157: return m_fMin;
158: }
159:
160: /**
161: * Returns the maximum value this range will allow.
162: *
163: * @return the maximum value this range will allow
164: */
165: public float getMax() {
166: return m_fMax;
167: }
168:
169: /* (non-Javadoc)
170: * @see org.openharmonise.rm.resources.metadata.properties.ranges.Range#getDetails()
171: */
172: public String getDetails() {
173:
174: if (isChanged()) {
175: StringBuffer strbuf = new StringBuffer();
176:
177: if (hasMin() == true) {
178: strbuf.append(m_fMin).append(SEPARATOR).append(
179: this .m_bMinExclusive);
180: } else {
181: strbuf.append(SEPARATOR);
182: }
183: strbuf.append(SEPARATOR);
184: if (hasMax() == true) {
185: strbuf.append(m_fMax).append(SEPARATOR).append(
186: this .m_bMaxExclusive);
187: } else {
188: strbuf.append(SEPARATOR);
189: }
190: super .setDetails(strbuf.toString());
191: }
192:
193: return super .getDetails();
194: }
195:
196: /* (non-Javadoc)
197: * @see org.openharmonise.rm.resources.metadata.properties.ranges.Range#setDetails(java.lang.String)
198: */
199: public void setDetails(String sDetails) {
200: if (sDetails != null) {
201: StringTokenizer tokenizer = new StringTokenizer(sDetails,
202: SEPARATOR);
203:
204: int i = 0;
205: float fTmp = 0;
206: while (tokenizer.hasMoreTokens()) {
207: String token = tokenizer.nextToken();
208:
209: if (token != null && token.length() > 0) {
210: if (i == 0 || i == 2) {
211: fTmp = Float.parseFloat(token);
212:
213: if (i == 0) {
214: m_fMin = fTmp;
215: hasMin(true);
216:
217: } else if (i == 2) {
218: m_fMax = fTmp;
219: hasMax(true);
220: }
221: } else {
222: if (i == 1) {
223: m_bMinExclusive = Boolean.getBoolean(token);
224: } else if (i == 3) {
225: m_bMaxExclusive = Boolean.getBoolean(token);
226: }
227: }
228:
229: }
230: i++;
231: }
232: }
233:
234: super .setDetails(sDetails);
235: }
236:
237: /* (non-Javadoc)
238: * @see org.openharmonise.rm.publishing.Publishable#getTagName()
239: */
240: public String getTagName() {
241: return TAG_FLOAT_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 xmlElement, State state)
248: throws PopulateException {
249:
250: String sTagname = xmlElement.getTagName();
251:
252: if (sTagname.equals(TAG_MAX)) {
253: setMax(getChildFloatValue(xmlElement),
254: isExclusive(xmlElement));
255: } else if (sTagname.equals(TAG_MIN)) {
256: setMin(getChildFloatValue(xmlElement),
257: 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 el) {
272: float fVal = 0;
273:
274: String sVal = el.getFirstChild().getNodeValue();
275:
276: if (sVal != null) {
277: fVal = Float.parseFloat(sVal);
278: }
279:
280: return fVal;
281: }
282:
283: }
|