Skip to content

Package: IndirectSet$IndirectSetIterator

IndirectSet$IndirectSetIterator

nameinstructionbranchcomplexitylinemethod
IndirectSet.IndirectSetIterator(IndirectSet, Iterator)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
hasNext()
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
next()
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
remove()
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%

Coverage

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 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: import cz.cvut.kbss.jopa.sessions.UnitOfWorkImpl;
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:                 super();
34:                 this.internalSet = new HashSet<>();
35:         }
36:
37:         public IndirectSet(Object owner, Field f, UnitOfWorkImpl uow, Set<E> referencedSet) {
38:                 super(owner, f, uow);
39:                 if (referencedSet == null) {
40:                         throw new NullPointerException("Null passed in as the referencedSet.");
41:                 }
42:                 this.internalSet = referencedSet;
43:         }
44:
45:         @Override
46:         public int size() {
47:                 return internalSet.size();
48:         }
49:
50:         @Override
51:         public boolean isEmpty() {
52:                 return internalSet.isEmpty();
53:         }
54:
55: @Override
56:         public boolean contains(Object o) {
57:                 return internalSet.contains(o);
58:         }
59:
60: @Override
61:         public Iterator<E> iterator() {
62:                 return new IndirectSetIterator<E>(internalSet.iterator());
63:         }
64:
65: @Override
66:         public Object[] toArray() {
67:                 return internalSet.toArray();
68:         }
69:
70: @Override
71:         public <T> T[] toArray(T[] a) {
72:                 return internalSet.toArray(a);
73:         }
74:
75: @Override
76:         public boolean add(E e) {
77:                 boolean res = internalSet.add(e);
78:                 if (res) {
79:                         persistChange();
80:                 }
81:                 return res;
82:         }
83:
84: @Override
85:         public boolean remove(Object o) {
86:                 boolean res = internalSet.remove(o);
87:                 if (res) {
88:                         persistChange();
89:                 }
90:                 return res;
91:         }
92:
93: @Override
94:         public boolean containsAll(Collection<?> c) {
95:                 return internalSet.containsAll(c);
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: }