Skip to contentMethod: collectionType(Class)
1: /**
2: * Copyright (C) 2020 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.metamodel;
16:
17: import cz.cvut.kbss.jopa.oom.converter.ConverterWrapper;
18:
19: import java.lang.reflect.Field;
20: import java.util.Collection;
21: import java.util.Map;
22:
23: public abstract class AbstractPluralAttribute<X, C, E> extends AbstractAttribute<X, C>
24: implements PluralAttribute<X, C, E> {
25:
26: private final Type<E> elementType;
27:
28: private final Class<C> collectionType;
29:
30: AbstractPluralAttribute(PluralAttributeBuilder<X, C, E> builder) {
31: super(builder);
32: this.elementType = builder.elementType;
33: this.collectionType = builder.collectionType;
34: }
35:
36: @Override
37: public boolean isAssociation() {
38: return getPersistentAttributeType().equals(
39: PersistentAttributeType.OBJECT);
40: }
41:
42: @Override
43: public boolean isCollection() {
44: return true;
45: }
46:
47: @Override
48: public Class<E> getBindableJavaType() {
49: return elementType.getJavaType();
50: }
51:
52: @Override
53: public cz.cvut.kbss.jopa.model.metamodel.Bindable.BindableType getBindableType() {
54: return BindableType.PLURAL_ATTRIBUTE;
55: }
56:
57: @Override
58: public cz.cvut.kbss.jopa.model.metamodel.PluralAttribute.CollectionType getCollectionType() {
59: if (getJavaType().isAssignableFrom(Collection.class)) {
60: return CollectionType.COLLECTION;
61: } else if (getJavaType().isAssignableFrom(Map.class)) {
62: return CollectionType.MAP;
63: } else {
64: throw new IllegalArgumentException();
65: }
66: }
67:
68: @Override
69: public Type<E> getElementType() {
70: return elementType;
71: }
72:
73: @Override
74: public Class<C> getJavaType() {
75: return collectionType;
76: }
77:
78: public abstract static class PluralAttributeBuilder<X, C, E> extends AbstractAttributeBuilder<X, C> {
79: private Type<E> elementType;
80: private Class<C> collectionType;
81:
82: @Override
83: public PluralAttributeBuilder<X, C, E> config(PropertyAttributes config) {
84: super.config(config);
85: elementType((Type<E>) config.getType());
86: return this;
87: }
88:
89: public PluralAttributeBuilder<X, C, E> elementType(Type<E> elementType) {
90: this.elementType = elementType;
91: return this;
92: }
93:
94: public PluralAttributeBuilder<X, C, E> collectionType(Class<C> collectionType) {
95: this.collectionType = collectionType;
96: return this;
97: }
98:
99: @Override
100: public PluralAttributeBuilder<X, C, E> field(Field field) {
101: super.field(field);
102: return this;
103: }
104:
105: @Override
106: public PluralAttributeBuilder<X, C, E> declaringType(ManagedType<X> declaringType) {
107: super.declaringType(declaringType);
108: return this;
109: }
110:
111: @Override
112: public PluralAttributeBuilder<X, C, E> inferred(boolean inferred) {
113: super.inferred(inferred);
114: return this;
115: }
116:
117: @Override
118: public PluralAttributeBuilder<X, C, E> includeExplicit(boolean includeExplicit) {
119: super.includeExplicit(includeExplicit);
120: return this;
121: }
122:
123: @Override
124: public PluralAttributeBuilder<X, C, E> converter(ConverterWrapper converter) {
125: super.converter(converter);
126: return this;
127: }
128:
129: public abstract PluralAttribute<X, C, E> build();
130: }
131: }