Skip to content

Package: IndirectWrapperHelper

IndirectWrapperHelper

nameinstructionbranchcomplexitylinemethod
IndirectWrapperHelper(UnitOfWork)
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: 11 C: 56
84%
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: 16
100%
M: 0 C: 8
100%
M: 0 C: 5
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: * JOPA
3: * Copyright (C) 2024 Czech Technical University in Prague
4: *
5: * This library is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU Lesser General Public
7: * License as published by the Free Software Foundation; either
8: * version 3.0 of the License, or (at your option) any later version.
9: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library.
17: */
18: package cz.cvut.kbss.jopa.sessions;
19:
20: import cz.cvut.kbss.jopa.proxy.change.ChangeTrackingIndirectList;
21: import cz.cvut.kbss.jopa.proxy.change.ChangeTrackingIndirectMap;
22: import cz.cvut.kbss.jopa.proxy.change.ChangeTrackingIndirectMultilingualString;
23: import cz.cvut.kbss.jopa.proxy.change.ChangeTrackingIndirectSet;
24: import cz.cvut.kbss.jopa.model.MultilingualString;
25: import cz.cvut.kbss.jopa.proxy.lazy.LazyLoadingProxy;
26:
27: import java.lang.reflect.Field;
28: import java.util.Collection;
29: import java.util.List;
30: import java.util.Map;
31: import java.util.Set;
32:
33: /**
34: * Helper for dealing with indirect wrappers (mainly collections, multilingual strings).
35: * <p>
36: * Indirect wrappers are used to ensure that changes to the wrapped instances are propagated into the persistence
37: * context during transactions.
38: */
39: class IndirectWrapperHelper {
40:
41: private final UnitOfWork uow;
42:
43: IndirectWrapperHelper(UnitOfWork uow) {
44: this.uow = uow;
45: }
46:
47: /**
48: * Creates an indirect wrapper for the specified {@code wrapped} object.
49: *
50: * @param wrapped The object to wrap
51: * @param owner Instance owning the wrapped object. Necessary to associate the changes with the correct subject
52: * @param field Field filled with the wrapped object
53: * @return A suitable indirect wrapper
54: */
55: Object createIndirectWrapper(Object wrapped, Object owner, Field field) {
56:• assert requiresIndirectWrapper(wrapped);
57:• if (wrapped instanceof List) {
58: return new ChangeTrackingIndirectList<>(owner, field, uow, (List<?>) wrapped);
59:• } else if (wrapped instanceof Set) {
60: return new ChangeTrackingIndirectSet<>(owner, field, uow, (Set<?>) wrapped);
61:• } else if (wrapped instanceof Map) {
62: return new ChangeTrackingIndirectMap<>(owner, field, uow, (Map<?, ?>) wrapped);
63:• } else if (wrapped instanceof MultilingualString) {
64: return new ChangeTrackingIndirectMultilingualString(owner, field, uow, (MultilingualString) wrapped);
65: } else {
66: throw new UnsupportedOperationException("Unsupported wrapped type " + wrapped.getClass());
67: }
68: }
69:
70: /**
71: * Checks whether the specified object is of a type requiring an indirect wrapper.
72: *
73: * @param target Target object to check
74: * @return {@code true} if an indirect wrapper is used for the specified target
75: */
76: static boolean requiresIndirectWrapper(Object target) {
77:• return (!(target instanceof LazyLoadingProxy)) && (target instanceof Collection || target instanceof MultilingualString || target instanceof Map);
78: }
79: }