Skip to contentMethod: iri(IRI)
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.annotations.CascadeType;
18: import cz.cvut.kbss.jopa.model.annotations.FetchType;
19: import cz.cvut.kbss.jopa.model.annotations.ParticipationConstraint;
20: import cz.cvut.kbss.jopa.model.metamodel.ManagedType;
21: import cz.cvut.kbss.jopa.model.metamodel.PluralAttribute;
22: import cz.cvut.kbss.jopa.model.metamodel.Type;
23:
24: import java.lang.reflect.Field;
25: import java.lang.reflect.Member;
26: import java.util.Collection;
27: import java.util.Map;
28:
29: public class PluralAttributeImpl<X, C, E> implements PluralAttribute<X, C, E> {
30:
31: private final String name;
32:
33: private final Type<E> elementType;
34:
35: private final Class<C> collectionType;
36:
37: private final Field member;
38:
39: private final ManagedType<X> declaringType;
40:
41: private final PersistentAttributeType pat;
42:
43: private final IRI iri;
44:
45: private final CascadeType[] cascadeTypes;
46:
47: private final FetchType fetchType;
48:
49: private boolean inferred;
50:
51: private boolean includeExplicit;
52:
53: private boolean readOnly;
54:
55: private boolean nonEmpty;
56:
57: private ParticipationConstraint[] constraints;
58:
59: protected PluralAttributeImpl(PluralAttributeBuilder<X, C, E> builder) {
60: this.elementType = builder.elementType;
61: this.member = builder.field;
62: assert member != null;
63: this.name = member.getName();
64: this.pat = builder.attributeType;
65: this.collectionType = builder.collectionType;
66: this.declaringType = builder.declaringType;
67: this.iri = builder.iri;
68: this.cascadeTypes = builder.cascadeTypes;
69: this.fetchType = builder.fetchType;
70: this.inferred = builder.inferred;
71: this.includeExplicit = builder.includeExplicit;
72: this.readOnly = builder.readOnly;
73: this.nonEmpty = builder.nonEmpty;
74: this.constraints = builder.constraints;
75: }
76:
77: public ManagedType<X> getDeclaringType() {
78: return declaringType;
79: }
80:
81:
82: public Member getJavaMember() {
83: return member;
84: }
85:
86:
87: public String getName() {
88: return name;
89: }
90:
91:
92: public cz.cvut.kbss.jopa.model.metamodel.Attribute.PersistentAttributeType getPersistentAttributeType() {
93: return pat;
94: }
95:
96:
97: public boolean isAssociation() {
98: return getPersistentAttributeType().equals(
99: PersistentAttributeType.OBJECT);
100: }
101:
102:
103: public boolean isCollection() {
104: return true;
105: }
106:
107:
108: public Class<E> getBindableJavaType() {
109: return elementType.getJavaType();
110: }
111:
112:
113: public cz.cvut.kbss.jopa.model.metamodel.Bindable.BindableType getBindableType() {
114: return BindableType.PLURAL_ATTRIBUTE;
115: }
116:
117:
118: public cz.cvut.kbss.jopa.model.metamodel.PluralAttribute.CollectionType getCollectionType() {
119: if (getJavaType().isAssignableFrom(Collection.class)) {
120: return CollectionType.COLLECTION;
121: } else if (getJavaType().isAssignableFrom(Map.class)) {
122: return CollectionType.MAP;
123: } else {
124: throw new IllegalArgumentException();
125: }
126: }
127:
128:
129: public Type<E> getElementType() {
130: return elementType;
131: }
132:
133:
134: public Class<C> getJavaType() {
135: return collectionType;
136: }
137:
138:
139: public Field getJavaField() {
140: return member;
141: }
142:
143:
144: public IRI getIRI() {
145: return iri;
146: }
147:
148:
149: public CascadeType[] getCascadeTypes() {
150: if (getPersistentAttributeType().equals(PersistentAttributeType.OBJECT)) {
151: return cascadeTypes;
152: }
153:
154: return cascadeTypes;
155: }
156:
157:
158: public FetchType getFetchType() {
159: return fetchType;
160: }
161:
162: public boolean isInferred() {
163: return inferred;
164: }
165:
166: @Override
167: public boolean includeExplicit() {
168: return includeExplicit;
169: }
170:
171: @Override
172: public boolean isReadOnly() {
173: return readOnly;
174: }
175:
176: @Override
177: public boolean isNonEmpty() {
178: return nonEmpty;
179: }
180:
181: public ParticipationConstraint[] getConstraints() {
182: return constraints;
183: }
184:
185: public static PluralAttributeBuilder iri(IRI iri) {
186: return new PluralAttributeBuilder().iri(iri);
187: }
188:
189:
190: public static class PluralAttributeBuilder<X, C, E> {
191: private Type<E> elementType;
192: private Class<C> collectionType;
193: private Field field;
194: private ManagedType<X> declaringType;
195: private PersistentAttributeType attributeType;
196: private IRI iri;
197: private CascadeType[] cascadeTypes;
198: private FetchType fetchType;
199: private boolean inferred;
200: private boolean includeExplicit;
201: private boolean readOnly;
202: private boolean nonEmpty;
203: private ParticipationConstraint[] constraints;
204:
205: public PluralAttributeBuilder elementType(Type<E> elementType) {
206: this.elementType = elementType;
207: return this;
208: }
209:
210: public PluralAttributeBuilder collectionType(Class<C> collectionType) {
211: this.collectionType = collectionType;
212: return this;
213: }
214:
215: public PluralAttributeBuilder field(Field field) {
216: this.field = field;
217: return this;
218: }
219:
220: public PluralAttributeBuilder declaringType(ManagedType<X> declaringType) {
221: this.declaringType = declaringType;
222: return this;
223: }
224:
225: public PluralAttributeBuilder attributeType(PersistentAttributeType attributeType) {
226: this.attributeType = attributeType;
227: return this;
228: }
229:
230: public PluralAttributeBuilder iri(IRI iri) {
231: this.iri = iri;
232: return this;
233: }
234:
235: public PluralAttributeBuilder cascadeTypes(CascadeType[] cascadeTypes) {
236: this.cascadeTypes = cascadeTypes;
237: return this;
238: }
239:
240: public PluralAttributeBuilder fetchType(FetchType fetchType) {
241: this.fetchType = fetchType;
242: return this;
243: }
244:
245: public PluralAttributeBuilder inferred(boolean inferred) {
246: this.inferred = inferred;
247: return this;
248: }
249:
250: public PluralAttributeBuilder includeExplicit(boolean includeExplicit) {
251: this.includeExplicit = includeExplicit;
252: return this;
253: }
254:
255: public PluralAttributeBuilder readOnly(boolean readOnly) {
256: this.readOnly = readOnly;
257: return this;
258: }
259:
260: public PluralAttributeBuilder nonEmpty(boolean nonEmpty) {
261: this.nonEmpty = nonEmpty;
262: return this;
263: }
264:
265: public PluralAttributeBuilder participationConstraints(ParticipationConstraint[] constraints) {
266: this.constraints = constraints;
267: return this;
268: }
269:
270: public PluralAttributeImpl<X, C, E> build() {
271: return new PluralAttributeImpl<>(this);
272: }
273: }
274: }