Skip to content

Method: removeAll(Collection)

1: /**
2: * Copyright (C) 2016 Czech Technical University in Prague
3: * <p>
4: * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
5: * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
6: * version.
7: * <p>
8: * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
9: * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
10: * details. You should have received a copy of the GNU General Public License along with this program. If not, see
11: * <http://www.gnu.org/licenses/>.
12: */
13: package cz.cvut.kbss.jopa.adapters;
14:
15: import cz.cvut.kbss.jopa.sessions.UnitOfWorkImpl;
16:
17: import java.lang.reflect.Field;
18: import java.util.*;
19:
20: public class IndirectSet<E> extends IndirectCollection<Set<E>> implements Set<E> {
21:
22: private Set<E> internalSet;
23:
24: /**
25: * No-arg constructor to allow clone building.
26: */
27: IndirectSet() {
28: this.internalSet = new HashSet<>();
29: }
30:
31: public IndirectSet(Object owner, Field f, UnitOfWorkImpl uow, Set<E> referencedSet) {
32: super(owner, f, uow);
33: this.internalSet = Objects.requireNonNull(referencedSet);
34: }
35:
36: @Override
37: public int size() {
38: return internalSet.size();
39: }
40:
41: @Override
42: public boolean isEmpty() {
43: return internalSet.isEmpty();
44: }
45:
46: @Override
47: public boolean contains(Object o) {
48: return internalSet.contains(o);
49: }
50:
51: @Override
52: public Iterator<E> iterator() {
53: return new IndirectSetIterator<>(internalSet.iterator());
54: }
55:
56: @Override
57: public Object[] toArray() {
58: return internalSet.toArray();
59: }
60:
61: @Override
62: public <T> T[] toArray(T[] a) {
63: return internalSet.toArray(a);
64: }
65:
66: @Override
67: public boolean add(E e) {
68: boolean res = internalSet.add(e);
69: if (res) {
70: persistChange();
71: }
72: return res;
73: }
74:
75: @Override
76: public boolean remove(Object o) {
77: boolean res = internalSet.remove(o);
78: if (res) {
79: persistChange();
80: }
81: return res;
82: }
83:
84: @Override
85: public boolean containsAll(Collection<?> c) {
86: return internalSet.containsAll(c);
87: }
88:
89: @Override
90:
91: public boolean addAll(Collection<? extends E> c) {
92: boolean res = internalSet.addAll(c);
93: if (res) {
94: persistChange();
95: }
96: return res;
97: }
98:
99: @Override
100: public boolean retainAll(Collection<?> c) {
101: boolean res = internalSet.retainAll(c);
102: if (res) {
103: persistChange();
104: }
105: return res;
106: }
107:
108: @Override
109: public boolean removeAll(Collection<?> c) {
110: boolean res = internalSet.removeAll(c);
111:• if (res) {
112: persistChange();
113: }
114: return res;
115: }
116:
117: @Override
118: public void clear() {
119: internalSet.clear();
120: persistChange();
121: }
122:
123: private class IndirectSetIterator<T> implements Iterator<T> {
124:
125: private Iterator<T> iterator;
126:
127: private IndirectSetIterator(Iterator<T> iterator) {
128: this.iterator = iterator;
129: }
130:
131: @Override
132: public boolean hasNext() {
133: return iterator.hasNext();
134: }
135:
136: @Override
137: public T next() {
138: return iterator.next();
139: }
140:
141: @Override
142: public void remove() {
143: iterator.remove();
144: IndirectSet.this.persistChange();
145: }
146: }
147:
148: @Override
149: public Set<E> getReferencedCollection() {
150: return internalSet;
151: }
152:
153: @Override
154: public boolean equals(Object o) {
155: if (o instanceof Set) {
156: if (o instanceof IndirectSet) {
157: return internalSet.equals(((IndirectSet) o).internalSet);
158: }
159: return internalSet.equals(o);
160: }
161: return false;
162: }
163:
164: @Override
165: public int hashCode() {
166: return internalSet.hashCode();
167: }
168:
169: @Override
170: public String toString() {
171: return internalSet.toString();
172: }
173: }