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%
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.sessions.UnitOfWorkImpl;
22:
23: import java.lang.reflect.Field;
24: import java.util.*;
25:
26: public final class CollectionFactory {
27:
28: private final UnitOfWorkImpl uow;
29:
30: public CollectionFactory(UnitOfWorkImpl uow) {
31: this.uow = uow;
32: }
33:
34: /**
35: * Creates an indirect collection which wraps the specified collection and propagates changes to the current
36: * persistence context.
37: *
38: * @param collection The collection to be proxied
39: * @param owner Collection owner instance
40: * @param field Field whose value the collection is
41: * @return Indirect collection wrapping the collection
42: */
43: public IndirectCollection<?> createIndirectCollection(Object collection, Object owner, Field field) {
44: final IndirectCollection<?> res;
45:• if (collection instanceof List) {
46: res = new IndirectList<>(owner, field, uow, (List<?>) collection);
47:• } else if (collection instanceof Set) {
48: res = new IndirectSet<>(owner, field, uow, (Set<?>) collection);
49:• } else if (collection instanceof Map) {
50: res = new IndirectMap<>(owner, field, uow, (Map<?, ?>) collection);
51: } else {
52: throw new UnsupportedOperationException("Unsupported collection type " + collection.getClass());
53: }
54: return res;
55: }
56:
57: /**
58: * Creates an instance of a {@link Collection} implementation best matching the specified instance.
59: * <p>
60: * E.g. for any kind of {@link List}, an {@link ArrayList} is returned.
61: *
62: * @param collection Create matching instance for this collection
63: * @return Best matching collection instance
64: */
65: public static Collection<?> createInstance(Collection<?> collection) {
66: Objects.requireNonNull(collection);
67:
68:• if (collection instanceof List) {
69: return new ArrayList<>(collection.size());
70:• } else if (collection instanceof Set) {
71: return new HashSet<>(collection.size());
72: }
73: throw new IllegalArgumentException("Unsupported collection type: " + collection);
74: }
75: }