1
2
3
4
5
6
7
8
9
10
11
12
13
14 package org.exolab.castor.builder.types;
15
16 import org.exolab.javasource.JClass;
17 import org.exolab.javasource.JSourceCode;
18 import org.exolab.javasource.JType;
19
20
21
22
23
24
25
26
27 public final class XSDouble extends AbstractRangeFacet {
28
29
30 public static final String NAME = "double";
31
32
33 public static final short TYPE = XSType.DOUBLE_TYPE;
34
35
36 public static final String MIN_VALUE = Double.toString(-Double.MAX_VALUE);
37
38
39 public static final String MAX_VALUE = Double.toString(Double.MAX_VALUE);
40
41 public static final String INFINITY_VALUE = "Double.POSITIVE_INFINITY";
42 public static final String NEGATIVE_INFINITY_VALUE = "Double.NEGATIVE_INFINITY";
43
44
45 private final boolean _asWrapper;
46
47
48 private final JType _jType;
49
50
51
52
53 public XSDouble() {
54 this(false);
55 }
56
57
58
59
60
61
62 public XSDouble(final boolean asWrapper) {
63 super();
64
65 _asWrapper = asWrapper;
66 if (_asWrapper) {
67 _jType = new JClass("java.lang.Double");
68 } else {
69 _jType = JType.DOUBLE;
70 }
71
72 setMinInclusive(MIN_VALUE);
73 setMaxInclusive(MAX_VALUE);
74
75 setPositiveInfinity(INFINITY_VALUE);
76 setNegativeInfinity(NEGATIVE_INFINITY_VALUE);
77 }
78
79
80
81
82 public String getName() {
83 return NAME;
84 }
85
86
87
88
89 public short getType() {
90 return TYPE;
91 }
92
93
94
95
96 public boolean isPrimitive() {
97 return true;
98 }
99
100
101
102
103 public boolean isDateTime() {
104 return false;
105 }
106
107
108
109
110 public JType getJType() {
111 return _jType;
112 }
113
114
115
116
117 public String newInstanceCode() {
118 return "new java.lang.Double(0.0);";
119 }
120
121
122
123
124 public String createToJavaObjectCode(final String variableName) {
125 if (_asWrapper) {
126 return variableName;
127 }
128 return "new java.lang.Double(" + variableName + ")";
129 }
130
131
132
133
134 public String createFromJavaObjectCode(final String variableName) {
135 if (_asWrapper) {
136 return "((java.lang.Double) " + variableName + ")";
137 }
138 return "((java.lang.Double) " + variableName + ").doubleValue()";
139 }
140
141
142
143
144 public void validationCode(final JSourceCode jsc, final String fixedValue,
145 final String validatorInstanceName) {
146 jsc.add("org.exolab.castor.xml.validators.DoubleValidator typeValidator;\n"
147 + "typeValidator = new org.exolab.castor.xml.validators.DoubleValidator();\n"
148 + "{0}.setValidator(typeValidator);", validatorInstanceName);
149
150 if (fixedValue != null) {
151 jsc.add("typeValidator.setFixed(" + fixedValue + ");");
152 }
153
154 codePatternFacet(jsc, "typeValidator");
155 codeWhiteSpaceFacet(jsc, "typeValidator");
156
157 if (getMinExclusive() != null) {
158 jsc.add("typeValidator.setMinExclusive(" + getMinExclusive() + ");");
159 } else if (getMinInclusive() != null) {
160 jsc.add("typeValidator.setMinInclusive(" + getMinInclusive() + ");");
161 }
162
163 if (getMaxExclusive() != null) {
164 jsc.add("typeValidator.setMaxExclusive(" + getMaxExclusive() + ");");
165 } else if (getMaxInclusive() != null) {
166 jsc.add("typeValidator.setMaxInclusive(" + getMaxInclusive() + ");");
167 }
168
169 if (getPositiveInfinity() != null) {
170 jsc.add("typeValidator.setPositiveInfinity(java.lang." + getPositiveInfinity() + ");");
171 }
172
173 if (getNegativeInfinity() != null) {
174 jsc.add("typeValidator.setNegativeInfinity(java.lang." + getNegativeInfinity() + ");");
175 }
176 }
177
178
179
180
181 public String createDefaultValueWithString(final String variableName) {
182 if (_asWrapper) {
183 return "java.lang.Double.valueOf(" + variableName + ")";
184 }
185 return "java.lang.Double.valueOf(" + variableName + ").doubleValue()";
186 }
187
188 }