Skip to content

Method: unwrap()

1: /*
2: * JOPA
3: * Copyright (C) 2024 Czech Technical University in Prague
4: *
5: * This library is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU Lesser General Public
7: * License as published by the Free Software Foundation; either
8: * version 3.0 of the License, or (at your option) any later version.
9: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library.
17: */
18: package cz.cvut.kbss.jopa.proxy.change;
19:
20: import cz.cvut.kbss.jopa.sessions.UnitOfWork;
21:
22: import java.lang.reflect.Field;
23: import java.util.Collection;
24: import java.util.Iterator;
25: import java.util.List;
26: import java.util.ListIterator;
27: import java.util.Objects;
28:
29: public class ChangeTrackingIndirectList<E> extends ChangeTrackingIndirectCollection<List<E>> implements List<E> {
30:
31: private final List<E> internalList;
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 ChangeTrackingIndirectList(Object owner, Field f, UnitOfWork 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: internalList.addAll(c);
63: persistChange();
64: return true;
65: }
66:
67: @Override
68: public boolean addAll(int index, Collection<? extends E> c) {
69: boolean res = internalList.addAll(index, c);
70: if (res) {
71: persistChange();
72: }
73: return res;
74: }
75:
76: @Override
77: public void clear() {
78: internalList.clear();
79: persistChange();
80: }
81:
82: @Override
83: public boolean contains(Object arg0) {
84: return internalList.contains(arg0);
85: }
86:
87: @Override
88: public boolean containsAll(Collection<?> arg0) {
89: return internalList.containsAll(arg0);
90: }
91:
92: @Override
93: public E get(int arg0) {
94: return internalList.get(arg0);
95: }
96:
97: @Override
98: public int indexOf(Object arg0) {
99: return internalList.indexOf(arg0);
100: }
101:
102: @Override
103: public boolean isEmpty() {
104: return internalList.isEmpty();
105: }
106:
107: @Override
108: public Iterator<E> iterator() {
109: return new IndirectIterator(internalList.iterator());
110: }
111:
112: @Override
113: public int lastIndexOf(Object arg0) {
114: return internalList.lastIndexOf(arg0);
115: }
116:
117: @Override
118: public ListIterator<E> listIterator() {
119: return new IndirectListIterator(internalList.listIterator());
120: }
121:
122: @Override
123: public ListIterator<E> listIterator(int arg0) {
124: return new IndirectListIterator(internalList.listIterator(arg0));
125: }
126:
127: @Override
128: public boolean remove(Object arg0) {
129: boolean res = internalList.remove(arg0);
130: if (res) {
131: persistChange();
132: }
133: return res;
134: }
135:
136: @Override
137: public E remove(int arg0) {
138: E elem = internalList.remove(arg0);
139: persistChange();
140: return elem;
141: }
142:
143: @Override
144: public boolean removeAll(Collection<?> arg0) {
145: boolean res = internalList.removeAll(arg0);
146: if (res) {
147: persistChange();
148: }
149: return res;
150: }
151:
152: @Override
153: public boolean retainAll(Collection<?> arg0) {
154: boolean res = internalList.retainAll(arg0);
155: if (res) {
156: persistChange();
157: }
158: return res;
159: }
160:
161: @Override
162: public E set(int arg0, E arg1) {
163: E elem = internalList.set(arg0, arg1);
164: persistChange();
165: return elem;
166: }
167:
168: @Override
169: public int size() {
170: return internalList.size();
171: }
172:
173: @Override
174: public List<E> subList(int fromIndex, int toIndex) {
175: return new ChangeTrackingIndirectList<>(owner, field, persistenceContext, internalList.subList(fromIndex, toIndex));
176: }
177:
178: @Override
179: public Object[] toArray() {
180: return internalList.toArray();
181: }
182:
183: @Override
184: public <T> T[] toArray(T[] arg0) {
185: return internalList.toArray(arg0);
186: }
187:
188: @Override
189: public List<E> unwrap() {
190: return internalList;
191: }
192:
193: @Override
194: public boolean equals(Object o) {
195: if (o instanceof List) {
196: if (o instanceof ChangeTrackingIndirectList) {
197: return internalList.equals(((ChangeTrackingIndirectList) o).internalList);
198: }
199: return internalList.equals(o);
200: }
201: return false;
202: }
203:
204: @Override
205: public int hashCode() {
206: return internalList.hashCode();
207: }
208:
209: @Override
210: public String toString() {
211: return internalList.toString();
212: }
213:
214: private class IndirectIterator implements Iterator<E> {
215:
216: private final Iterator<E> it;
217:
218: private IndirectIterator(Iterator<E> it) {
219: this.it = it;
220: }
221:
222: @Override
223: public boolean hasNext() {
224: return it.hasNext();
225: }
226:
227: @Override
228: public E next() {
229: return it.next();
230: }
231:
232: @Override
233: public void remove() {
234: it.remove();
235: ChangeTrackingIndirectList.this.persistChange();
236: }
237: }
238:
239: private class IndirectListIterator implements ListIterator<E> {
240:
241: private final ListIterator<E> lit;
242:
243: private IndirectListIterator(ListIterator<E> lit) {
244: this.lit = lit;
245: }
246:
247: @Override
248: public boolean hasNext() {
249: return lit.hasNext();
250: }
251:
252: @Override
253: public E next() {
254: return lit.next();
255: }
256:
257: @Override
258: public boolean hasPrevious() {
259: return lit.hasPrevious();
260: }
261:
262: @Override
263: public E previous() {
264: return lit.previous();
265: }
266:
267: @Override
268: public int nextIndex() {
269: return lit.nextIndex();
270: }
271:
272: @Override
273: public int previousIndex() {
274: return lit.previousIndex();
275: }
276:
277: @Override
278: public void remove() {
279: lit.remove();
280: ChangeTrackingIndirectList.this.persistChange();
281: }
282:
283: @Override
284: public void set(E e) {
285: lit.set(e);
286: ChangeTrackingIndirectList.this.persistChange();
287: }
288:
289: @Override
290: public void add(E e) {
291: lit.add(e);
292: ChangeTrackingIndirectList.this.persistChange();
293: }
294: }
295: }