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.joda.time.DateTimeZone;
35 import org.modeshape.common.text.TextDecoder;
36 import org.modeshape.graph.GraphI18n;
37 import org.modeshape.graph.property.Binary;
38 import org.modeshape.graph.property.DateTime;
39 import org.modeshape.graph.property.DateTimeFactory;
40 import org.modeshape.graph.property.IoException;
41 import org.modeshape.graph.property.Name;
42 import org.modeshape.graph.property.Path;
43 import org.modeshape.graph.property.PropertyType;
44 import org.modeshape.graph.property.Reference;
45 import org.modeshape.graph.property.ValueFactory;
46 import org.modeshape.graph.property.ValueFormatException;
47
48
49
50
51 @Immutable
52 public class JodaDateTimeValueFactory extends AbstractValueFactory<DateTime> implements DateTimeFactory {
53
54 public JodaDateTimeValueFactory( TextDecoder decoder,
55 ValueFactory<String> stringValueFactory ) {
56 super(PropertyType.DATE, decoder, stringValueFactory);
57 }
58
59
60
61
62 public DateTime create( String value ) {
63 if (value == null) return null;
64 try {
65 return new JodaDateTime(value.trim());
66 } catch (IllegalArgumentException err) {
67
68 try {
69 Long longValue = Long.parseLong(value);
70 return new JodaDateTime(longValue);
71 } catch (NumberFormatException e) {
72
73 throw new ValueFormatException(value, getPropertyType(),
74 GraphI18n.errorConvertingType.text(String.class.getSimpleName(),
75 DateTime.class.getSimpleName(),
76 value), err);
77 }
78 }
79 }
80
81
82
83
84 public DateTime create( String value,
85 TextDecoder decoder ) {
86
87 return create(getDecoder(decoder).decode(value));
88 }
89
90
91
92
93 public DateTime create( int value ) {
94 return create((long)value);
95 }
96
97
98
99
100 public DateTime create( long value ) {
101 return new JodaDateTime(value);
102 }
103
104
105
106
107 public DateTime create( boolean value ) {
108 throw new ValueFormatException(value, getPropertyType(), GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
109 Date.class.getSimpleName(),
110 value));
111 }
112
113
114
115
116 public DateTime create( float value ) {
117 return create((long)value);
118 }
119
120
121
122
123 public DateTime create( double value ) {
124 return create((long)value);
125 }
126
127
128
129
130 public DateTime create( BigDecimal value ) {
131 if (value == null) return null;
132 return create(value.longValue());
133 }
134
135
136
137
138 public DateTime create( Calendar value ) {
139 if (value == null) return null;
140 return new JodaDateTime(value);
141 }
142
143
144
145
146 public DateTime create( Date value ) {
147 if (value == null) return null;
148 return new JodaDateTime(value);
149 }
150
151
152
153
154
155
156 public DateTime create( DateTime value ) throws ValueFormatException {
157 return value;
158 }
159
160
161
162
163 public DateTime create( Name value ) {
164 throw new ValueFormatException(value, getPropertyType(), GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
165 Name.class.getSimpleName(),
166 value));
167 }
168
169
170
171
172 public DateTime create( Path value ) {
173 throw new ValueFormatException(value, getPropertyType(), GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
174 Path.class.getSimpleName(),
175 value));
176 }
177
178
179
180
181 public DateTime create( Path.Segment value ) {
182 throw new ValueFormatException(value, getPropertyType(),
183 GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
184 Path.Segment.class.getSimpleName(),
185 value));
186 }
187
188
189
190
191 public DateTime create( Reference value ) {
192 throw new ValueFormatException(value, getPropertyType(),
193 GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
194 Reference.class.getSimpleName(),
195 value));
196 }
197
198
199
200
201 public DateTime create( URI value ) {
202 throw new ValueFormatException(value, getPropertyType(), GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
203 URI.class.getSimpleName(),
204 value));
205 }
206
207
208
209
210
211
212 public DateTime create( UUID value ) {
213 throw new ValueFormatException(value, getPropertyType(), GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
214 UUID.class.getSimpleName(),
215 value));
216 }
217
218
219
220
221 public DateTime create( byte[] value ) {
222
223 return create(getStringValueFactory().create(value));
224 }
225
226
227
228
229
230
231 public DateTime create( Binary value ) throws ValueFormatException, IoException {
232
233 return create(getStringValueFactory().create(value));
234 }
235
236
237
238
239 public DateTime create( InputStream stream,
240 long approximateLength ) throws IoException {
241
242 return create(getStringValueFactory().create(stream, approximateLength));
243 }
244
245
246
247
248 public DateTime create( Reader reader,
249 long approximateLength ) throws IoException {
250
251 return create(getStringValueFactory().create(reader, approximateLength));
252 }
253
254
255
256
257 public DateTime create() {
258 return new JodaDateTime();
259 }
260
261
262
263
264
265
266 public DateTime createUtc() {
267 return new JodaDateTime(DateTimeZone.UTC);
268 }
269
270
271
272
273 public DateTime create( int year,
274 int monthOfYear,
275 int dayOfMonth,
276 int hourOfDay,
277 int minuteOfHour,
278 int secondOfMinute,
279 int millisecondsOfSecond ) {
280 return new JodaDateTime(year, monthOfYear, dayOfMonth, hourOfDay, minuteOfHour, secondOfMinute, millisecondsOfSecond);
281 }
282
283
284
285
286 public DateTime create( int year,
287 int monthOfYear,
288 int dayOfMonth,
289 int hourOfDay,
290 int minuteOfHour,
291 int secondOfMinute,
292 int millisecondsOfSecond,
293 int timeZoneOffsetHours ) {
294 return new JodaDateTime(year, monthOfYear, dayOfMonth, hourOfDay, minuteOfHour, secondOfMinute, millisecondsOfSecond,
295 timeZoneOffsetHours);
296 }
297
298
299
300
301 public DateTime create( int year,
302 int monthOfYear,
303 int dayOfMonth,
304 int hourOfDay,
305 int minuteOfHour,
306 int secondOfMinute,
307 int millisecondsOfSecond,
308 String timeZoneId ) {
309 return new JodaDateTime(year, monthOfYear, dayOfMonth, hourOfDay, minuteOfHour, secondOfMinute, millisecondsOfSecond,
310 timeZoneId);
311 }
312
313
314
315
316
317
318 public DateTime create( DateTime original,
319 long offsetInMillis ) {
320 assert original != null;
321 if (offsetInMillis == 0l) return original;
322 long newMillis = original.getMilliseconds() + offsetInMillis;
323 return new JodaDateTime(newMillis, original.getTimeZoneId());
324 }
325
326
327
328
329 @Override
330 protected DateTime[] createEmptyArray( int length ) {
331 return new DateTime[length];
332 }
333 }