1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package org.modeshape.graph.property.basic;
25
26 import java.io.InputStream;
27 import java.io.Reader;
28 import java.math.BigDecimal;
29 import java.net.URI;
30 import java.util.Calendar;
31 import java.util.Date;
32 import java.util.UUID;
33 import net.jcip.annotations.Immutable;
34 import org.modeshape.common.text.TextDecoder;
35 import org.modeshape.graph.GraphI18n;
36 import org.modeshape.graph.property.Binary;
37 import org.modeshape.graph.property.DateTime;
38 import org.modeshape.graph.property.IoException;
39 import org.modeshape.graph.property.Name;
40 import org.modeshape.graph.property.Path;
41 import org.modeshape.graph.property.PropertyType;
42 import org.modeshape.graph.property.Reference;
43 import org.modeshape.graph.property.ValueFactory;
44 import org.modeshape.graph.property.ValueFormatException;
45
46
47
48
49 @Immutable
50 public class DoubleValueFactory extends AbstractValueFactory<Double> {
51
52 public DoubleValueFactory( TextDecoder decoder,
53 ValueFactory<String> stringValueFactory ) {
54 super(PropertyType.DOUBLE, decoder, stringValueFactory);
55 }
56
57
58
59
60 public Double create( String value ) {
61 if (value == null) return null;
62 try {
63 return Double.valueOf(value.trim());
64 } catch (NumberFormatException err) {
65 throw new ValueFormatException(value, getPropertyType(),
66 GraphI18n.errorConvertingType.text(String.class.getSimpleName(),
67 Double.class.getSimpleName(),
68 value), err);
69 }
70 }
71
72
73
74
75 public Double create( String value,
76 TextDecoder decoder ) {
77
78 return create(getDecoder(decoder).decode(value));
79 }
80
81
82
83
84 public Double create( int value ) {
85 return Double.valueOf(value);
86 }
87
88
89
90
91 public Double create( long value ) {
92 return new Double(value);
93 }
94
95
96
97
98 public Double create( boolean value ) {
99 throw new ValueFormatException(value, getPropertyType(), GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
100 Double.class.getSimpleName(),
101 value));
102 }
103
104
105
106
107 public Double create( float value ) {
108 return Double.valueOf(value);
109 }
110
111
112
113
114 public Double create( double value ) {
115 return value;
116 }
117
118
119
120
121 public Double create( BigDecimal value ) {
122 if (value == null) return null;
123 double result = value.doubleValue();
124 if (result == Double.NEGATIVE_INFINITY || result == Double.POSITIVE_INFINITY) {
125 throw new ValueFormatException(value, getPropertyType(),
126 GraphI18n.errorConvertingType.text(BigDecimal.class.getSimpleName(),
127 Double.class.getSimpleName(),
128 value));
129 }
130 return result;
131 }
132
133
134
135
136 public Double create( Calendar value ) {
137 if (value == null) return null;
138 return create(value.getTimeInMillis());
139 }
140
141
142
143
144 public Double create( Date value ) {
145 if (value == null) return null;
146 return create(value.getTime());
147 }
148
149
150
151
152
153
154 public Double create( DateTime value ) throws ValueFormatException {
155 if (value == null) return null;
156 return create(value.getMilliseconds());
157 }
158
159
160
161
162 public Double create( Name value ) {
163 throw new ValueFormatException(value, getPropertyType(), GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
164 Name.class.getSimpleName(),
165 value));
166 }
167
168
169
170
171 public Double create( Path value ) {
172 throw new ValueFormatException(value, getPropertyType(), GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
173 Path.class.getSimpleName(),
174 value));
175 }
176
177
178
179
180 public Double create( Path.Segment value ) {
181 throw new ValueFormatException(value, getPropertyType(),
182 GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
183 Path.Segment.class.getSimpleName(),
184 value));
185 }
186
187
188
189
190 public Double create( Reference value ) {
191 throw new ValueFormatException(value, getPropertyType(),
192 GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
193 Reference.class.getSimpleName(),
194 value));
195 }
196
197
198
199
200 public Double create( URI value ) {
201 throw new ValueFormatException(value, getPropertyType(), GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
202 URI.class.getSimpleName(),
203 value));
204 }
205
206
207
208
209
210
211 public Double create( UUID value ) {
212 throw new ValueFormatException(value, getPropertyType(), GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
213 UUID.class.getSimpleName(),
214 value));
215 }
216
217
218
219
220 public Double create( byte[] value ) {
221
222 return create(getStringValueFactory().create(value));
223 }
224
225
226
227
228
229
230 public Double create( Binary value ) throws ValueFormatException, IoException {
231
232 return create(getStringValueFactory().create(value));
233 }
234
235
236
237
238 public Double create( InputStream stream,
239 long approximateLength ) throws IoException {
240
241 return create(getStringValueFactory().create(stream, approximateLength));
242 }
243
244
245
246
247 public Double create( Reader reader,
248 long approximateLength ) throws IoException {
249
250 return create(getStringValueFactory().create(reader, approximateLength));
251 }
252
253
254
255
256 @Override
257 protected Double[] createEmptyArray( int length ) {
258 return new Double[length];
259 }
260
261 }