Skip to content

Package: IndirectWrapperHelper

IndirectWrapperHelper

nameinstructionbranchcomplexitylinemethod
IndirectWrapperHelper(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%
createIndirectWrapper(Object, Object, Field)
M: 17 C: 56
77%
M: 2 C: 8
80%
M: 2 C: 4
67%
M: 1 C: 9
90%
M: 0 C: 1
100%
requiresIndirectWrapper(Object)
M: 0 C: 13
100%
M: 0 C: 6
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
static {...}
M: 0 C: 1
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

1: /**
2: * Copyright (C) 2022 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.sessions;
16:
17: import cz.cvut.kbss.jopa.adapters.IndirectList;
18: import cz.cvut.kbss.jopa.adapters.IndirectMap;
19: import cz.cvut.kbss.jopa.adapters.IndirectMultilingualString;
20: import cz.cvut.kbss.jopa.adapters.IndirectSet;
21: import cz.cvut.kbss.jopa.model.MultilingualString;
22:
23: import java.lang.reflect.Field;
24: import java.util.Collection;
25: import java.util.List;
26: import java.util.Map;
27: import java.util.Set;
28:
29: /**
30: * Helper for dealing with indirect wrappers (mainly collections, multilingual strings).
31: * <p>
32: * Indirect wrappers are used to ensure that changes to the wrapped instances are propagated into the persistence
33: * context during transactions.
34: */
35: class IndirectWrapperHelper {
36:
37: private final UnitOfWorkImpl uow;
38:
39: IndirectWrapperHelper(UnitOfWorkImpl uow) {
40: this.uow = uow;
41: }
42:
43: /**
44: * Creates an indirect wrapper for the specified {@code wrapped} object.
45: *
46: * @param wrapped The object to wrap
47: * @param owner Instance owning the wrapped object. Necessary to associate the changes with the correct subject
48: * @param field Field filled with the wrapped object
49: * @return A suitable indirect wrapper
50: */
51: Object createIndirectWrapper(Object wrapped, Object owner, Field field) {
52:• assert requiresIndirectWrapper(wrapped);
53:• if (wrapped instanceof List) {
54: return new IndirectList<>(owner, field, uow, (List<?>) wrapped);
55:• } else if (wrapped instanceof Set) {
56: return new IndirectSet<>(owner, field, uow, (Set<?>) wrapped);
57:• } else if (wrapped instanceof Map) {
58: return new IndirectMap<>(owner, field, uow, (Map<?, ?>) wrapped);
59:• } else if (wrapped instanceof MultilingualString) {
60: return new IndirectMultilingualString(owner, field, uow, (MultilingualString) wrapped);
61: } else {
62: throw new UnsupportedOperationException("Unsupported wrapped type " + wrapped.getClass());
63: }
64: }
65:
66: /**
67: * Checks whether the specified object is of a type requiring an indirect wrapper.
68: *
69: * @param target Target object to check
70: * @return {@code true} if an indirect wrapper is used for the specified target
71: */
72: static boolean requiresIndirectWrapper(Object target) {
73:• return target instanceof Collection || target instanceof MultilingualString || target instanceof Map;
74: }
75: }