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.IoException;
037    import org.jboss.dna.graph.properties.Name;
038    import org.jboss.dna.graph.properties.Path;
039    import org.jboss.dna.graph.properties.PropertyType;
040    import org.jboss.dna.graph.properties.Reference;
041    import org.jboss.dna.graph.properties.ValueFactory;
042    import org.jboss.dna.graph.properties.ValueFormatException;
043    
044    /**
045     * The standard {@link ValueFactory} for {@link PropertyType#LONG} values.
046     * 
047     * @author Randall Hauch
048     * @author John Verhaeg
049     */
050    @Immutable
051    public class LongValueFactory extends AbstractValueFactory<Long> {
052    
053        public LongValueFactory( TextDecoder decoder,
054                                 ValueFactory<String> stringValueFactory ) {
055            super(PropertyType.LONG, decoder, stringValueFactory);
056        }
057    
058        /**
059         * {@inheritDoc}
060         */
061        public Long create( String value ) {
062            if (value == null) return null;
063            try {
064                return Long.valueOf(value.trim());
065            } catch (NumberFormatException err) {
066                throw new ValueFormatException(value, getPropertyType(),
067                                               GraphI18n.errorConvertingType.text(String.class.getSimpleName(),
068                                                                                Long.class.getSimpleName(),
069                                                                                value), err);
070            }
071        }
072    
073        /**
074         * {@inheritDoc}
075         */
076        public Long create( String value,
077                            TextDecoder decoder ) {
078            // 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
079            return create(getDecoder(decoder).decode(value));
080        }
081    
082        /**
083         * {@inheritDoc}
084         */
085        public Long create( int value ) {
086            return Long.valueOf(value);
087        }
088    
089        /**
090         * {@inheritDoc}
091         */
092        public Long create( long value ) {
093            return value;
094        }
095    
096        /**
097         * {@inheritDoc}
098         */
099        public Long create( boolean value ) {
100            throw new ValueFormatException(value, getPropertyType(), GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
101                                                                                                      Long.class.getSimpleName(),
102                                                                                                      value));
103        }
104    
105        /**
106         * {@inheritDoc}
107         */
108        public Long create( float value ) {
109            return (long)value;
110        }
111    
112        /**
113         * {@inheritDoc}
114         */
115        public Long create( double value ) {
116            return (long)value;
117        }
118    
119        /**
120         * {@inheritDoc}
121         */
122        public Long create( BigDecimal value ) {
123            if (value == null) return null;
124            return value.longValue();
125        }
126    
127        /**
128         * {@inheritDoc}
129         */
130        public Long create( Calendar value ) {
131            if (value == null) return null;
132            return value.getTimeInMillis();
133        }
134    
135        /**
136         * {@inheritDoc}
137         */
138        public Long create( Date value ) {
139            if (value == null) return null;
140            return value.getTime();
141        }
142    
143        /**
144         * {@inheritDoc}
145         * 
146         * @see org.jboss.dna.graph.properties.ValueFactory#create(org.jboss.dna.graph.properties.DateTime)
147         */
148        public Long create( DateTime value ) throws ValueFormatException {
149            if (value == null) return null;
150            return value.getMilliseconds();
151        }
152    
153        /**
154         * {@inheritDoc}
155         */
156        public Long create( Name value ) {
157            throw new ValueFormatException(value, getPropertyType(), GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
158                                                                                                      Name.class.getSimpleName(),
159                                                                                                      value));
160        }
161    
162        /**
163         * {@inheritDoc}
164         */
165        public Long create( Path value ) {
166            throw new ValueFormatException(value, getPropertyType(), GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
167                                                                                                      Path.class.getSimpleName(),
168                                                                                                      value));
169        }
170    
171        /**
172         * {@inheritDoc}
173         */
174        public Long create( Reference value ) {
175            throw new ValueFormatException(value, getPropertyType(),
176                                           GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
177                                                                            Reference.class.getSimpleName(),
178                                                                            value));
179        }
180    
181        /**
182         * {@inheritDoc}
183         */
184        public Long create( URI value ) {
185            throw new ValueFormatException(value, getPropertyType(), GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
186                                                                                                      URI.class.getSimpleName(),
187                                                                                                      value));
188        }
189    
190        /**
191         * {@inheritDoc}
192         * 
193         * @see org.jboss.dna.graph.properties.ValueFactory#create(java.util.UUID)
194         */
195        public Long create( UUID value ) throws IoException {
196            throw new ValueFormatException(value, getPropertyType(), GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
197                                                                                                      UUID.class.getSimpleName(),
198                                                                                                      value));
199        }
200    
201        /**
202         * {@inheritDoc}
203         */
204        public Long create( byte[] value ) {
205            // First attempt to create a string from the value, then a long from the string ...
206            return create(getStringValueFactory().create(value));
207        }
208    
209        /**
210         * {@inheritDoc}
211         * 
212         * @see org.jboss.dna.graph.properties.ValueFactory#create(org.jboss.dna.graph.properties.Binary)
213         */
214        public Long create( Binary value ) throws ValueFormatException, IoException {
215            // First create a string and then create the boolean from the string value ...
216            return create(getStringValueFactory().create(value));
217        }
218    
219        /**
220         * {@inheritDoc}
221         */
222        public Long create( InputStream stream,
223                            long approximateLength ) throws IoException {
224            // First attempt to create a string from the value, then a long from the string ...
225            return create(getStringValueFactory().create(stream, approximateLength));
226        }
227    
228        /**
229         * {@inheritDoc}
230         */
231        public Long create( Reader reader,
232                            long approximateLength ) throws IoException {
233            // First attempt to create a string from the value, then a long from the string ...
234            return create(getStringValueFactory().create(reader, approximateLength));
235        }
236    
237        /**
238         * {@inheritDoc}
239         */
240        @Override
241        protected Long[] createEmptyArray( int length ) {
242            return new Long[length];
243        }
244    
245    }