1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
48
49 @Immutable
50 public class LongValueFactory extends AbstractValueFactory<Long> {
51
52 public LongValueFactory( TextDecoder decoder,
53 ValueFactory<String> stringValueFactory ) {
54 super(PropertyType.LONG, decoder, stringValueFactory);
55 }
56
57
58
59
60 public Long create( String value ) {
61 if (value == null) return null;
62 try {
63 return Long.valueOf(value.trim());
64 } catch (NumberFormatException err) {
65 throw new ValueFormatException(value, getPropertyType(),
66 GraphI18n.errorConvertingType.text(String.class.getSimpleName(),
67 Long.class.getSimpleName(),
68 value), err);
69 }
70 }
71
72
73
74
75 public Long create( String value,
76 TextDecoder decoder ) {
77
78 return create(getDecoder(decoder).decode(value));
79 }
80
81
82
83
84 public Long create( int value ) {
85 return Long.valueOf(value);
86 }
87
88
89
90
91 public Long create( long value ) {
92 return value;
93 }
94
95
96
97
98 public Long create( boolean value ) {
99 throw new ValueFormatException(value, getPropertyType(), GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
100 Long.class.getSimpleName(),
101 value));
102 }
103
104
105
106
107 public Long create( float value ) {
108 return (long)value;
109 }
110
111
112
113
114 public Long create( double value ) {
115 return (long)value;
116 }
117
118
119
120
121 public Long create( BigDecimal value ) {
122 if (value == null) return null;
123 return value.longValue();
124 }
125
126
127
128
129 public Long create( Calendar value ) {
130 if (value == null) return null;
131 return value.getTimeInMillis();
132 }
133
134
135
136
137 public Long create( Date value ) {
138 if (value == null) return null;
139 return value.getTime();
140 }
141
142
143
144
145
146
147 public Long create( DateTime value ) throws ValueFormatException {
148 if (value == null) return null;
149 return value.getMilliseconds();
150 }
151
152
153
154
155 public Long create( Name value ) {
156 throw new ValueFormatException(value, getPropertyType(), GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
157 Name.class.getSimpleName(),
158 value));
159 }
160
161
162
163
164 public Long create( Path value ) {
165 throw new ValueFormatException(value, getPropertyType(), GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
166 Path.class.getSimpleName(),
167 value));
168 }
169
170
171
172
173 public Long 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
182
183 public Long create( URI value ) {
184 throw new ValueFormatException(value, getPropertyType(), GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
185 URI.class.getSimpleName(),
186 value));
187 }
188
189
190
191
192
193
194 public Long 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
202
203 public Long create( byte[] value ) {
204
205 return create(getStringValueFactory().create(value));
206 }
207
208
209
210
211
212
213 public Long create( Binary value ) throws ValueFormatException, IoException {
214
215 return create(getStringValueFactory().create(value));
216 }
217
218
219
220
221 public Long create( InputStream stream,
222 long approximateLength ) throws IoException {
223
224 return create(getStringValueFactory().create(stream, approximateLength));
225 }
226
227
228
229
230 public Long create( Reader reader,
231 long approximateLength ) throws IoException {
232
233 return create(getStringValueFactory().create(reader, approximateLength));
234 }
235
236
237
238
239 @Override
240 protected Long[] createEmptyArray( int length ) {
241 return new Long[length];
242 }
243
244 }