View Javadoc

1   /*
2    * ModeShape (http://www.modeshape.org)
3    * See the COPYRIGHT.txt file distributed with this work for information
4    * regarding copyright ownership.  Some portions may be licensed
5    * to Red Hat, Inc. under one or more contributor license agreements.
6   * See the AUTHORS.txt file in the distribution for a full listing of 
7   * individual contributors.
8    *
9    * ModeShape is free software. Unless otherwise indicated, all code in ModeShape
10   * is licensed to you under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation; either version 2.1 of
12   * the License, or (at your option) any later version.
13   *
14   * ModeShape is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17   * Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this software; if not, write to the Free
21   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23   */
24  package org.modeshape.graph.property.basic;
25  
26  import java.io.InputStream;
27  import java.io.Reader;
28  import java.math.BigDecimal;
29  import java.net.URI;
30  import java.util.Calendar;
31  import java.util.Date;
32  import java.util.UUID;
33  import net.jcip.annotations.Immutable;
34  import org.modeshape.common.text.TextDecoder;
35  import org.modeshape.graph.GraphI18n;
36  import org.modeshape.graph.property.Binary;
37  import org.modeshape.graph.property.DateTime;
38  import org.modeshape.graph.property.IoException;
39  import org.modeshape.graph.property.Name;
40  import org.modeshape.graph.property.Path;
41  import org.modeshape.graph.property.PropertyType;
42  import org.modeshape.graph.property.Reference;
43  import org.modeshape.graph.property.ValueFactory;
44  import org.modeshape.graph.property.ValueFormatException;
45  
46  /**
47   * The standard {@link ValueFactory} for {@link PropertyType#DECIMAL} values.
48   */
49  @Immutable
50  public class DecimalValueFactory extends AbstractValueFactory<BigDecimal> {
51  
52      public DecimalValueFactory( TextDecoder decoder,
53                                  ValueFactory<String> stringValueFactory ) {
54          super(PropertyType.DECIMAL, decoder, stringValueFactory);
55      }
56  
57      /**
58       * {@inheritDoc}
59       */
60      public BigDecimal create( String value ) {
61          if (value == null) return null;
62          try {
63              return new BigDecimal(value.trim());
64          } catch (NumberFormatException err) {
65              throw new ValueFormatException(value, getPropertyType(),
66                                             GraphI18n.errorConvertingType.text(String.class.getSimpleName(),
67                                                                                BigDecimal.class.getSimpleName(),
68                                                                                value), err);
69          }
70      }
71  
72      /**
73       * {@inheritDoc}
74       */
75      public BigDecimal create( String value,
76                                TextDecoder decoder ) {
77          // 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
78          return create(getDecoder(decoder).decode(value.trim()));
79      }
80  
81      /**
82       * {@inheritDoc}
83       */
84      public BigDecimal create( int value ) {
85          return BigDecimal.valueOf(value);
86      }
87  
88      /**
89       * {@inheritDoc}
90       */
91      public BigDecimal create( long value ) {
92          return BigDecimal.valueOf(value);
93      }
94  
95      /**
96       * {@inheritDoc}
97       */
98      public BigDecimal create( boolean value ) {
99          throw new ValueFormatException(value, getPropertyType(),
100                                        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.modeshape.graph.property.ValueFactory#create(org.modeshape.graph.property.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( Path.Segment value ) {
174         throw new ValueFormatException(value, getPropertyType(),
175                                        GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
176                                                                           Path.Segment.class.getSimpleName(),
177                                                                           value));
178     }
179 
180     /**
181      * {@inheritDoc}
182      */
183     public BigDecimal 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 BigDecimal 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.modeshape.graph.property.ValueFactory#create(java.util.UUID)
203      */
204     public BigDecimal create( UUID value ) throws IoException {
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 BigDecimal 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.modeshape.graph.property.ValueFactory#create(org.modeshape.graph.property.Binary)
222      */
223     public BigDecimal 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 BigDecimal 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 BigDecimal 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 BigDecimal[] createEmptyArray( int length ) {
251         return new BigDecimal[length];
252     }
253 
254 }