-
Notifications
You must be signed in to change notification settings - Fork 14
/
AbstractIdentifiableType.java
356 lines (291 loc) · 12.3 KB
/
AbstractIdentifiableType.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
package cz.cvut.kbss.jopa.model.metamodel;
import java.util.*;
import java.util.stream.Collectors;
abstract class AbstractIdentifiableType<X> implements IdentifiableType<X> {
private final Class<X> javaType;
private Identifier identifier;
private AbstractIdentifiableType<? super X> supertype;
private TypesSpecification<X, ?> directTypes;
private PropertiesSpecification<X, ?, ?, ?> properties;
private final Map<String, Attribute<X, ?>> declaredAttributes = new HashMap<>();
AbstractIdentifiableType(Class<X> javaType) {
this.javaType = javaType;
}
void addDeclaredAttribute(final String name, final Attribute<X, ?> a) {
declaredAttributes.put(name, a);
}
void setSupertype(AbstractIdentifiableType<? super X> supertype) {
this.supertype = supertype;
}
void addDirectTypes(TypesSpecification<X, ?> a) {
this.directTypes = a;
}
void addOtherProperties(PropertiesSpecification<X, ?, ?, ?> a) {
this.properties = a;
}
public void setIdentifier(final Identifier identifier) {
this.identifier = identifier;
}
@Override
public boolean hasSingleIdAttribute() {
return true; // We do not support id classes
}
@Override
public <Y> SingularAttribute<X, Y> getDeclaredVersion(Class<Y> type) {
// TODO
throw new UnsupportedOperationException();
}
@Override
public IdentifiableType<? super X> getSupertype() {
return supertype;
}
@Override
public <Y> SingularAttribute<? super X, Y> getVersion(Class<Y> type) {
// TODO
throw new UnsupportedOperationException();
}
@Override
public boolean hasVersionAttribute() {
return false;
}
@Override
public Set<Attribute<? super X, ?>> getAttributes() {
final Set<Attribute<? super X, ?>> attributes = new HashSet<>(declaredAttributes.values());
if (supertype != null) {
attributes.addAll(supertype.getAttributes());
}
return attributes;
}
@Override
public Attribute<? super X, ?> getAttribute(String name) {
Objects.requireNonNull(name);
if (declaredAttributes.containsKey(name)) {
return declaredAttributes.get(name);
}
if (supertype != null) {
return supertype.getAttribute(name);
}
throw new IllegalArgumentException("Attribute " + name + " is not present in type " + this.toString());
}
@Override
public CollectionAttribute<? super X, ?> getCollection(String name) {
return getCollection(name, Object.class);
}
@Override
public <E> CollectionAttribute<? super X, E> getCollection(String name, Class<E> elementType) {
return getPluralAttribute("Collection", name, elementType, CollectionAttribute.class);
}
private <E, R extends PluralAttribute<? super X, ?, E>> R getPluralAttribute(String type, String name,
Class<E> elementType,
Class<R> attType) {
final Attribute<? super X, ?> a = getAttribute(name);
checkPluralAttribute(a, type, name, elementType, attType, false);
return attType.cast(a);
}
private <E, R extends PluralAttribute<? super X, ?, E>> void checkPluralAttribute(Attribute<? super X, ?> att,
String type, String name,
Class<E> elementType,
Class<R> attType,
boolean declared) {
if (!attType.isAssignableFrom(att.getClass())) {
throw pluralAttNotFound(type, name, elementType, declared);
}
final PluralAttribute<? super X, ?, E> colAtt = (PluralAttribute<? super X, ?, E>) att;
if (!elementType.isAssignableFrom(colAtt.getBindableJavaType())) {
throw pluralAttNotFound(type, name, elementType, declared);
}
}
private IllegalArgumentException pluralAttNotFound(String type, String name, Class<?> elementType,
boolean declared) {
return new IllegalArgumentException(type + " attribute " + name + " with element type " + elementType +
" is not " + (declared ? "declared" : "present") + " in type " + this);
}
@Override
public <E> ListAttribute<? super X, E> getList(String name, Class<E> elementType) {
return getPluralAttribute("List", name, elementType, ListAttribute.class);
}
@Override
public ListAttribute<? super X, ?> getList(String name) {
return getList(name, Object.class);
}
@Override
public <K, V> MapAttribute<? super X, K, V> getMap(String name, Class<K> keyType, Class<V> valueType) {
throw new UnsupportedOperationException();
}
@Override
public MapAttribute<? super X, ?, ?> getMap(String name) {
return getMap(name, Object.class, Object.class);
}
@Override
public <E> SetAttribute<? super X, E> getSet(String name, Class<E> elementType) {
return getPluralAttribute("Set", name, elementType, SetAttribute.class);
}
@Override
public SetAttribute<? super X, ?> getSet(String name) {
return getSet(name, Object.class);
}
@Override
public Set<PluralAttribute<? super X, ?, ?>> getPluralAttributes() {
final Set<PluralAttribute<? super X, ?, ?>> plurals = new HashSet<>();
plurals.addAll(getDeclaredPluralAttributes());
if (supertype != null) {
plurals.addAll(supertype.getPluralAttributes());
}
return plurals;
}
@Override
public Set<SingularAttribute<? super X, ?>> getSingularAttributes() {
final Set<SingularAttribute<? super X, ?>> singulars = new HashSet<>();
singulars.addAll(getDeclaredSingularAttributes());
if (supertype != null) {
singulars.addAll(supertype.getSingularAttributes());
}
return singulars;
}
@Override
public <Y> SingularAttribute<? super X, Y> getSingularAttribute(String name, Class<Y> type) {
final Attribute<? super X, ?> a = getAttribute(name);
if (a.isCollection()) {
throw singularAttNotFound(name, type, false);
}
assert a instanceof SingularAttribute;
if (!type.isAssignableFrom(a.getJavaType())) {
throw singularAttNotFound(name, type, false);
}
return (SingularAttribute<? super X, Y>) a;
}
private IllegalArgumentException singularAttNotFound(String name, Class<?> type, boolean declared) {
return new IllegalArgumentException(
"Singular attribute " + name + " of type " + type + " is not " + (declared ? "declared" : "present") +
" in type " + this);
}
@Override
public SingularAttribute<? super X, ?> getSingularAttribute(String name) {
return getSingularAttribute(name, Object.class);
}
@Override
public Set<Attribute<X, ?>> getDeclaredAttributes() {
return new HashSet<>(declaredAttributes.values());
}
@Override
public Set<PluralAttribute<X, ?, ?>> getDeclaredPluralAttributes() {
return declaredAttributes.values().stream().filter(Attribute::isCollection)
.map(a -> (PluralAttribute<X, ?, ?>) a).collect(Collectors.toSet());
}
@Override
public Set<SingularAttribute<X, ?>> getDeclaredSingularAttributes() {
return declaredAttributes.values().stream().filter(att -> !att.isCollection()).map(
a -> (SingularAttribute<X, ?>) a).collect(Collectors.toSet());
}
@Override
public Attribute<X, ?> getDeclaredAttribute(String name) {
if (declaredAttributes.containsKey(name)) {
return declaredAttributes.get(name);
}
throw new IllegalArgumentException("Attribute " + name + " is not declared in type " + name);
}
@Override
public <E> CollectionAttribute<X, E> getDeclaredCollection(String name, Class<E> elementType) {
return getDeclaredPluralAttribute("Collection", name, elementType, CollectionAttribute.class);
}
private <E, R extends PluralAttribute<? super X, ?, E>> R getDeclaredPluralAttribute(String type, String name,
Class<E> elementType,
Class<R> attType) {
final Attribute<? super X, ?> a = getDeclaredAttribute(name);
checkPluralAttribute(a, type, name, elementType, attType, true);
return attType.cast(a);
}
@Override
public CollectionAttribute<X, ?> getDeclaredCollection(String name) {
return getDeclaredCollection(name, Object.class);
}
@Override
public <E> ListAttribute<X, E> getDeclaredList(String name, Class<E> elementType) {
return getDeclaredPluralAttribute("List", name, elementType, ListAttribute.class);
}
@Override
public ListAttribute<X, ?> getDeclaredList(String name) {
return getDeclaredList(name, Object.class);
}
@Override
public <K, V> MapAttribute<X, K, V> getDeclaredMap(String name, Class<K> keyType, Class<V> valueType) {
throw new UnsupportedOperationException();
}
@Override
public MapAttribute<X, ?, ?> getDeclaredMap(String name) {
return getDeclaredMap(name, Object.class, Object.class);
}
@Override
public <E> SetAttribute<X, E> getDeclaredSet(String name, Class<E> elementType) {
return getDeclaredPluralAttribute("Set", name, elementType, SetAttribute.class);
}
@Override
public SetAttribute<X, ?> getDeclaredSet(String name) {
return getDeclaredSet(name, Object.class);
}
@Override
public <Y> SingularAttribute<X, Y> getDeclaredSingularAttribute(String name, Class<Y> type) {
final Attribute<X, ?> a = getDeclaredAttribute(name);
if (a.isCollection()) {
throw singularAttNotFound(name, type, true);
}
if (!type.isAssignableFrom(a.getJavaType())) {
throw singularAttNotFound(name, type, true);
}
return (SingularAttribute<X, Y>) a;
}
@Override
public SingularAttribute<X, ?> getDeclaredSingularAttribute(String name) {
return getDeclaredSingularAttribute(name, Object.class);
}
@Override
public TypesSpecification<? super X, ?> getTypes() {
if (directTypes != null) {
return directTypes;
}
return supertype != null ? supertype.getTypes() : null;
}
@Override
public PropertiesSpecification<? super X, ?, ?, ?> getProperties() {
if (properties != null) {
return properties;
}
return supertype != null ? supertype.getProperties() : null;
}
@Override
public Set<FieldSpecification<? super X, ?>> getFieldSpecifications() {
final Set<FieldSpecification<? super X, ?>> specs = new HashSet<>(getAttributes());
final TypesSpecification<? super X, ?> types = getTypes();
if (types != null) {
specs.add(types);
}
final PropertiesSpecification<? super X, ?, ?, ?> props = getProperties();
if (props != null) {
specs.add(props);
}
return specs;
}
@Override
public FieldSpecification<? super X, ?> getFieldSpecification(String fieldName) {
if (declaredAttributes.containsKey(fieldName)) {
return declaredAttributes.get(fieldName);
}
if (directTypes != null && directTypes.getName().equals(fieldName)) {
return directTypes;
} else if (properties != null && properties.getName().equals(fieldName)) {
return properties;
}
if (supertype != null) {
return supertype.getFieldSpecification(fieldName);
}
throw new IllegalArgumentException("Field " + fieldName + " is not present in type " + this);
}
@Override
public Identifier getIdentifier() {
return identifier;
}
@Override
public Class<X> getJavaType() {
return javaType;
}
}