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