KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > vfs > metadata > range > IntegerRange


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 integer type properties.
32  *
33  * @author Matthew Large
34  * @version $Revision: 1.1 $
35  *
36  */

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

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

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

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

57     private Integer JavaDoc m_nMaxExclusive = null;
58
59     /**
60      *
61      */

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

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

87     public Integer JavaDoc getMaximum() {
88         if( this.m_nMaxInclusive!=null ) {
89             return this.m_nMaxInclusive;
90         } else {
91             return this.m_nMaxExclusive;
92         }
93     }
94     
95     /**
96      * Sets the minimum inclusive integer value.
97      *
98      * @param nVal Value
99      */

100     public void setMinimum(Integer JavaDoc nVal) {
101         this.m_nMinInclusive = nVal;
102     }
103     
104     /**
105      * Sets the minimum inclusive integer value.
106      *
107      * @param nVal Value
108      */

109     public void setMinimum(int nVal) {
110         this.m_nMinInclusive = new Integer JavaDoc(nVal);
111     }
112     
113     /**
114      * Sets the maximum inclusive integer value.
115      *
116      * @param nVal Value
117      */

118     public void setMaximum(Integer JavaDoc nVal) {
119         this.m_nMaxInclusive = nVal;
120     }
121     
122     /**
123      * Sets the maximum inclusive integer value.
124      *
125      * @param nVal Value
126      */

127     public void setMaximum(int nVal) {
128         this.m_nMaxInclusive = new Integer JavaDoc(nVal);
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         int nVal = ((IntegerValue)value).getValue();
136         boolean bIsValid = true;
137         
138         if(m_nMinInclusive != null) {
139             bIsValid = m_nMinInclusive.intValue() <= nVal;
140         } else if(m_nMinExclusive != null) {
141             bIsValid = m_nMinExclusive.intValue() < nVal;
142         }
143     
144         if(bIsValid == true) {
145             if(m_nMaxInclusive != null) {
146                 bIsValid = m_nMaxInclusive.intValue() >= nVal;
147             } else if(m_nMaxExclusive != null) {
148                 bIsValid = m_nMaxExclusive.intValue() > nVal;
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_nMinInclusive = Integer.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_nMaxInclusive = Integer.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_nMinExclusive = Integer.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_nMaxExclusive = Integer.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("IntegerRange:\n");
194         if(this.m_nMinInclusive!=null) {
195             sBuff.append("minInclusive: ").append(this.m_nMinInclusive).append("\n");
196         }
197         if(this.m_nMaxInclusive!=null) {
198             sBuff.append("maxInclusive: ").append(this.m_nMaxInclusive).append("\n");
199         }
200         if(this.m_nMinExclusive!=null) {
201             sBuff.append("minExclusive: ").append(this.m_nMinExclusive).append("\n");
202         }
203         if(this.m_nMaxExclusive!=null) {
204             sBuff.append("maxExclusive: ").append(this.m_nMaxExclusive).append("\n");
205         }
206         
207         return sBuff.toString();
208     }
209
210 }
211
Popular Tags