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 integer values.
030: *
031: * @author Michael Bell
032: * @version $Revision: 1.2 $
033: *
034: */
035: public class IntegerRange extends NumberRange {
036:
037: /**
038: * Integer range tag name
039: */
040: public final static String TAG_INTEGER_RANGE = "IntegerRange";
041:
042: /**
043: * The maximum limit for this range
044: */
045: private int m_nMax = -1;
046:
047: /**
048: * The minumum limit for this range
049: */
050: private int m_nMin = -1;
051:
052: /**
053: * The separator character used to separate 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>IntegerRange</code>
060: */
061: public IntegerRange() {
062: super (Integer.class.getName());
063: }
064:
065: /* (non-Javadoc)
066: * @see org.openharmonise.rm.resources.metadata.properties.ranges.Range#isValid(java.lang.Object)
067: */
068: public boolean isValid(Object obj) {
069: boolean bIsValid = false;
070:
071: if (obj instanceof Integer) {
072: bIsValid = true;
073: int nVal = ((Integer) obj).intValue();
074:
075: if (hasMin() == true) {
076: int nMin = m_nMin;
077:
078: if (m_bMinExclusive == true) {
079: nMin = nMin + 1;
080: }
081:
082: if (nVal <= nMin) {
083: bIsValid = false;
084: }
085: }
086:
087: if (hasMax() == true && bIsValid == true) {
088: int nMax = m_nMax;
089:
090: if (m_bMinExclusive == true) {
091: nMax = nMax - 1;
092: }
093:
094: if (nVal >= nMax) {
095: bIsValid = false;
096: }
097: }
098: }
099:
100: return bIsValid;
101: }
102:
103: /**
104: * Sets the maximum value for this range and whether it's an exclusive limit.
105: *
106: * @param nMax the maximum integer limit for this range
107: * @param bIsExclusive <code>true</code> if the maximum limit is exclusive,
108: * otherwise <code>false</code>
109: */
110: public void setMax(int nMax, boolean bIsExclusive) {
111: m_nMax = nMax;
112: m_bMaxExclusive = bIsExclusive;
113: hasMax(true);
114: isChanged(true);
115: }
116:
117: /**
118: * Sets the minimum value for this range and whether it's an exclusive limit.
119: *
120: * @param nMax the minimum integer value for this range
121: * @param bIsExclusive <code>true</code> if the minimum limit is exclusive,
122: * otherwise <code>false</code>
123: */
124: public void setMin(int nMin, boolean bIsExclusive) {
125: m_nMin = nMin;
126: m_bMinExclusive = bIsExclusive;
127: hasMin(true);
128: isChanged(true);
129: }
130:
131: /**
132: * Returns the minimum value this range will allow.
133: *
134: * @return the minimum value this range will allow
135: */
136: public int getMin() {
137: return m_nMin;
138: }
139:
140: /**
141: * Returns the maximum value this range will allow.
142: *
143: * @return the maximum value this range will allow
144: */
145: public int getMax() {
146: return m_nMax;
147: }
148:
149: /* (non-Javadoc)
150: * @see org.openharmonise.rm.resources.metadata.properties.ranges.Range#getDetails()
151: */
152: public String getDetails() {
153:
154: if (isChanged()) {
155: StringBuffer strbuf = new StringBuffer();
156:
157: if (hasMin() == true) {
158: strbuf.append(m_nMin).append(SEPARATOR).append(
159: this .m_bMinExclusive);
160: } else {
161: strbuf.append(SEPARATOR);
162: }
163: strbuf.append(SEPARATOR);
164: if (hasMax() == true) {
165: strbuf.append(m_nMax).append(SEPARATOR).append(
166: this .m_bMaxExclusive);
167: } else {
168: strbuf.append(SEPARATOR);
169: }
170: super .setDetails(strbuf.toString());
171: }
172:
173: return super .getDetails();
174: }
175:
176: /* (non-Javadoc)
177: * @see org.openharmonise.rm.resources.metadata.properties.ranges.Range#setDetails(java.lang.String)
178: */
179: public void setDetails(String sDetails) {
180: if (sDetails != null) {
181: StringTokenizer tokenizer = new StringTokenizer(sDetails,
182: SEPARATOR);
183:
184: int i = 0;
185: int nTmp = 0;
186: while (tokenizer.hasMoreTokens()) {
187: String token = tokenizer.nextToken();
188:
189: if (token != null && token.length() > 0) {
190: if (i == 0 || i == 2) {
191: nTmp = Integer.parseInt(token);
192:
193: if (i == 0) {
194: m_nMin = nTmp;
195: hasMin(true);
196:
197: } else if (i == 2) {
198: m_nMax = nTmp;
199: hasMax(true);
200: }
201: } else {
202: if (i == 1) {
203: m_bMinExclusive = Boolean.getBoolean(token);
204: } else if (i == 3) {
205: m_bMaxExclusive = Boolean.getBoolean(token);
206: }
207: }
208:
209: }
210: i++;
211: }
212: }
213:
214: super .setDetails(sDetails);
215: }
216:
217: /* (non-Javadoc)
218: * @see java.lang.Object#equals(java.lang.Object)
219: */
220: public boolean equals(Object obj) {
221: boolean bResult = false;
222:
223: if (obj instanceof IntegerRange) {
224: if (super .equals(obj) == true) {
225: IntegerRange iRange = (IntegerRange) obj;
226:
227: if (m_nMin == iRange.getMin()
228: && 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 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 xmlElement, State state)
248: throws PopulateException {
249:
250: String sTagname = xmlElement.getTagName();
251:
252: if (sTagname.equals(TAG_MAX)) {
253: setMax(getChildIntValue(xmlElement),
254: isExclusive(xmlElement));
255: } else if (sTagname.equals(TAG_MIN)) {
256: setMin(getChildIntValue(xmlElement),
257: isExclusive(xmlElement));
258: } else {
259: super .populate(xmlElement, state);
260: }
261:
262: }
263:
264: /**
265: * Returns the <code>int</code> value taken from the first child node
266: * of the specified XML element.
267: *
268: * @param xmlElement the XML element
269: * @return the <code>int</code> value
270: */
271: private int getChildIntValue(Element el) {
272: int nVal = 0;
273:
274: String sVal = el.getFirstChild().getNodeValue();
275:
276: if (sVal != null) {
277: nVal = Integer.parseInt(sVal);
278: }
279:
280: return nVal;
281: }
282: }
|