001 /*
002 * JBoss DNA (http://www.jboss.org/dna)
003 * See the COPYRIGHT.txt file distributed with this work for information
004 * regarding copyright ownership. Some portions may be licensed
005 * to Red Hat, Inc. under one or more contributor license agreements.
006 * See the AUTHORS.txt file in the distribution for a full listing of
007 * individual contributors.
008 *
009 * JBoss DNA is free software. Unless otherwise indicated, all code in JBoss DNA
010 * is licensed to you under the terms of the GNU Lesser General Public License as
011 * published by the Free Software Foundation; either version 2.1 of
012 * the License, or (at your option) any later version.
013 *
014 * JBoss DNA is distributed in the hope that it will be useful,
015 * but WITHOUT ANY WARRANTY; without even the implied warranty of
016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017 * Lesser General Public License for more details.
018 *
019 * You should have received a copy of the GNU Lesser General Public
020 * License along with this software; if not, write to the Free
021 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
022 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
023 */
024 package org.jboss.dna.graph.property.basic;
025
026 import java.io.InputStream;
027 import java.io.Reader;
028 import java.math.BigDecimal;
029 import java.net.URI;
030 import java.util.Calendar;
031 import java.util.Date;
032 import java.util.UUID;
033 import net.jcip.annotations.Immutable;
034 import org.jboss.dna.common.text.TextDecoder;
035 import org.jboss.dna.graph.GraphI18n;
036 import org.jboss.dna.graph.property.Binary;
037 import org.jboss.dna.graph.property.DateTime;
038 import org.jboss.dna.graph.property.DateTimeFactory;
039 import org.jboss.dna.graph.property.IoException;
040 import org.jboss.dna.graph.property.Name;
041 import org.jboss.dna.graph.property.Path;
042 import org.jboss.dna.graph.property.PropertyType;
043 import org.jboss.dna.graph.property.Reference;
044 import org.jboss.dna.graph.property.ValueFactory;
045 import org.jboss.dna.graph.property.ValueFormatException;
046 import org.joda.time.DateTimeZone;
047
048 /**
049 * The standard {@link ValueFactory} for {@link PropertyType#DATE} values.
050 *
051 * @author Randall Hauch
052 * @author John Verhaeg
053 */
054 @Immutable
055 public class JodaDateTimeValueFactory extends AbstractValueFactory<DateTime> implements DateTimeFactory {
056
057 public JodaDateTimeValueFactory( TextDecoder decoder,
058 ValueFactory<String> stringValueFactory ) {
059 super(PropertyType.DATE, decoder, stringValueFactory);
060 }
061
062 /**
063 * {@inheritDoc}
064 */
065 public DateTime create( String value ) {
066 if (value == null) return null;
067 try {
068 return new JodaDateTime(value.trim());
069 } catch (IllegalArgumentException err) {
070 throw new ValueFormatException(value, getPropertyType(),
071 GraphI18n.errorConvertingType.text(String.class.getSimpleName(),
072 DateTime.class.getSimpleName(),
073 value), err);
074 }
075 }
076
077 /**
078 * {@inheritDoc}
079 */
080 public DateTime create( String value,
081 TextDecoder decoder ) {
082 // 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
083 return create(getDecoder(decoder).decode(value));
084 }
085
086 /**
087 * {@inheritDoc}
088 */
089 public DateTime create( int value ) {
090 return create((long)value);
091 }
092
093 /**
094 * {@inheritDoc}
095 */
096 public DateTime create( long value ) {
097 return new JodaDateTime(value);
098 }
099
100 /**
101 * {@inheritDoc}
102 */
103 public DateTime create( boolean value ) {
104 throw new ValueFormatException(value, getPropertyType(), GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
105 Date.class.getSimpleName(),
106 value));
107 }
108
109 /**
110 * {@inheritDoc}
111 */
112 public DateTime create( float value ) {
113 return create((long)value);
114 }
115
116 /**
117 * {@inheritDoc}
118 */
119 public DateTime create( double value ) {
120 return create((long)value);
121 }
122
123 /**
124 * {@inheritDoc}
125 */
126 public DateTime create( BigDecimal value ) {
127 if (value == null) return null;
128 return create(value.longValue());
129 }
130
131 /**
132 * {@inheritDoc}
133 */
134 public DateTime create( Calendar value ) {
135 if (value == null) return null;
136 return new JodaDateTime(value);
137 }
138
139 /**
140 * {@inheritDoc}
141 */
142 public DateTime create( Date value ) {
143 if (value == null) return null;
144 return new JodaDateTime(value);
145 }
146
147 /**
148 * {@inheritDoc}
149 *
150 * @see org.jboss.dna.graph.property.ValueFactory#create(org.jboss.dna.graph.property.DateTime)
151 */
152 public DateTime create( DateTime value ) throws ValueFormatException {
153 return value;
154 }
155
156 /**
157 * {@inheritDoc}
158 */
159 public DateTime create( Name value ) {
160 throw new ValueFormatException(value, getPropertyType(), GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
161 Name.class.getSimpleName(),
162 value));
163 }
164
165 /**
166 * {@inheritDoc}
167 */
168 public DateTime create( Path value ) {
169 throw new ValueFormatException(value, getPropertyType(), GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
170 Path.class.getSimpleName(),
171 value));
172 }
173
174 /**
175 * {@inheritDoc}
176 */
177 public DateTime create( Reference value ) {
178 throw new ValueFormatException(value, getPropertyType(),
179 GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
180 Reference.class.getSimpleName(),
181 value));
182 }
183
184 /**
185 * {@inheritDoc}
186 */
187 public DateTime create( URI value ) {
188 throw new ValueFormatException(value, getPropertyType(), GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
189 URI.class.getSimpleName(),
190 value));
191 }
192
193 /**
194 * {@inheritDoc}
195 *
196 * @see org.jboss.dna.graph.property.ValueFactory#create(java.util.UUID)
197 */
198 public DateTime create( UUID value ) {
199 throw new ValueFormatException(value, getPropertyType(), GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
200 UUID.class.getSimpleName(),
201 value));
202 }
203
204 /**
205 * {@inheritDoc}
206 */
207 public DateTime create( byte[] value ) {
208 // First attempt to create a string from the value, then a long from the string ...
209 return create(getStringValueFactory().create(value));
210 }
211
212 /**
213 * {@inheritDoc}
214 *
215 * @see org.jboss.dna.graph.property.ValueFactory#create(org.jboss.dna.graph.property.Binary)
216 */
217 public DateTime create( Binary value ) throws ValueFormatException, IoException {
218 // First create a string and then create the boolean from the string value ...
219 return create(getStringValueFactory().create(value));
220 }
221
222 /**
223 * {@inheritDoc}
224 */
225 public DateTime create( InputStream stream,
226 long approximateLength ) throws IoException {
227 // First attempt to create a string from the value, then a double from the string ...
228 return create(getStringValueFactory().create(stream, approximateLength));
229 }
230
231 /**
232 * {@inheritDoc}
233 */
234 public DateTime create( Reader reader,
235 long approximateLength ) throws IoException {
236 // First attempt to create a string from the value, then a double from the string ...
237 return create(getStringValueFactory().create(reader, approximateLength));
238 }
239
240 /**
241 * {@inheritDoc}
242 */
243 public DateTime create() {
244 return new JodaDateTime();
245 }
246
247 /**
248 * {@inheritDoc}
249 *
250 * @see org.jboss.dna.graph.property.DateTimeFactory#createUtc()
251 */
252 public DateTime createUtc() {
253 return new JodaDateTime(DateTimeZone.UTC);
254 }
255
256 /**
257 * {@inheritDoc}
258 */
259 public DateTime create( int year,
260 int monthOfYear,
261 int dayOfMonth,
262 int hourOfDay,
263 int minuteOfHour,
264 int secondOfMinute,
265 int millisecondsOfSecond ) {
266 return new JodaDateTime(year, monthOfYear, dayOfMonth, hourOfDay, minuteOfHour, secondOfMinute, millisecondsOfSecond);
267 }
268
269 /**
270 * {@inheritDoc}
271 */
272 public DateTime create( int year,
273 int monthOfYear,
274 int dayOfMonth,
275 int hourOfDay,
276 int minuteOfHour,
277 int secondOfMinute,
278 int millisecondsOfSecond,
279 int timeZoneOffsetHours ) {
280 return new JodaDateTime(year, monthOfYear, dayOfMonth, hourOfDay, minuteOfHour, secondOfMinute, millisecondsOfSecond,
281 timeZoneOffsetHours);
282 }
283
284 /**
285 * {@inheritDoc}
286 */
287 public DateTime create( int year,
288 int monthOfYear,
289 int dayOfMonth,
290 int hourOfDay,
291 int minuteOfHour,
292 int secondOfMinute,
293 int millisecondsOfSecond,
294 String timeZoneId ) {
295 return new JodaDateTime(year, monthOfYear, dayOfMonth, hourOfDay, minuteOfHour, secondOfMinute, millisecondsOfSecond,
296 timeZoneId);
297 }
298
299 /**
300 * {@inheritDoc}
301 *
302 * @see org.jboss.dna.graph.property.DateTimeFactory#create(org.jboss.dna.graph.property.DateTime, long)
303 */
304 public DateTime create( DateTime original,
305 long offsetInMillis ) {
306 assert original != null;
307 if (offsetInMillis == 0l) return original;
308 long newMillis = original.getMilliseconds() + offsetInMillis;
309 return new JodaDateTime(newMillis, original.getTimeZoneId());
310 }
311
312 /**
313 * {@inheritDoc}
314 */
315 @Override
316 protected DateTime[] createEmptyArray( int length ) {
317 return new DateTime[length];
318 }
319 }