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