KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > axi > datatype > NumberBase


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.axi.datatype;
21
22 import java.math.BigDecimal JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.List JavaDoc;
26 import org.netbeans.modules.xml.axi.datatype.Datatype.Facet;
27 import org.netbeans.modules.xml.schema.model.Whitespace;
28
29 /**
30  *
31  * @author Ayub Khan
32  */

33 public abstract class NumberBase extends Datatype {
34     
35     public final static Number JavaDoc UNBOUNDED_VALUE = Double.MAX_VALUE;
36     
37     public final static String JavaDoc UNBOUNDED_STRING = "unbounded"; //NoI18n
38

39     static List JavaDoc<Facet> applicableFacets;
40     
41     private Datatype.Kind kind;
42     
43     private boolean hasFacets;
44     
45     private boolean isList;
46     
47     private List JavaDoc<Integer JavaDoc> totalDigits;
48     
49     private List JavaDoc<Integer JavaDoc> fractionDigits;
50     
51     private List JavaDoc<String JavaDoc> patterns;
52     
53     private List JavaDoc<Whitespace.Treatment> whitespaces;
54     
55     private List JavaDoc<Number JavaDoc> enumerations;
56     
57     private List JavaDoc<Number JavaDoc> maxInclusives;
58     
59     private List JavaDoc<Number JavaDoc> maxExclusives;
60     
61     private List JavaDoc<Number JavaDoc> minInclusives;
62     
63     private List JavaDoc<Number JavaDoc> minExclusives;
64     
65     /** Creates a new instance of NumberBase */
66     public NumberBase(Datatype.Kind kind) {
67         this.kind = kind;
68     }
69     
70     public Datatype.Kind getKind() {
71         return kind;
72     }
73     
74     public synchronized List JavaDoc<Facet> getApplicableFacets() {
75         if(applicableFacets == null) {
76             List JavaDoc<Facet> facets = new ArrayList JavaDoc<Facet>();
77             facets.add(Facet.TOTATDIGITS);
78             facets.add(Facet.FRACTIONDIGITS);
79             facets.add(Facet.PATTERN);
80             facets.add(Facet.WHITESPACE);
81             facets.add(Facet.ENUMERATION);
82             facets.add(Facet.MAXINCLUSIVE);
83             facets.add(Facet.MAXEXCLUSIVE);
84             facets.add(Facet.MININCLUSIVE);
85             facets.add(Facet.MINEXCLUSIVE);
86             applicableFacets = Collections.unmodifiableList(facets);
87         }
88         return applicableFacets;
89     }
90     
91     public boolean hasFacets() {
92         return hasFacets;
93     }
94     
95     public boolean isList() {
96         return isList;
97     }
98     
99     public void setIsList(boolean isList) {
100         this.isList = isList;
101     }
102     
103     /*
104      * returns pattern value (this corresponds to the value of pattern facet in schema)
105      *
106      * @return pattern
107      */

108     public List JavaDoc<String JavaDoc> getPatterns() {
109         return patterns;
110     }
111     
112     /*
113      * returns enumeration values (this corresponds to the values of enumeration facets in schema)
114      *
115      * @return enumeration
116      */

117     public List JavaDoc<Number JavaDoc> getEnumerations() {
118         return enumerations;
119     }
120     
121     /*
122      * returns whitespace value (this corresponds to the value of whitespace facet in schema)
123      *
124      * @return whitespaces
125      */

126     public List JavaDoc<Whitespace.Treatment> getWhiteSpaces() {
127         return whitespaces;
128     }
129     
130     /*
131      * returns total digits value (this corresponds to the value of totalDigits facet in schema)
132      *
133      * @return totalDigits
134      */

135     public List JavaDoc<Integer JavaDoc> getTotalDigits() {
136         return totalDigits;
137     }
138     
139     /*
140      * returns fraction digits value (this corresponds to the value of fractionDigits facet in schema)
141      *
142      * @return fractionDigits
143      */

144     public List JavaDoc<Integer JavaDoc> getFractionDigits() {
145         return fractionDigits;
146     }
147     
148     /*
149      * returns maximum Inclusive value (this corresponds to the value of maxInclusive facet in schema)
150      *
151      * @return maxInclusive
152      */

153     public List JavaDoc<Number JavaDoc> getMaxInclusives() {
154         return maxInclusives;
155     }
156     
157     /*
158      * returns maximum Exclusive value (this corresponds to the value of maxExclusive facet in schema)
159      *
160      * @return maxExclusive
161      */

162     public List JavaDoc<Number JavaDoc> getMaxExclusives() {
163         return maxExclusives;
164     }
165     
166     /*
167      * returns minimum Inclusive value (this corresponds to the value of minInclusive facet in schema)
168      *
169      * @return minInclusive
170      */

171     public List JavaDoc<Number JavaDoc> getMinInclusives() {
172         return minInclusives;
173     }
174     
175     /*
176      * returns minExclusive value (this corresponds to the value of minExclusive facet in schema)
177      *
178      * @return minExclusive
179      */

180     public List JavaDoc<Number JavaDoc> getMinExclusives() {
181         return minExclusives;
182     }
183     
184     /*
185      * set pattern value (this corresponds to the value of pattern facet in schema)
186      *
187      * @param pattern
188      */

189     public void addPattern(String JavaDoc pattern) {
190         if(patterns == null) {
191             patterns = new ArrayList JavaDoc<String JavaDoc>(1);
192             hasFacets = true;
193         }
194         this.patterns.add(pattern);
195     }
196     
197     /*
198      * set enumeration values (this corresponds to the values of enumeration facets in schema)
199      *
200      * @param enumeration
201      */

202     public void addEnumeration(Number JavaDoc enumeration) {
203         if(enumerations == null) {
204             enumerations = new ArrayList JavaDoc<Number JavaDoc>(1);
205             hasFacets = true;
206         }
207         this.enumerations.add(enumeration);
208     }
209     
210     /*
211      * set whitespace value (this corresponds to the value of whitespace facet in schema)
212      *
213      * @param whitespace
214      */

215     public void addWhitespace(Whitespace.Treatment whitespace) {
216         if(whitespaces == null) {
217             whitespaces = new ArrayList JavaDoc<Whitespace.Treatment>(1);
218             hasFacets = true;
219         }
220         this.whitespaces.add(whitespace);
221     }
222     
223     /*
224      * set total digits value (this corresponds to the value of totalDigits facet in schema)
225      *
226      * @param totalDigits
227      */

228     public void addTotalDigits(int totalDigits ) {
229         if(this.totalDigits == null) {
230             this.totalDigits = new ArrayList JavaDoc<Integer JavaDoc>(1);
231             hasFacets = true;
232         }
233         this.totalDigits.add(new Integer JavaDoc(totalDigits));
234     }
235     
236     /*
237      * set fraction digits value (this corresponds to the value of fractionDigits facet in schema)
238      *
239      * @param fractionDigits
240      */

241     public void addFractionDigits(int fractionDigits) {
242         if(this.fractionDigits == null) {
243             this.fractionDigits = new ArrayList JavaDoc<Integer JavaDoc>(1);
244             hasFacets = true;
245         }
246         this.fractionDigits.add(new Integer JavaDoc(fractionDigits));
247     }
248     
249     /*
250      * set maximum Inclusive value (this corresponds to the value of maxInclusive facet in schema)
251      *
252      * @param maxInclusive
253      */

254     public void addMaxInclusive(Number JavaDoc maxInclusive) {
255         if(maxInclusives == null) {
256             maxInclusives = new ArrayList JavaDoc<Number JavaDoc>(1);
257             hasFacets = true;
258         }
259         this.maxInclusives.add(maxInclusive);
260     }
261     
262     /*
263      * set maximum Exclusive value (this corresponds to the value of maxExclusive facet in schema)
264      *
265      * @param maxExclusive
266      */

267     public void addMaxExclusive(Number JavaDoc maxExclusive) {
268         if(maxExclusives == null) {
269             maxExclusives = new ArrayList JavaDoc<Number JavaDoc>(1);
270             hasFacets = true;
271         }
272         this.maxExclusives.add(maxExclusive);
273     }
274     
275     /*
276      * set minimum Inclusive value (this corresponds to the value of minInclusive facet in schema)
277      *
278      * @param minInclusive
279      */

280     public void addMinInclusive(Number JavaDoc minInclusive) {
281         if(minInclusives == null) {
282             minInclusives = new ArrayList JavaDoc<Number JavaDoc>(1);
283             hasFacets = true;
284         }
285         this.minInclusives.add(minInclusive);
286     }
287     
288     /*
289      * set minExclusive value (this corresponds to the value of minExclusive facet in schema)
290      *
291      * @param minExclusive
292      */

293     public void addMinExclusive(Number JavaDoc minExclusive) {
294         if(minExclusives == null) {
295             minExclusives = new ArrayList JavaDoc<Number JavaDoc>(1);
296             hasFacets = true;
297         }
298         this.minExclusives.add(minExclusive);
299     }
300     
301     /*
302      * remove pattern value (this corresponds to the value of pattern facet in schema)
303      *
304      * @param pattern
305      */

306     public void removePattern(String JavaDoc pattern) {
307         if(patterns != null)
308             patterns.remove(pattern);
309     }
310     
311     /*
312      * set enumeration values (this corresponds to the values of enumeration facets in schema)
313      *
314      * @param enumeration
315      */

316     public void removeEnumeration(Number JavaDoc enumeration) {
317         if(enumerations != null)
318             enumerations.remove(enumeration);
319     }
320     
321     /*
322      * set whitespace value (this corresponds to the value of whitespace facet in schema)
323      *
324      * @param whitespace
325      */

326     public void removeWhitespace(Whitespace.Treatment whitespace) {
327         if(whitespaces != null)
328             whitespaces.remove(whitespace);
329     }
330     
331     /*
332      * set total digits value (this corresponds to the value of totalDigits facet in schema)
333      *
334      * @param totalDigits
335      */

336     public void removeTotalDigits(Number JavaDoc totalDigits ) {
337         if(this.totalDigits != null)
338             this.totalDigits.remove(totalDigits);
339     }
340     
341     /*
342      * set fraction digits value (this corresponds to the value of fractionDigits facet in schema)
343      *
344      * @param fractionDigits
345      */

346     public void removeFractionDigits(Number JavaDoc fractionDigits) {
347         if(this.fractionDigits != null)
348             this.fractionDigits.remove(fractionDigits);
349     }
350     
351     /*
352      * set maximum Inclusive value (this corresponds to the value of maxInclusive facet in schema)
353      *
354      * @param maxInclusive
355      */

356     public void removeMaxInclusive(Number JavaDoc maxInclusive) {
357         if(maxInclusives != null)
358             maxInclusives.remove(maxInclusive);
359     }
360     
361     /*
362      * set maximum Exclusive value (this corresponds to the value of maxExclusive facet in schema)
363      *
364      * @param maxExclusive
365      */

366     public void removeMaxExclusive(Number JavaDoc maxExclusive) {
367         if(maxExclusives != null)
368             maxExclusives.remove(maxExclusive);
369     }
370     
371     /*
372      * set minimum Inclusive value (this corresponds to the value of minInclusive facet in schema)
373      *
374      * @param minInclusive
375      */

376     public void removeMinInclusive(Number JavaDoc minInclusive) {
377         if(minInclusives != null)
378             minInclusives.remove(minInclusive);
379     }
380     
381     /*
382      * set minExclusive value (this corresponds to the value of minExclusive facet in schema)
383      *
384      * @param minExclusive
385      */

386     public void removeMinExclusive(Number JavaDoc minExclusive) {
387         if(minExclusives != null)
388             minExclusives.remove(minExclusive);
389     }
390     
391     public static Number JavaDoc toNumber(String JavaDoc value) {
392         Number JavaDoc n = null;
393         if(value.equals(UNBOUNDED_STRING))
394             n = UNBOUNDED_VALUE;
395         else {
396             try{
397                 n = new BigDecimal JavaDoc(value);
398             } catch(Throwable JavaDoc th) {
399                 n = 0;
400             }
401         }
402         return n;
403     }
404     
405     public static String JavaDoc toXMLString(Number JavaDoc val) {
406         if(val == UNBOUNDED_VALUE)
407             return UNBOUNDED_STRING;
408         else
409             return String.valueOf(val);
410     }
411 }
412
Popular Tags