Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 236d80cfcefb300c456dfa3c6204c1cc6c29dbf9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*****************************************************************************
 * Copyright (c) 2015, 2016 Christian W. Damus and others.
 * 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   Christian W. Damus - Initial API and implementation
 *   
 *****************************************************************************/

package org.eclipse.papyrus.infra.tools.databinding;

import static org.eclipse.core.databinding.observable.Diffs.createListDiff;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

import org.eclipse.core.databinding.observable.Diffs;
import org.eclipse.core.databinding.observable.Realm;
import org.eclipse.core.databinding.observable.list.ListDiff;
import org.eclipse.core.databinding.observable.list.ListDiffEntry;
import org.eclipse.core.databinding.observable.list.ListDiffVisitor;
import org.eclipse.core.databinding.observable.list.WritableList;

/**
 * A specialization of the core Databindings {@link WritableList} providing
 * iterators that support modification.
 */
public class WritableListWithIterator<E> extends WritableList<E> implements ReferenceCountedObservable {

	private final ReferenceCountedObservable.Support refCount = new ReferenceCountedObservable.Support(this);

	private final ListDiffVisitor<E> mutationHook = createMutationHook();

	public WritableListWithIterator() {
		super();
	}

	public WritableListWithIterator(Realm realm) {
		super(realm);
	}

	public WritableListWithIterator(List<E> toWrap, Object elementType) {
		super(toWrap, elementType);
	}

	public WritableListWithIterator(Collection<E> collection, Object elementType) {
		super(collection, elementType);
	}

	public WritableListWithIterator(Realm realm, List<E> toWrap, Object elementType) {
		super(realm, toWrap, elementType);
	}

	public WritableListWithIterator(Realm realm, Collection<E> collection, Object elementType) {
		super(realm, collection, elementType);
	}

	//
	// Mutation hooks
	//

	void didAdd(E element) {
		// Pass
	}

	void didRemove(E element) {
		// Pass
	}

	private ListDiffVisitor<E> createMutationHook() {
		return new ListDiffVisitor<E>() {
			@Override
			public void handleAdd(int index, E element) {
				didAdd(element);
			}

			@Override
			public void handleRemove(int index, E element) {
				didRemove(element);
			}
		};
	}

	@Override
	protected void fireListChange(ListDiff<E> diff) {
		diff.accept(mutationHook);

		super.fireListChange(diff);
	}

	//
	// Reference counting
	//

	@Override
	public void retain() {
		refCount.retain();
	}

	@Override
	public void release() {
		refCount.release();
	}

	@Override
	public void autorelease() {
		refCount.autorelease();
	}

	//
	// Iteration
	//

	@Override
	public Iterator<E> iterator() {
		getterCalled();
		return new Iter();
	}

	@Override
	public ListIterator<E> listIterator() {
		getterCalled();
		return new ListIter(0);
	}

	@Override
	public ListIterator<E> listIterator(int index) {
		getterCalled();
		return new ListIter(index);
	}

	static <E> ListDiffEntry<E> added(E element, int position) {
		return Diffs.createListDiffEntry(position, true, element);
	}

	static <E> ListDiffEntry<E> removed(E element, int position) {
		return Diffs.createListDiffEntry(position, false, element);
	}

	//
	// Nested types
	//

	private class Iter implements Iterator<E> {

		final ListIterator<E> delegate;
		int lastReturned = -1;

		Iter() {
			this(0);
		}

		Iter(int index) {
			super();

			this.delegate = wrappedList.listIterator(index);
		}

		@Override
		public boolean hasNext() {
			return delegate.hasNext();
		}

		@Override
		public E next() {
			E result = delegate.next();
			lastReturned = delegate.previousIndex();
			return result;
		}

		E lastReturned() {
			return ((lastReturned >= 0) && (lastReturned < size()))
					? get(lastReturned)
					: null;
		}

		@Override
		public void remove() {
			E removed = lastReturned();

			delegate.remove();

			// We only get this far if remove succeeded
			fireListChange(createListDiff(removed(removed, lastReturned)));
		}
	}

	private class ListIter extends Iter implements ListIterator<E> {

		ListIter(int index) {
			super(index);
		}

		@Override
		public boolean hasPrevious() {
			return delegate.hasPrevious();
		}

		@Override
		public int nextIndex() {
			return delegate.nextIndex();
		}

		@Override
		public int previousIndex() {
			return delegate.previousIndex();
		}

		@Override
		public E previous() {
			E result = delegate.previous();
			lastReturned = delegate.nextIndex();
			return result;
		}

		@Override
		public void set(E e) {
			E removed = lastReturned();

			delegate.set(e);

			// We only get this far if remove succeeded
			fireListChange(createListDiff(removed(removed, lastReturned), added(e, lastReturned)));
		}

		@Override
		public void add(E e) {
			delegate.add(e);

			// We only get this far if add succeeded
			fireListChange(createListDiff(added(e, previousIndex())));
		}

	}

	/**
	 * A specialized writable list that owns its elements via strong (and counted) references.
	 * It does not support wrapping an externally-provided list.
	 */
	public static class Containment<E extends ReferenceCountedObservable> extends WritableListWithIterator<E> {

		public Containment() {
			super();
		}

		public Containment(Object elementType) {
			super(new ArrayList<E>(), elementType);
		}

		public Containment(Realm realm, Object elementType) {
			super(realm, new ArrayList<E>(), elementType);
		}

		public Containment(Realm realm) {
			super(realm);
		}

		@Override
		public synchronized void dispose() {
			super.dispose();

			// Release my contained elements
			wrappedList.forEach(ReferenceCountedObservable::release);
			wrappedList.clear();
		}

		@Override
		void didAdd(E element) {
			if (element != null) {
				element.retain();
			}
		}

		@Override
		void didRemove(E element) {
			if (element != null) {
				element.release();
			}
		}
	}
}

Back to the top