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