Skip to contentMethod: remove(Object)
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.adapters;
16:
17: import cz.cvut.kbss.jopa.sessions.UnitOfWorkImpl;
18:
19: import java.lang.reflect.Field;
20: import java.util.Collection;
21: import java.util.HashSet;
22: import java.util.Iterator;
23: import java.util.Set;
24:
25: public class IndirectSet<E> extends IndirectCollection<Set<E>> implements Set<E> {
26:
27: private Set<E> internalSet;
28:
29: /**
30: * No-arg constructor to allow clone building.
31: */
32: IndirectSet() {
33: this.internalSet = new HashSet<>();
34: }
35:
36: public IndirectSet(Object owner, Field f, UnitOfWorkImpl uow, Set<E> referencedSet) {
37: super(owner, f, uow);
38: if (referencedSet == null) {
39: throw new NullPointerException("Null passed in as the referencedSet.");
40: }
41: this.internalSet = referencedSet;
42: }
43:
44: @Override
45: public int size() {
46: return internalSet.size();
47: }
48:
49: @Override
50: public boolean isEmpty() {
51: return internalSet.isEmpty();
52: }
53:
54: @Override
55: public boolean contains(Object o) {
56: return internalSet.contains(o);
57: }
58:
59: @Override
60: public Iterator<E> iterator() {
61: return new IndirectSetIterator<>(internalSet.iterator());
62: }
63:
64: @Override
65: public Object[] toArray() {
66: return internalSet.toArray();
67: }
68:
69: @Override
70: public <T> T[] toArray(T[] a) {
71: return internalSet.toArray(a);
72: }
73:
74: @Override
75: public boolean add(E e) {
76: boolean res = internalSet.add(e);
77: if (res) {
78: persistChange();
79: }
80: return res;
81: }
82:
83: @Override
84: public boolean remove(Object o) {
85: boolean res = internalSet.remove(o);
86:• if (res) {
87: persistChange();
88: }
89: return res;
90: }
91:
92: @Override
93: public boolean containsAll(Collection<?> c) {
94: return internalSet.containsAll(c);
95: }
96:
97: @Override
98:
99: public boolean addAll(Collection<? extends E> c) {
100: boolean res = internalSet.addAll(c);
101: if (res) {
102: persistChange();
103: }
104: return res;
105: }
106:
107: @Override
108: public boolean retainAll(Collection<?> c) {
109: boolean res = internalSet.retainAll(c);
110: if (res) {
111: persistChange();
112: }
113: return res;
114: }
115:
116: @Override
117: public boolean removeAll(Collection<?> c) {
118: boolean res = internalSet.removeAll(c);
119: if (res) {
120: persistChange();
121: }
122: return res;
123: }
124:
125: @Override
126: public void clear() {
127: internalSet.clear();
128: persistChange();
129: }
130:
131: private class IndirectSetIterator<T> implements Iterator<T> {
132:
133: private Iterator<T> iterator;
134:
135: private IndirectSetIterator(Iterator<T> iterator) {
136: this.iterator = iterator;
137: }
138:
139: @Override
140: public boolean hasNext() {
141: return iterator.hasNext();
142: }
143:
144: @Override
145: public T next() {
146: return iterator.next();
147: }
148:
149: @Override
150: public void remove() {
151: iterator.remove();
152: IndirectSet.this.persistChange();
153: }
154: }
155:
156: @Override
157: public Set<E> getReferencedCollection() {
158: return internalSet;
159: }
160:
161: @Override
162: public boolean equals(Object o) {
163: if (o instanceof Set) {
164: if (o instanceof IndirectSet) {
165: return internalSet.equals(((IndirectSet) o).internalSet);
166: }
167: return internalSet.equals(o);
168: }
169: return false;
170: }
171:
172: @Override
173: public int hashCode() {
174: return internalSet.hashCode();
175: }
176:
177: @Override
178: public String toString() {
179: return internalSet.toString();
180: }
181: }