Skip to content

Method: getMap(String)

1: /**
2: * Copyright (C) 2016 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.jopa.model;
16:
17: import cz.cvut.kbss.jopa.model.metamodel.*;
18:
19: import java.util.HashMap;
20: import java.util.HashSet;
21: import java.util.Map;
22: import java.util.Set;
23: import java.util.stream.Collectors;
24:
25: public class EntityTypeImpl<X> implements EntityType<X> {
26:
27: private final String name;
28:
29: private Class<X> javaType;
30:
31: private final Map<String, Attribute<? super X, ?>> attributeMap = new HashMap<>();
32:
33: private final Map<String, Attribute<X, ?>> declaredAttributeMap = new HashMap<>();
34:
35: private final IRI iri;
36:
37: private Identifier identifier;
38:
39: private TypesSpecification<X, ?> directTypes;
40:
41: private PropertiesSpecification<X, ?, ?, ?> properties;
42:
43: public EntityTypeImpl(String name, Class<X> javaType, final IRI iri) {
44: this.name = name;
45: this.javaType = javaType;
46: this.iri = iri;
47: }
48:
49: // void addAttribute(final String name, final Attribute<? super X, ?> a) {
50: // attributeMap.put(name, a);
51: // }
52:
53: public void addDirectTypes(TypesSpecification<X, ?> a) {
54: this.directTypes = a;
55: }
56:
57: public void addOtherProperties(PropertiesSpecification<X, ?, ?, ?> a) {
58: this.properties = a;
59: }
60:
61: public void addDeclaredAttribute(final String name, final Attribute<X, ?> a) {
62: declaredAttributeMap.put(name, a);
63: attributeMap.put(name, a);
64: }
65:
66: public void setIdentifier(final Identifier identifier) {
67: this.identifier = identifier;
68: }
69:
70: public String getName() {
71: return name;
72: }
73:
74: //
75: // public <Y> SingularAttribute<X, Y> getDeclaredId(Class<Y> type) {
76: // for (final Attribute<X, ?> a : declaredAttributeMap.values()) {
77: // final SingularAttribute<X, Y> sa = (SingularAttribute<X, Y>) a;
78: //
79: // if (sa.isId() && sa.getJavaType().isAssignableFrom(type)) {
80: // return sa;
81: // }
82: // }
83: //
84: // throw new IllegalArgumentException();
85: // }
86: //
87: //
88: // public <Y> SingularAttribute<? super X, Y> getId(Class<Y> type) {
89: // for (final Attribute<? super X, ?> a : attributeMap.values()) {
90: // final SingularAttribute<? super X, Y> sa = (SingularAttribute<? super X,
91: // Y>) a;
92: //
93: // if (sa.isId() && sa.getJavaType().isAssignableFrom(type)) {
94: // return sa;
95: // }
96: // }
97: //
98: // throw new IllegalArgumentException();
99: // }
100: //
101: //
102: // public Type<?> getIdType() {
103: // return idField.getType();
104: // }
105:
106: public <Y> SingularAttribute<X, Y> getDeclaredVersion(Class<Y> type) {
107: // TODO
108: throw new UnsupportedOperationException();
109: }
110:
111: public Set<SingularAttribute<? super X, ?>> getIdClassAttributes() {
112: // TODO
113: throw new UnsupportedOperationException();
114: }
115:
116: public IdentifiableType<? super X> getSupertype() {
117: // TODO
118: throw new UnsupportedOperationException();
119: }
120:
121: public <Y> SingularAttribute<? super X, Y> getVersion(Class<Y> type) {
122: // TODO
123: throw new UnsupportedOperationException();
124: }
125:
126: public boolean hasSingleIdAttribute() {
127: // TODO
128: return false;
129: }
130:
131: public boolean hasVersionAttribute() {
132: // TODO
133: return false;
134: }
135:
136: public Attribute<? super X, ?> getAttribute(String name) {
137: if (attributeMap.containsKey(name)) {
138: return attributeMap.get(name);
139: }
140: throw new IllegalArgumentException("Attribute " + name + " is not present in type " + name);
141: }
142:
143: public Set<Attribute<? super X, ?>> getAttributes() {
144: return new HashSet<>(attributeMap.values());
145: }
146:
147: public <E> CollectionAttribute<? super X, E> getCollection(String name, Class<E> elementType) {
148: final CollectionAttribute<? super X, E> a = (CollectionAttribute<? super X, E>) getAttribute(name);
149:
150: if (!a.getBindableJavaType().isAssignableFrom(elementType)) {
151: throw new IllegalArgumentException();
152: }
153:
154: return a;
155: }
156:
157: public CollectionAttribute<? super X, ?> getCollection(String name) {
158: return getCollection(name, Object.class);
159: }
160:
161: public Attribute<X, ?> getDeclaredAttribute(String name) {
162: if (declaredAttributeMap.containsKey(name)) {
163: return declaredAttributeMap.get(name);
164: }
165: throw new IllegalArgumentException("Attribute " + name + " is not declared in type " + name);
166: }
167:
168: public Set<Attribute<X, ?>> getDeclaredAttributes() {
169: return new HashSet<Attribute<X, ?>>(declaredAttributeMap.values());
170: }
171:
172: public <E> CollectionAttribute<X, E> getDeclaredCollection(String name, Class<E> elementType) {
173: final CollectionAttribute<X, E> a = (CollectionAttribute<X, E>) getDeclaredAttribute(name);
174:
175: if (!a.getBindableJavaType().isAssignableFrom(elementType)) {
176: throw new IllegalArgumentException();
177: }
178:
179: return a;
180: }
181:
182: public CollectionAttribute<X, ?> getDeclaredCollection(String name) {
183: return getDeclaredCollection(name, Object.class);
184: }
185:
186: public <E> ListAttribute<X, E> getDeclaredList(String name, Class<E> elementType) {
187: final ListAttribute<X, E> a = (ListAttribute<X, E>) getDeclaredAttribute(name);
188:
189: if (!a.getBindableJavaType().isAssignableFrom(elementType)) {
190: throw new IllegalArgumentException();
191: }
192:
193: return a;
194: }
195:
196: public ListAttribute<X, ?> getDeclaredList(String name) {
197: return getDeclaredList(name, Object.class);
198: }
199:
200: public <K, V> MapAttribute<X, K, V> getDeclaredMap(String name, Class<K> keyType,
201: Class<V> valueType) {
202: final MapAttribute<X, K, V> a = (MapAttribute<X, K, V>) getDeclaredAttribute(name);
203:
204: if (!a.getKeyJavaType().isAssignableFrom(keyType)
205: || !a.getBindableJavaType().isAssignableFrom(valueType)) {
206: throw new IllegalArgumentException();
207: }
208:
209: return a;
210: }
211:
212: public MapAttribute<X, ?, ?> getDeclaredMap(String name) {
213: return getDeclaredMap(name, Object.class, Object.class);
214: }
215:
216: public Set<PluralAttribute<X, ?, ?>> getDeclaredPluralAttributes() {
217: final Set<PluralAttribute<X, ?, ?>> set = new HashSet<PluralAttribute<X, ?, ?>>();
218:
219: for (final Attribute<X, ?> a : declaredAttributeMap.values()) {
220: if (a.isCollection()) {
221: set.add((PluralAttribute<X, ?, ?>) a);
222: }
223: }
224:
225: return set;
226: }
227:
228: public <E> SetAttribute<X, E> getDeclaredSet(String name, Class<E> elementType) {
229: final SetAttribute<X, E> a = (SetAttribute<X, E>) getDeclaredAttribute(name);
230:
231: if (!a.getBindableJavaType().isAssignableFrom(elementType)) {
232: throw new IllegalArgumentException();
233: }
234:
235: return a;
236: }
237:
238: public SetAttribute<X, ?> getDeclaredSet(String name) {
239: return getDeclaredSet(name, Object.class);
240: }
241:
242: public <Y> SingularAttribute<X, Y> getDeclaredSingularAttribute(String name, Class<Y> type) {
243: final SingularAttribute<X, Y> a = (SingularAttribute<X, Y>) getDeclaredAttribute(name);
244:
245: if (!a.getJavaType().isAssignableFrom(type)) {
246: throw new IllegalArgumentException();
247: }
248:
249: return a;
250: }
251:
252: public SingularAttribute<X, ?> getDeclaredSingularAttribute(String name) {
253: return getDeclaredSingularAttribute(name, Object.class);
254: }
255:
256: public Set<SingularAttribute<X, ?>> getDeclaredSingularAttributes() {
257: return declaredAttributeMap.values().stream().filter(Attribute::isCollection).map(
258: a -> (SingularAttribute<X, ?>) a)
259: .collect(Collectors.toSet());
260: }
261:
262: public <E> ListAttribute<? super X, E> getList(String name, Class<E> elementType) {
263: final ListAttribute<? super X, E> a = (ListAttribute<? super X, E>) getAttribute(name);
264:
265: if (!a.getBindableJavaType().isAssignableFrom(elementType)) {
266: throw new IllegalArgumentException();
267: }
268:
269: return a;
270: }
271:
272: public ListAttribute<? super X, ?> getList(String name) {
273: return getList(name, Object.class);
274: }
275:
276: public <K, V> MapAttribute<? super X, K, V> getMap(String name, Class<K> keyType,
277: Class<V> valueType) {
278: final MapAttribute<? super X, K, V> a = (MapAttribute<? super X, K, V>) getAttribute(name);
279:
280: if (!a.getKeyJavaType().isAssignableFrom(keyType)
281: || !a.getBindableJavaType().isAssignableFrom(valueType)) {
282: throw new IllegalArgumentException();
283: }
284:
285: return a;
286: }
287:
288: public MapAttribute<? super X, ?, ?> getMap(String name) {
289: return getMap(name, Object.class, Object.class);
290: }
291:
292: public Set<PluralAttribute<? super X, ?, ?>> getPluralAttributes() {
293: return attributeMap.values().stream().filter(Attribute::isCollection).map(
294: a -> (PluralAttribute<? super X, ?, ?>) a)
295: .collect(Collectors.toSet());
296: }
297:
298: public <E> SetAttribute<? super X, E> getSet(String name, Class<E> elementType) {
299: final SetAttribute<? super X, E> a = (SetAttribute<? super X, E>) getAttribute(name);
300:
301: if (!a.getBindableJavaType().isAssignableFrom(elementType)) {
302: throw new IllegalArgumentException();
303: }
304:
305: return a;
306: }
307:
308: public SetAttribute<? super X, ?> getSet(String name) {
309: return getSet(name, Object.class);
310: }
311:
312: public <Y> SingularAttribute<? super X, Y> getSingularAttribute(String name, Class<Y> type) {
313: final Attribute<? super X, ?> a = getAttribute(name);
314:
315: if (!a.getJavaType().isAssignableFrom(type)) {
316: throw new IllegalArgumentException();
317: }
318:
319: return (SingularAttribute<? super X, Y>) a;
320: }
321:
322: public SingularAttribute<? super X, ?> getSingularAttribute(String name) {
323: return getSingularAttribute(name, Object.class);
324: }
325:
326: public Set<SingularAttribute<? super X, ?>> getSingularAttributes() {
327: final Set<SingularAttribute<? super X, ?>> set = new HashSet<SingularAttribute<? super X, ?>>();
328:
329: for (final Attribute<? super X, ?> a : attributeMap.values()) {
330: if (!a.isCollection()) {
331: set.add((SingularAttribute<? super X, ?>) a);
332: }
333: }
334:
335: return set;
336: }
337:
338: public Class<X> getJavaType() {
339: return javaType;
340: }
341:
342: public cz.cvut.kbss.jopa.model.metamodel.Type.PersistenceType getPersistenceType() {
343: return PersistenceType.ENTITY;
344: }
345:
346: public Class<X> getBindableJavaType() {
347: return getJavaType();
348: }
349:
350: public cz.cvut.kbss.jopa.model.metamodel.Bindable.BindableType getBindableType() {
351: return BindableType.ENTITY_TYPE;
352: }
353:
354: public IRI getIRI() {
355: return iri;
356: }
357:
358: public Identifier getIdentifier() {
359: return identifier;
360: }
361:
362: public TypesSpecification<? super X, ?> getTypes() {
363: return directTypes;
364: }
365:
366: public PropertiesSpecification<? super X, ?, ?, ?> getProperties() {
367: return properties;
368: }
369:
370: public FieldSpecification<? super X, ?> getFieldSpecification(String fieldName) {
371: FieldSpecification<? super X, ?> att = attributeMap.get(fieldName);
372: if (att == null) {
373: if (directTypes != null && directTypes.getName().equals(fieldName)) {
374: att = directTypes;
375: } else if (properties != null && properties.getName().equals(fieldName)) {
376: att = properties;
377: }
378: }
379: if (att == null) {
380: throw new IllegalArgumentException("Field " + fieldName + " is not declared in type " + name);
381: }
382: return att;
383: }
384:
385: @Override
386: public Set<FieldSpecification<? super X, ?>> getFieldSpecifications() {
387: final Set<FieldSpecification<? super X, ?>> specs = new HashSet<>(attributeMap.values());
388: if (directTypes != null) {
389: specs.add(directTypes);
390: }
391: if (properties != null) {
392: specs.add(properties);
393: }
394: return specs;
395: }
396:
397: @Override
398: public String toString() {
399: return "EntityType{" + name + "<" + iri + ">}";
400: }
401: }