-
Notifications
You must be signed in to change notification settings - Fork 14
/
EntityTypeImpl.java
401 lines (318 loc) · 12.1 KB
/
EntityTypeImpl.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/**
* Copyright (C) 2016 Czech Technical University in Prague
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details. You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cz.cvut.kbss.jopa.model;
import cz.cvut.kbss.jopa.model.metamodel.*;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
public class EntityTypeImpl<X> implements EntityType<X> {
private final String name;
private Class<X> javaType;
private final Map<String, Attribute<? super X, ?>> attributeMap = new HashMap<>();
private final Map<String, Attribute<X, ?>> declaredAttributeMap = new HashMap<>();
private final IRI iri;
private Identifier identifier;
private TypesSpecification<X, ?> directTypes;
private PropertiesSpecification<X, ?, ?, ?> properties;
public EntityTypeImpl(String name, Class<X> javaType, final IRI iri) {
this.name = name;
this.javaType = javaType;
this.iri = iri;
}
// void addAttribute(final String name, final Attribute<? super X, ?> a) {
// attributeMap.put(name, a);
// }
public void addDirectTypes(TypesSpecification<X, ?> a) {
this.directTypes = a;
}
public void addOtherProperties(PropertiesSpecification<X, ?, ?, ?> a) {
this.properties = a;
}
public void addDeclaredAttribute(final String name, final Attribute<X, ?> a) {
declaredAttributeMap.put(name, a);
attributeMap.put(name, a);
}
public void setIdentifier(final Identifier identifier) {
this.identifier = identifier;
}
@Override
public String getName() {
return name;
}
@Override
public <Y> SingularAttribute<X, Y> getDeclaredVersion(Class<Y> type) {
// TODO
throw new UnsupportedOperationException();
}
@Override
public Set<SingularAttribute<? super X, ?>> getIdClassAttributes() {
// TODO
throw new UnsupportedOperationException();
}
@Override
public IdentifiableType<? super X> getSupertype() {
// TODO
throw new UnsupportedOperationException();
}
@Override
public <Y> SingularAttribute<? super X, Y> getVersion(Class<Y> type) {
// TODO
throw new UnsupportedOperationException();
}
@Override
public boolean hasSingleIdAttribute() {
// TODO
return false;
}
@Override
public boolean hasVersionAttribute() {
// TODO
return false;
}
@Override
public Attribute<? super X, ?> getAttribute(String name) {
if (attributeMap.containsKey(name)) {
return attributeMap.get(name);
}
throw new IllegalArgumentException("Attribute " + name + " is not present in type " + name);
}
@Override
public Set<Attribute<? super X, ?>> getAttributes() {
return new HashSet<>(attributeMap.values());
}
@Override
public <E> CollectionAttribute<? super X, E> getCollection(String name, Class<E> elementType) {
final CollectionAttribute<? super X, E> a = (CollectionAttribute<? super X, E>) getAttribute(name);
if (!a.getBindableJavaType().isAssignableFrom(elementType)) {
throw new IllegalArgumentException();
}
return a;
}
@Override
public CollectionAttribute<? super X, ?> getCollection(String name) {
return getCollection(name, Object.class);
}
@Override
public Attribute<X, ?> getDeclaredAttribute(String name) {
if (declaredAttributeMap.containsKey(name)) {
return declaredAttributeMap.get(name);
}
throw new IllegalArgumentException("Attribute " + name + " is not declared in type " + name);
}
@Override
public Set<Attribute<X, ?>> getDeclaredAttributes() {
return new HashSet<>(declaredAttributeMap.values());
}
@Override
public <E> CollectionAttribute<X, E> getDeclaredCollection(String name, Class<E> elementType) {
final CollectionAttribute<X, E> a = (CollectionAttribute<X, E>) getDeclaredAttribute(name);
if (!a.getBindableJavaType().isAssignableFrom(elementType)) {
throw new IllegalArgumentException();
}
return 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) {
final ListAttribute<X, E> a = (ListAttribute<X, E>) getDeclaredAttribute(name);
if (!a.getBindableJavaType().isAssignableFrom(elementType)) {
throw new IllegalArgumentException();
}
return a;
}
@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) {
final MapAttribute<X, K, V> a = (MapAttribute<X, K, V>) getDeclaredAttribute(name);
if (!a.getKeyJavaType().isAssignableFrom(keyType)
|| !a.getBindableJavaType().isAssignableFrom(valueType)) {
throw new IllegalArgumentException();
}
return a;
}
@Override
public MapAttribute<X, ?, ?> getDeclaredMap(String name) {
return getDeclaredMap(name, Object.class, Object.class);
}
@Override
public Set<PluralAttribute<X, ?, ?>> getDeclaredPluralAttributes() {
return declaredAttributeMap.values().stream().filter(Attribute::isCollection)
.map(a -> (PluralAttribute<X, ?, ?>) a)
.collect(Collectors.toSet());
}
@Override
public <E> SetAttribute<X, E> getDeclaredSet(String name, Class<E> elementType) {
final SetAttribute<X, E> a = (SetAttribute<X, E>) getDeclaredAttribute(name);
if (!a.getBindableJavaType().isAssignableFrom(elementType)) {
throw new IllegalArgumentException();
}
return a;
}
@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 SingularAttribute<X, Y> a = (SingularAttribute<X, Y>) getDeclaredAttribute(name);
if (!a.getJavaType().isAssignableFrom(type)) {
throw new IllegalArgumentException();
}
return a;
}
@Override
public SingularAttribute<X, ?> getDeclaredSingularAttribute(String name) {
return getDeclaredSingularAttribute(name, Object.class);
}
@Override
public Set<SingularAttribute<X, ?>> getDeclaredSingularAttributes() {
return declaredAttributeMap.values().stream().filter(Attribute::isCollection).map(
a -> (SingularAttribute<X, ?>) a)
.collect(Collectors.toSet());
}
@Override
public <E> ListAttribute<? super X, E> getList(String name, Class<E> elementType) {
final ListAttribute<? super X, E> a = (ListAttribute<? super X, E>) getAttribute(name);
if (!a.getBindableJavaType().isAssignableFrom(elementType)) {
throw new IllegalArgumentException();
}
return a;
}
@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) {
final MapAttribute<? super X, K, V> a = (MapAttribute<? super X, K, V>) getAttribute(name);
if (!a.getKeyJavaType().isAssignableFrom(keyType)
|| !a.getBindableJavaType().isAssignableFrom(valueType)) {
throw new IllegalArgumentException();
}
return a;
}
@Override
public MapAttribute<? super X, ?, ?> getMap(String name) {
return getMap(name, Object.class, Object.class);
}
@Override
public Set<PluralAttribute<? super X, ?, ?>> getPluralAttributes() {
return attributeMap.values().stream().filter(Attribute::isCollection).map(
a -> (PluralAttribute<? super X, ?, ?>) a)
.collect(Collectors.toSet());
}
@Override
public <E> SetAttribute<? super X, E> getSet(String name, Class<E> elementType) {
final SetAttribute<? super X, E> a = (SetAttribute<? super X, E>) getAttribute(name);
if (!a.getBindableJavaType().isAssignableFrom(elementType)) {
throw new IllegalArgumentException();
}
return a;
}
@Override
public SetAttribute<? super X, ?> getSet(String name) {
return getSet(name, Object.class);
}
@Override
public <Y> SingularAttribute<? super X, Y> getSingularAttribute(String name, Class<Y> type) {
final Attribute<? super X, ?> a = getAttribute(name);
if (!a.getJavaType().isAssignableFrom(type)) {
throw new IllegalArgumentException();
}
return (SingularAttribute<? super X, Y>) a;
}
@Override
public SingularAttribute<? super X, ?> getSingularAttribute(String name) {
return getSingularAttribute(name, Object.class);
}
@Override
public Set<SingularAttribute<? super X, ?>> getSingularAttributes() {
return attributeMap.values().stream().filter(a -> !a.isCollection())
.map(a -> (SingularAttribute<? super X, ?>) a)
.collect(Collectors.toSet());
}
@Override
public Class<X> getJavaType() {
return javaType;
}
@Override
public cz.cvut.kbss.jopa.model.metamodel.Type.PersistenceType getPersistenceType() {
return PersistenceType.ENTITY;
}
@Override
public Class<X> getBindableJavaType() {
return getJavaType();
}
@Override
public cz.cvut.kbss.jopa.model.metamodel.Bindable.BindableType getBindableType() {
return BindableType.ENTITY_TYPE;
}
@Override
public IRI getIRI() {
return iri;
}
@Override
public Identifier getIdentifier() {
return identifier;
}
@Override
public TypesSpecification<? super X, ?> getTypes() {
return directTypes;
}
@Override
public PropertiesSpecification<? super X, ?, ?, ?> getProperties() {
return properties;
}
@Override
public FieldSpecification<? super X, ?> getFieldSpecification(String fieldName) {
FieldSpecification<? super X, ?> att = attributeMap.get(fieldName);
if (att == null) {
if (directTypes != null && directTypes.getName().equals(fieldName)) {
att = directTypes;
} else if (properties != null && properties.getName().equals(fieldName)) {
att = properties;
}
}
if (att == null) {
throw new IllegalArgumentException("Field " + fieldName + " is not declared in type " + name);
}
return att;
}
@Override
public Set<FieldSpecification<? super X, ?>> getFieldSpecifications() {
final Set<FieldSpecification<? super X, ?>> specs = new HashSet<>(attributeMap.values());
if (directTypes != null) {
specs.add(directTypes);
}
if (properties != null) {
specs.add(properties);
}
return specs;
}
@Override
public String toString() {
return "EntityType{" + name + "<" + iri + ">}";
}
}