View Javadoc

1   /*
2    * ModeShape (http://www.modeshape.org)
3    * See the COPYRIGHT.txt file distributed with this work for information
4    * regarding copyright ownership.  Some portions may be licensed
5    * to Red Hat, Inc. under one or more contributor license agreements.
6    * See the AUTHORS.txt file in the distribution for a full listing of 
7    * individual contributors. 
8    *
9    * ModeShape is free software. Unless otherwise indicated, all code in ModeShape
10   * is licensed to you under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation; either version 2.1 of
12   * the License, or (at your option) any later version.
13   *
14   * ModeShape is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17   * Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this software; if not, write to the Free
21   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
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   * The standard {@link ValueFactory} for {@link PropertyType#DATE} values.
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       * {@inheritDoc}
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              // See if this string represents a LONG value ...
68              try {
69                  Long longValue = Long.parseLong(value);
70                  return new JodaDateTime(longValue);
71              } catch (NumberFormatException e) {
72                  // Guess it wasn't a long value ...
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       * {@inheritDoc}
83       */
84      public DateTime create( String value,
85                              TextDecoder decoder ) {
86          // this probably doesn't really need to call the decoder, but by doing so then we don't care at all what the decoder does
87          return create(getDecoder(decoder).decode(value));
88      }
89  
90      /**
91       * {@inheritDoc}
92       */
93      public DateTime create( int value ) {
94          return create((long)value);
95      }
96  
97      /**
98       * {@inheritDoc}
99       */
100     public DateTime create( long value ) {
101         return new JodaDateTime(value);
102     }
103 
104     /**
105      * {@inheritDoc}
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      * {@inheritDoc}
115      */
116     public DateTime create( float value ) {
117         return create((long)value);
118     }
119 
120     /**
121      * {@inheritDoc}
122      */
123     public DateTime create( double value ) {
124         return create((long)value);
125     }
126 
127     /**
128      * {@inheritDoc}
129      */
130     public DateTime create( BigDecimal value ) {
131         if (value == null) return null;
132         return create(value.longValue());
133     }
134 
135     /**
136      * {@inheritDoc}
137      */
138     public DateTime create( Calendar value ) {
139         if (value == null) return null;
140         return new JodaDateTime(value);
141     }
142 
143     /**
144      * {@inheritDoc}
145      */
146     public DateTime create( Date value ) {
147         if (value == null) return null;
148         return new JodaDateTime(value);
149     }
150 
151     /**
152      * {@inheritDoc}
153      * 
154      * @see org.modeshape.graph.property.ValueFactory#create(org.modeshape.graph.property.DateTime)
155      */
156     public DateTime create( DateTime value ) throws ValueFormatException {
157         return value;
158     }
159 
160     /**
161      * {@inheritDoc}
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      * {@inheritDoc}
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      * {@inheritDoc}
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      * {@inheritDoc}
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      * {@inheritDoc}
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      * {@inheritDoc}
209      * 
210      * @see org.modeshape.graph.property.ValueFactory#create(java.util.UUID)
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      * {@inheritDoc}
220      */
221     public DateTime create( byte[] value ) {
222         // First attempt to create a string from the value, then a long from the string ...
223         return create(getStringValueFactory().create(value));
224     }
225 
226     /**
227      * {@inheritDoc}
228      * 
229      * @see org.modeshape.graph.property.ValueFactory#create(org.modeshape.graph.property.Binary)
230      */
231     public DateTime create( Binary value ) throws ValueFormatException, IoException {
232         // First create a string and then create the boolean from the string value ...
233         return create(getStringValueFactory().create(value));
234     }
235 
236     /**
237      * {@inheritDoc}
238      */
239     public DateTime create( InputStream stream,
240                             long approximateLength ) throws IoException {
241         // First attempt to create a string from the value, then a double from the string ...
242         return create(getStringValueFactory().create(stream, approximateLength));
243     }
244 
245     /**
246      * {@inheritDoc}
247      */
248     public DateTime create( Reader reader,
249                             long approximateLength ) throws IoException {
250         // First attempt to create a string from the value, then a double from the string ...
251         return create(getStringValueFactory().create(reader, approximateLength));
252     }
253 
254     /**
255      * {@inheritDoc}
256      */
257     public DateTime create() {
258         return new JodaDateTime();
259     }
260 
261     /**
262      * {@inheritDoc}
263      * 
264      * @see org.modeshape.graph.property.DateTimeFactory#createUtc()
265      */
266     public DateTime createUtc() {
267         return new JodaDateTime(DateTimeZone.UTC);
268     }
269 
270     /**
271      * {@inheritDoc}
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      * {@inheritDoc}
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      * {@inheritDoc}
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      * {@inheritDoc}
315      * 
316      * @see org.modeshape.graph.property.DateTimeFactory#create(org.modeshape.graph.property.DateTime, long)
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      * {@inheritDoc}
328      */
329     @Override
330     protected DateTime[] createEmptyArray( int length ) {
331         return new DateTime[length];
332     }
333 }