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