Skip to content

Method: isEmpty()

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.proxy.change;
19:
20: import cz.cvut.kbss.jopa.proxy.IndirectWrapper;
21: import cz.cvut.kbss.jopa.model.MultilingualString;
22: import cz.cvut.kbss.jopa.sessions.UnitOfWork;
23:
24: import java.lang.reflect.Field;
25: import java.util.Map;
26: import java.util.Objects;
27: import java.util.Set;
28:
29: /**
30: * Wraps a {@link MultilingualString} so that calls to modifying operations are intercepted and reported to the
31: * persistence context (if necessary).
32: */
33: public class ChangeTrackingIndirectMultilingualString extends MultilingualString implements IndirectWrapper {
34:
35: private final transient Object owner;
36: private final transient Field field;
37: private final transient UnitOfWork persistenceContext;
38:
39: private final MultilingualString referencedString;
40:
41: /**
42: * Create new indirect multilingual string backed by the specified referenced {@link MultilingualString}.
43: *
44: * @param owner Owner of the string
45: * @param f The field holding this string
46: * @param uow Persistence context the owner belongs to
47: * @param referencedString The string to reference
48: * @throws NullPointerException If the {@code referencedString} is null
49: */
50: public ChangeTrackingIndirectMultilingualString(Object owner, Field f, UnitOfWork uow, MultilingualString referencedString) {
51: this.owner = owner;
52: this.field = f;
53: this.persistenceContext = Objects.requireNonNull(uow);
54: this.referencedString = Objects.requireNonNull(referencedString);
55: }
56:
57: private void notifyPersistenceContext() {
58: assert persistenceContext != null;
59: if (persistenceContext.isInTransaction() && !persistenceContext.isFlushingChanges()) {
60: persistenceContext.attributeChanged(owner, field);
61: }
62: }
63:
64: @Override
65: public MultilingualString set(String language, String value) {
66: referencedString.set(language, value);
67: notifyPersistenceContext();
68: return this;
69: }
70:
71: @Override
72: public MultilingualString set(String value) {
73: referencedString.set(value);
74: notifyPersistenceContext();
75: return this;
76: }
77:
78: @Override
79: public String get(String language) {
80: return referencedString.get(language);
81: }
82:
83: @Override
84: public String get() {
85: return referencedString.get();
86: }
87:
88: @Override
89: public boolean contains(String language) {
90: return referencedString.contains(language);
91: }
92:
93: @Override
94: public boolean containsSimple() {
95: return referencedString.containsSimple();
96: }
97:
98: @Override
99: public boolean isEmpty() {
100: return referencedString.isEmpty();
101: }
102:
103: @Override
104: public void remove(String language) {
105: referencedString.remove(language);
106: notifyPersistenceContext();
107: }
108:
109: @Override
110: public Set<String> getLanguages() {
111: return referencedString.getLanguages();
112: }
113:
114: @Override
115: public Map<String, String> getValue() {
116: return referencedString.getValue();
117: }
118:
119: @Override
120: public boolean equals(Object o) {
121: if (o instanceof MultilingualString) {
122: if (o instanceof ChangeTrackingIndirectMultilingualString) {
123: return referencedString.equals(((ChangeTrackingIndirectMultilingualString) o).referencedString);
124: }
125: return referencedString.equals(o);
126: }
127: return false;
128: }
129:
130: @Override
131: public int hashCode() {
132: return referencedString.hashCode();
133: }
134:
135: @Override
136: public String toString() {
137: return referencedString.toString();
138: }
139:
140: @Override
141: public MultilingualString unwrap() {
142: return referencedString;
143: }
144: }