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