Skip to content

Method: getCollectionType()

1: /**
2: * Copyright (C) 2019 Czech Technical University in Prague
3: * <p>
4: * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
5: * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
6: * version.
7: * <p>
8: * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
9: * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
10: * details. You should have received a copy of the GNU General Public License along with this program. If not, see
11: * <http://www.gnu.org/licenses/>.
12: */
13: package cz.cvut.kbss.jopa.model.metamodel;
14:
15: import cz.cvut.kbss.jopa.oom.converter.ConverterWrapper;
16:
17: import java.lang.reflect.Field;
18: import java.util.Collection;
19: import java.util.Map;
20:
21: public abstract class AbstractPluralAttribute<X, C, E> extends AbstractAttribute<X, C>
22: implements PluralAttribute<X, C, E> {
23:
24: private final Type<E> elementType;
25:
26: private final Class<C> collectionType;
27:
28: AbstractPluralAttribute(PluralAttributeBuilder<X, C, E> builder) {
29: super(builder);
30: this.elementType = builder.elementType;
31: this.collectionType = builder.collectionType;
32: }
33:
34: @Override
35: public boolean isAssociation() {
36: return getPersistentAttributeType().equals(
37: PersistentAttributeType.OBJECT);
38: }
39:
40: @Override
41: public boolean isCollection() {
42: return true;
43: }
44:
45: @Override
46: public Class<E> getBindableJavaType() {
47: return elementType.getJavaType();
48: }
49:
50: @Override
51: public cz.cvut.kbss.jopa.model.metamodel.Bindable.BindableType getBindableType() {
52: return BindableType.PLURAL_ATTRIBUTE;
53: }
54:
55: @Override
56: public cz.cvut.kbss.jopa.model.metamodel.PluralAttribute.CollectionType getCollectionType() {
57:• if (getJavaType().isAssignableFrom(Collection.class)) {
58: return CollectionType.COLLECTION;
59:• } else if (getJavaType().isAssignableFrom(Map.class)) {
60: return CollectionType.MAP;
61: } else {
62: throw new IllegalArgumentException();
63: }
64: }
65:
66: @Override
67: public Type<E> getElementType() {
68: return elementType;
69: }
70:
71: @Override
72: public Class<C> getJavaType() {
73: return collectionType;
74: }
75:
76: public abstract static class PluralAttributeBuilder<X, C, E> extends AbstractAttributeBuilder<X, C> {
77: private Type<E> elementType;
78: private Class<C> collectionType;
79:
80: @Override
81: public PluralAttributeBuilder<X, C, E> config(PropertyAttributes config) {
82: super.config(config);
83: elementType((Type<E>) config.getType());
84: return this;
85: }
86:
87: public PluralAttributeBuilder<X, C, E> elementType(Type<E> elementType) {
88: this.elementType = elementType;
89: return this;
90: }
91:
92: public PluralAttributeBuilder<X, C, E> collectionType(Class<C> collectionType) {
93: this.collectionType = collectionType;
94: return this;
95: }
96:
97: @Override
98: public PluralAttributeBuilder<X, C, E> field(Field field) {
99: super.field(field);
100: return this;
101: }
102:
103: @Override
104: public PluralAttributeBuilder<X, C, E> declaringType(ManagedType<X> declaringType) {
105: super.declaringType(declaringType);
106: return this;
107: }
108:
109: @Override
110: public PluralAttributeBuilder<X, C, E> inferred(boolean inferred) {
111: super.inferred(inferred);
112: return this;
113: }
114:
115: @Override
116: public PluralAttributeBuilder<X, C, E> includeExplicit(boolean includeExplicit) {
117: super.includeExplicit(includeExplicit);
118: return this;
119: }
120:
121: @Override
122: public PluralAttributeBuilder<X, C, E> converter(ConverterWrapper converter) {
123: super.converter(converter);
124: return this;
125: }
126:
127: public abstract PluralAttribute<X, C, E> build();
128: }
129: }