~ubuntu-branches/debian/wheezy/jing-trang/wheezy

« back to all changes in this revision

Viewing changes to mod/xsd-datatype/src/main/com/thaiopensource/datatype/xsd/DecimalDatatype.java

  • Committer: Bazaar Package Importer
  • Author(s): Samuel Thibault
  • Date: 2009-09-01 15:53:03 UTC
  • Revision ID: james.westby@ubuntu.com-20090901155303-2kweef05h5v9j3ni
Tags: upstream-20090818
ImportĀ upstreamĀ versionĀ 20090818

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package com.thaiopensource.datatype.xsd;
 
2
 
 
3
import org.relaxng.datatype.ValidationContext;
 
4
 
 
5
import java.math.BigDecimal;
 
6
 
 
7
class DecimalDatatype extends DatatypeBase implements OrderRelation {
 
8
 
 
9
  boolean lexicallyAllows(String 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
  String getLexicalSpaceKey() {
 
52
    return "decimal";
 
53
  }
 
54
 
 
55
  Object getValue(String str, ValidationContext vc) {
 
56
    if (str.charAt(0) == '+')
 
57
      str = str.substring(1);   // JDK 1.1 doesn't handle leading +
 
58
    return new BigDecimal(str);
 
59
  }
 
60
 
 
61
  OrderRelation getOrderRelation() {
 
62
    return this;
 
63
  }
 
64
 
 
65
  public boolean isLessThan(Object obj1, Object obj2) {
 
66
    return ((BigDecimal)obj1).compareTo((BigDecimal)obj2) < 0;
 
67
  }
 
68
 
 
69
  /**
 
70
   * BigDecimal.equals considers objects distinct if they have the
 
71
   * different scales but the same mathematical value. Similarly
 
72
   * for hashCode.
 
73
   */
 
74
 
 
75
  public boolean sameValue(Object value1, Object value2) {
 
76
    return ((BigDecimal)value1).compareTo((BigDecimal)value2) == 0;
 
77
  }
 
78
 
 
79
  public int valueHashCode(Object value) {
 
80
    return ((BigDecimal)value).toBigInteger().hashCode();
 
81
  }
 
82
 
 
83
}