Skip to content

Package: CollectionFactory

CollectionFactory

nameinstructionbranchcomplexitylinemethod
CollectionFactory(UnitOfWorkImpl)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
createDefaultCollection(PluralAttribute.CollectionType)
M: 0 C: 27
100%
M: 0 C: 3
100%
M: 0 C: 3
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
createDefaultMap()
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
createIndirectCollection(Object, Object, Field)
M: 13 C: 44
77%
M: 1 C: 5
83%
M: 1 C: 3
75%
M: 1 C: 7
88%
M: 0 C: 1
100%
createInstance(Collection)
M: 12 C: 21
64%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 1 C: 5
83%
M: 0 C: 1
100%

Coverage

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.utils;
16:
17: import cz.cvut.kbss.jopa.adapters.IndirectCollection;
18: import cz.cvut.kbss.jopa.adapters.IndirectList;
19: import cz.cvut.kbss.jopa.adapters.IndirectMap;
20: import cz.cvut.kbss.jopa.adapters.IndirectSet;
21: import cz.cvut.kbss.jopa.model.metamodel.PluralAttribute;
22: import cz.cvut.kbss.jopa.sessions.UnitOfWorkImpl;
23:
24: import java.lang.reflect.Field;
25: import java.util.*;
26:
27: public final class CollectionFactory {
28:
29: private final UnitOfWorkImpl uow;
30:
31: public CollectionFactory(UnitOfWorkImpl uow) {
32: this.uow = uow;
33: }
34:
35: /**
36: * Creates an indirect collection which wraps the specified collection and propagates changes to the current
37: * persistence context.
38: *
39: * @param collection The collection to be proxied
40: * @param owner Collection owner instance
41: * @param field Field whose value the collection is
42: * @return Indirect collection wrapping the collection
43: */
44: public IndirectCollection<?> createIndirectCollection(Object collection, Object owner, Field field) {
45: final IndirectCollection<?> res;
46:• if (collection instanceof List) {
47: res = new IndirectList<>(owner, field, uow, (List<?>) collection);
48:• } else if (collection instanceof Set) {
49: res = new IndirectSet<>(owner, field, uow, (Set<?>) collection);
50:• } else if (collection instanceof Map) {
51: res = new IndirectMap<>(owner, field, uow, (Map<?, ?>) collection);
52: } else {
53: throw new UnsupportedOperationException("Unsupported collection type " + collection.getClass());
54: }
55: return res;
56: }
57:
58: /**
59: * Creates an instance of a {@link Collection} implementation best matching the specified instance.
60: * <p>
61: * E.g. for any kind of {@link List}, an {@link ArrayList} is returned.
62: *
63: * @param collection Create matching instance for this collection
64: * @return Best matching collection instance
65: */
66: public static Collection<?> createInstance(Collection<?> collection) {
67: Objects.requireNonNull(collection);
68:
69:• if (collection instanceof List) {
70: return new ArrayList<>(collection.size());
71:• } else if (collection instanceof Set) {
72: return new HashSet<>(collection.size());
73: }
74: throw new IllegalArgumentException("Unsupported collection type: " + collection);
75: }
76:
77: /**
78: * Creates default collection for the specified collection type.
79: *
80: * @param collectionType Type of the collection to create
81: * @return Collection implementation instance
82: */
83: public static Collection<Object> createDefaultCollection(PluralAttribute.CollectionType collectionType) {
84:• switch (collectionType) {
85: case LIST:
86: return new ArrayList<>();
87: case SET:
88: case COLLECTION: // Intentional fall-through
89: return new HashSet<>();
90: default:
91: throw new IllegalArgumentException("Collection type " + collectionType + " is not supported.");
92: }
93: }
94:
95: /**
96: * Creates default instance of {@link Map}.
97: *
98: * @return Default Map implementation instance
99: */
100: public static Map<Object, Object> createDefaultMap() {
101: return new HashMap<>();
102: }
103: }