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.math.BigDecimal;
27 import java.net.URI;
28 import java.util.HashMap;
29 import java.util.Map;
30 import net.jcip.annotations.Immutable;
31 import org.modeshape.common.text.TextDecoder;
32 import org.modeshape.common.text.TextEncoder;
33 import org.modeshape.common.util.CheckArg;
34 import org.modeshape.graph.property.BinaryFactory;
35 import org.modeshape.graph.property.DateTimeFactory;
36 import org.modeshape.graph.property.NameFactory;
37 import org.modeshape.graph.property.NamespaceRegistry;
38 import org.modeshape.graph.property.PathFactory;
39 import org.modeshape.graph.property.PropertyType;
40 import org.modeshape.graph.property.Reference;
41 import org.modeshape.graph.property.UuidFactory;
42 import org.modeshape.graph.property.ValueFactory;
43 import org.modeshape.graph.property.ValueTypeSystem;
44 import org.modeshape.graph.query.model.TypeSystem;
45
46
47
48
49 @Immutable
50 public class StandardValueFactories extends AbstractValueFactories {
51
52
53 private final ValueFactory<String> stringFactory;
54 private final BinaryFactory binaryFactory;
55 private final ValueFactory<Boolean> booleanFactory;
56 private final DateTimeFactory dateFactory;
57 private final ValueFactory<BigDecimal> decimalFactory;
58 private final ValueFactory<Double> doubleFactory;
59 private final ValueFactory<Long> longFactory;
60 private final NameFactory nameFactory;
61 private final PathFactory pathFactory;
62 private final ValueFactory<Reference> referenceFactory;
63 private final ValueFactory<URI> uriFactory;
64 private final UuidFactory uuidFactory;
65 private final ValueFactory<Object> objectFactory;
66
67 private final NamespaceRegistry namespaceRegistry;
68
69 private final TypeSystem typeSystem;
70
71
72
73
74
75
76
77 public StandardValueFactories( NamespaceRegistry namespaceRegistry ) {
78 this(namespaceRegistry, null, null);
79 }
80
81
82
83
84
85
86
87
88
89
90
91 public StandardValueFactories( NamespaceRegistry namespaceRegistry,
92 TextDecoder decoder,
93 TextEncoder encoder,
94 ValueFactory<?>... extraFactories ) {
95 CheckArg.isNotNull(namespaceRegistry, "namespaceRegistry");
96 this.namespaceRegistry = namespaceRegistry;
97 decoder = decoder != null ? decoder : ValueFactory.DEFAULT_DECODER;
98 encoder = encoder != null ? encoder : ValueFactory.DEFAULT_ENCODER;
99 Map<PropertyType, ValueFactory<?>> factories = new HashMap<PropertyType, ValueFactory<?>>();
100
101
102 for (ValueFactory<?> factory : extraFactories) {
103 if (factory == null) continue;
104 factories.put(factory.getPropertyType(), factory);
105 }
106
107
108 this.stringFactory = getFactory(factories, new StringValueFactory(this.namespaceRegistry, decoder, encoder));
109
110
111 StringValueFactory stringFactoryWithoutNamespaces = new StringValueFactory(decoder, encoder);
112 this.binaryFactory = (BinaryFactory)getFactory(factories, new InMemoryBinaryValueFactory(decoder,
113 stringFactoryWithoutNamespaces));
114 this.booleanFactory = getFactory(factories, new BooleanValueFactory(decoder, this.stringFactory));
115 this.dateFactory = (DateTimeFactory)getFactory(factories, new JodaDateTimeValueFactory(decoder, this.stringFactory));
116 this.decimalFactory = getFactory(factories, new DecimalValueFactory(decoder, this.stringFactory));
117 this.doubleFactory = getFactory(factories, new DoubleValueFactory(decoder, this.stringFactory));
118 this.longFactory = getFactory(factories, new LongValueFactory(decoder, this.stringFactory));
119 this.nameFactory = (NameFactory)getFactory(factories, new NameValueFactory(this.namespaceRegistry, decoder,
120 this.stringFactory));
121 this.pathFactory = (PathFactory)getFactory(factories, new PathValueFactory(decoder, this.stringFactory, this.nameFactory));
122 this.referenceFactory = getFactory(factories, new ReferenceValueFactory(decoder, this.stringFactory));
123 this.uuidFactory = (UuidFactory)getFactory(factories, new UuidValueFactory(decoder, this.stringFactory));
124 this.uriFactory = getFactory(factories, new UriValueFactory(this.namespaceRegistry, decoder, this.stringFactory));
125 this.objectFactory = getFactory(factories, new ObjectValueFactory(decoder, this.stringFactory, this.binaryFactory));
126
127 this.typeSystem = new ValueTypeSystem(this);
128 }
129
130 @SuppressWarnings( "unchecked" )
131 private static <T> ValueFactory<T> getFactory( Map<PropertyType, ValueFactory<?>> factories,
132 ValueFactory<T> defaultFactory ) {
133 PropertyType type = defaultFactory.getPropertyType();
134 ValueFactory<?> factory = factories.get(type);
135 if (factory == null) {
136 factory = defaultFactory;
137 factories.put(type, factory);
138 }
139 return (ValueFactory<T>)factory;
140 }
141
142
143
144
145
146
147 public TypeSystem getTypeSystem() {
148 return typeSystem;
149 }
150
151
152
153
154 public NamespaceRegistry getNamespaceRegistry() {
155 return this.namespaceRegistry;
156 }
157
158
159
160
161 public BinaryFactory getBinaryFactory() {
162 return this.binaryFactory;
163 }
164
165
166
167
168 public ValueFactory<Boolean> getBooleanFactory() {
169 return this.booleanFactory;
170 }
171
172
173
174
175 public DateTimeFactory getDateFactory() {
176 return this.dateFactory;
177 }
178
179
180
181
182 public ValueFactory<BigDecimal> getDecimalFactory() {
183 return this.decimalFactory;
184 }
185
186
187
188
189 public ValueFactory<Double> getDoubleFactory() {
190 return this.doubleFactory;
191 }
192
193
194
195
196 public ValueFactory<Long> getLongFactory() {
197 return this.longFactory;
198 }
199
200
201
202
203 public NameFactory getNameFactory() {
204 return this.nameFactory;
205 }
206
207
208
209
210 public PathFactory getPathFactory() {
211 return this.pathFactory;
212 }
213
214
215
216
217 public ValueFactory<Reference> getReferenceFactory() {
218 return this.referenceFactory;
219 }
220
221
222
223
224 public ValueFactory<String> getStringFactory() {
225 return this.stringFactory;
226 }
227
228
229
230
231 public ValueFactory<URI> getUriFactory() {
232 return this.uriFactory;
233 }
234
235
236
237
238
239
240 public UuidFactory getUuidFactory() {
241 return this.uuidFactory;
242 }
243
244
245
246
247 public ValueFactory<Object> getObjectFactory() {
248 return this.objectFactory;
249 }
250
251 }