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