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