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