KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thaiopensource > datatype > xsd > DecimalDatatype


1 package com.thaiopensource.datatype.xsd;
2
3 import java.math.BigDecimal JavaDoc;
4
5 import org.relaxng.datatype.ValidationContext;
6
7 class DecimalDatatype extends DatatypeBase implements OrderRelation {
8
9   boolean lexicallyAllows(String JavaDoc str) {
10     int len = str.length();
11     if (len == 0)
12       return false;
13     int i = 0;
14     switch (str.charAt(i)) {
15     case '+':
16     case '-':
17       if (++i == len)
18     return false;
19     }
20     boolean hadDecimalPoint = false;
21     if (str.charAt(i) == '.') {
22       hadDecimalPoint = true;
23       if (++i == len)
24     return false;
25     }
26     do {
27       switch (str.charAt(i)) {
28       case '0':
29       case '1':
30       case '2':
31       case '3':
32       case '4':
33       case '5':
34       case '6':
35       case '7':
36       case '8':
37       case '9':
38     break;
39       case '.':
40     if (hadDecimalPoint)
41       return false;
42     hadDecimalPoint = true;
43     break;
44       default:
45     return false;
46       }
47     } while (++i < len);
48     return true;
49   }
50
51   Object JavaDoc getValue(String JavaDoc str, ValidationContext vc) {
52     if (str.charAt(0) == '+')
53       str = str.substring(1); // JDK 1.1 doesn't handle leading +
54
return new BigDecimal JavaDoc(str);
55   }
56
57   OrderRelation getOrderRelation() {
58     return this;
59   }
60
61   public boolean isLessThan(Object JavaDoc obj1, Object JavaDoc obj2) {
62     return ((BigDecimal JavaDoc)obj1).compareTo((BigDecimal JavaDoc)obj2) < 0;
63   }
64
65   /**
66    * BigDecimal.equals considers objects distinct if they have the
67    * different scales but the same mathematical value. Similarly
68    * for hashCode.
69    */

70
71   public boolean sameValue(Object JavaDoc value1, Object JavaDoc value2) {
72     return ((BigDecimal JavaDoc)value1).compareTo((BigDecimal JavaDoc)value2) == 0;
73   }
74
75   public int valueHashCode(Object JavaDoc value) {
76     return ((BigDecimal JavaDoc)value).toBigInteger().hashCode();
77   }
78
79 }
80
Popular Tags