KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.thaiopensource.datatype.xsd;
2
3 import org.relaxng.datatype.ValidationContext;
4
5 class DoubleDatatype extends DatatypeBase implements OrderRelation {
6
7   boolean lexicallyAllows(String JavaDoc str) {
8     if (str.equals("INF") || str.equals("-INF") || str.equals("NaN"))
9       return true;
10     int len = str.length();
11     boolean hadSign = false;
12     boolean hadDecimalPoint = false;
13     boolean hadDigit = false;
14     boolean hadE = false;
15     for (int i = 0; i < len; i++) {
16       switch (str.charAt(i)) {
17       case '+':
18       case '-':
19     if (hadDigit || hadDecimalPoint || hadSign)
20       return false;
21     hadSign = true;
22     break;
23       case '0':
24       case '1':
25       case '2':
26       case '3':
27       case '4':
28       case '5':
29       case '6':
30       case '7':
31       case '8':
32       case '9':
33     hadDigit = true;
34     break;
35       case 'e':
36       case 'E':
37     if (hadE || !hadDigit)
38       return false;
39     hadDigit = false;
40     hadE = true;
41     hadSign = false;
42     hadDecimalPoint = false;
43     break;
44       case '.':
45     if (hadDecimalPoint || hadE)
46       return false;
47     hadDecimalPoint = true;
48     break;
49       default:
50     return false;
51       }
52     }
53     return hadDigit;
54   }
55
56   Object JavaDoc getValue(String JavaDoc str, ValidationContext vc) {
57     if (str.equals("INF"))
58       return new Double JavaDoc(Double.POSITIVE_INFINITY);
59     if (str.equals("-INF"))
60       return new Double JavaDoc(Double.NEGATIVE_INFINITY);
61     if (str.equals("NaN"))
62       return new Double JavaDoc(Double.NaN);
63     return new Double JavaDoc(str);
64   }
65
66   OrderRelation getOrderRelation() {
67     return this;
68   }
69
70   public boolean isLessThan(Object JavaDoc obj1, Object JavaDoc obj2) {
71     return ((Double JavaDoc)obj1).doubleValue() < ((Double JavaDoc)obj2).doubleValue();
72   }
73
74   public boolean sameValue(Object JavaDoc value1, Object JavaDoc value2) {
75     double d1 = ((Double JavaDoc)value1).doubleValue();
76     double d2 = ((Double JavaDoc)value2).doubleValue();
77     // NaN = NaN
78
return d1 == d2 || (d1 != d1 && d2 != d2);
79   }
80 }
81
Popular Tags