Package: IndirectList$IndirectListIterator
IndirectList$IndirectListIterator
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
IndirectList.IndirectListIterator(IndirectList, ListIterator) |
|
|
|
|
|
||||||||||||||||||||
add(Object) |
|
|
|
|
|
||||||||||||||||||||
hasNext() |
|
|
|
|
|
||||||||||||||||||||
hasPrevious() |
|
|
|
|
|
||||||||||||||||||||
next() |
|
|
|
|
|
||||||||||||||||||||
nextIndex() |
|
|
|
|
|
||||||||||||||||||||
previous() |
|
|
|
|
|
||||||||||||||||||||
previousIndex() |
|
|
|
|
|
||||||||||||||||||||
remove() |
|
|
|
|
|
||||||||||||||||||||
set(Object) |
|
|
|
|
|
Coverage
1: /**
2: * Copyright (C) 2023 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.*;
21:
22: public class IndirectList<E> extends IndirectCollection<List<E>> implements List<E> {
23:
24: private final List<E> internalList;
25:
26: /**
27: * No-arg constructor to allow clone building.
28: */
29: IndirectList() {
30: this.internalList = new ArrayList<>();
31: }
32:
33: /**
34: * Create new indirect list backed by the specified referenced list.
35: *
36: * @param owner Owner of the list
37: * @param f The field holding this list
38: * @param uow Persistence context the owner belongs to
39: * @param referencedList The list to reference
40: * @throws NullPointerException If the {@code referencedList} is null
41: */
42: public IndirectList(Object owner, Field f, UnitOfWorkImpl uow, List<E> referencedList) {
43: super(owner, f, uow);
44: this.internalList = Objects.requireNonNull(referencedList);
45: }
46:
47: @Override
48: public boolean add(E arg0) {
49: internalList.add(arg0);
50: persistChange(); // There is always a change
51: return true;
52: }
53:
54: @Override
55: public void add(int arg0, E arg1) {
56: internalList.add(arg0, arg1);
57: persistChange();
58: }
59:
60: @Override
61: public boolean addAll(Collection<? extends E> c) {
62: boolean res = internalList.addAll(c);
63: if (res) {
64: persistChange();
65: }
66: return res;
67: }
68:
69: @Override
70: public boolean addAll(int index, Collection<? extends E> c) {
71: boolean res = internalList.addAll(index, c);
72: if (res) {
73: persistChange();
74: }
75: return res;
76: }
77:
78: @Override
79: public void clear() {
80: internalList.clear();
81: persistChange();
82: }
83:
84: @Override
85: public boolean contains(Object arg0) {
86: return internalList.contains(arg0);
87: }
88:
89: @Override
90: public boolean containsAll(Collection<?> arg0) {
91: return internalList.containsAll(arg0);
92: }
93:
94: @Override
95: public E get(int arg0) {
96: return internalList.get(arg0);
97: }
98:
99: @Override
100: public int indexOf(Object arg0) {
101: return internalList.indexOf(arg0);
102: }
103:
104: @Override
105: public boolean isEmpty() {
106: return internalList.isEmpty();
107: }
108:
109: @Override
110: public Iterator<E> iterator() {
111: return new IndirectIterator(internalList.iterator());
112: }
113:
114: @Override
115: public int lastIndexOf(Object arg0) {
116: return internalList.lastIndexOf(arg0);
117: }
118:
119: @Override
120: public ListIterator<E> listIterator() {
121: return new IndirectListIterator(internalList.listIterator());
122: }
123:
124: @Override
125: public ListIterator<E> listIterator(int arg0) {
126: return new IndirectListIterator(internalList.listIterator(arg0));
127: }
128:
129: @Override
130: public boolean remove(Object arg0) {
131: boolean res = internalList.remove(arg0);
132: if (res) {
133: persistChange();
134: }
135: return res;
136: }
137:
138: @Override
139: public E remove(int arg0) {
140: E elem = internalList.remove(arg0);
141: persistChange();
142: return elem;
143: }
144:
145: @Override
146: public boolean removeAll(Collection<?> arg0) {
147: boolean res = internalList.removeAll(arg0);
148: if (res) {
149: persistChange();
150: }
151: return res;
152: }
153:
154: @Override
155: public boolean retainAll(Collection<?> arg0) {
156: boolean res = internalList.retainAll(arg0);
157: if (res) {
158: persistChange();
159: }
160: return res;
161: }
162:
163: @Override
164: public E set(int arg0, E arg1) {
165: E elem = internalList.set(arg0, arg1);
166: persistChange();
167: return elem;
168: }
169:
170: @Override
171: public int size() {
172: return internalList.size();
173: }
174:
175: @Override
176: public List<E> subList(int fromIndex, int toIndex) {
177: return new IndirectList<>(owner, field, persistenceContext, internalList.subList(fromIndex, toIndex));
178: }
179:
180: @Override
181: public Object[] toArray() {
182: return internalList.toArray();
183: }
184:
185: @Override
186: public <T> T[] toArray(T[] arg0) {
187: return internalList.toArray(arg0);
188: }
189:
190: @Override
191: public List<E> unwrap() {
192: return internalList;
193: }
194:
195: @Override
196: public boolean equals(Object o) {
197: if (o instanceof List) {
198: if (o instanceof IndirectList) {
199: return internalList.equals(((IndirectList) o).internalList);
200: }
201: return internalList.equals(o);
202: }
203: return false;
204: }
205:
206: @Override
207: public int hashCode() {
208: return internalList.hashCode();
209: }
210:
211: @Override
212: public String toString() {
213: return internalList.toString();
214: }
215:
216: private class IndirectIterator implements Iterator<E> {
217:
218: private final Iterator<E> it;
219:
220: private IndirectIterator(Iterator<E> it) {
221: this.it = it;
222: }
223:
224: @Override
225: public boolean hasNext() {
226: return it.hasNext();
227: }
228:
229: @Override
230: public E next() {
231: return it.next();
232: }
233:
234: @Override
235: public void remove() {
236: it.remove();
237: IndirectList.this.persistChange();
238: }
239: }
240:
241: private class IndirectListIterator implements ListIterator<E> {
242:
243: private final ListIterator<E> lit;
244:
245: private IndirectListIterator(ListIterator<E> lit) {
246: this.lit = lit;
247: }
248:
249: @Override
250: public boolean hasNext() {
251: return lit.hasNext();
252: }
253:
254: @Override
255: public E next() {
256: return lit.next();
257: }
258:
259: @Override
260: public boolean hasPrevious() {
261: return lit.hasPrevious();
262: }
263:
264: @Override
265: public E previous() {
266: return lit.previous();
267: }
268:
269: @Override
270: public int nextIndex() {
271: return lit.nextIndex();
272: }
273:
274: @Override
275: public int previousIndex() {
276: return lit.previousIndex();
277: }
278:
279: @Override
280: public void remove() {
281: lit.remove();
282: IndirectList.this.persistChange();
283: }
284:
285: @Override
286: public void set(E e) {
287: lit.set(e);
288: IndirectList.this.persistChange();
289: }
290:
291: @Override
292: public void add(E e) {
293: lit.add(e);
294: IndirectList.this.persistChange();
295: }
296: }
297: }