Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: adf39509494e717f90d43fc2952d84c423066814 (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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/*****************************************************************************
 * Copyright (c) 2014, 2015 CEA LIST, 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:
 *  Laurent Wouters laurent.wouters@cea.fr - Initial API and implementation
 *  Christian W. Damus - bug 465416
 *
 *****************************************************************************/
package org.eclipse.papyrus.infra.sync;

import java.util.ArrayDeque;
import java.util.List;
import java.util.Queue;

import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;

/**
 * Represents a bucket of synchronized items.
 * A bucket contains synchronized items but has no assumption of which one, if any is the source of synchronization events.
 * All items in a bucket are however synchronized on the same events.
 * All items must then refer to the same model element (of type M).
 * The real client (backend) elements to synchronize are the elements of type T encapsulated into the SyncItems.
 * A bucket is associated with a set of synchronization feature (SyncFeature), defining which aspects of the elements in this bucket must be synchronize.
 *
 * @author Laurent Wouters
 *
 * @param <M>
 *            The type of the underlying model element common to all synchronized items in a single bucket
 * @param <T>
 *            The type of the backend element to synchronize
 */
public abstract class SyncBucket<M, T, X> extends SyncObject {
	/**
	 * The model element common to all synchronized items in this bucket
	 */
	private M model;
	/**
	 * The synchronized features
	 */
	private List<SyncFeature<M, T, X>> features;
	/**
	 * The slave synchronized items
	 */
	private SyncItemList<M, T> items;
	/**
	 * The pending synchronization requests
	 */
	private Queue<SyncRequest<M, T, X>> requests;
	/**
	 * The synchronization request currently being processed
	 */
	private SyncRequest<M, T, X> currentRequest;


	/**
	 * Gets the model element common to all synchronized items in this bucket
	 *
	 * @return The model element common to all synchronized items in this bucket
	 */
	public M getModel() {
		return model;
	}

	/**
	 * Initializes this bucket for the specified common model element
	 *
	 * @param model
	 *            The common model element for all items in this bucket
	 * @throws IllegalArgumentException
	 *             when the model element is null
	 */
	public SyncBucket(M model) throws IllegalArgumentException {
		super();

		if (model == null) {
			throw new IllegalArgumentException("The model element must not be null");
		}
		this.model = model;
		this.features = Lists.newArrayListWithExpectedSize(2);
		this.items = new SyncItemList<M, T>();
		this.requests = new ArrayDeque<SyncRequest<M, T, X>>();
	}

	boolean checkEnabled() {
		boolean result = isActive();

		if (!result) {
			// Bucket is dead
			clear();
		}

		return result;
	}

	/**
	 * Gets the synchronization item corresponding to the given backend element in this bucket
	 *
	 * @param element
	 *            A backend element
	 * @throws IllegalArgumentException
	 *             When the specified element is null
	 */
	public SyncItem<M, T> get(T element) throws IllegalArgumentException {
		if (element == null) {
			throw new IllegalArgumentException("The specified element must not be null");
		}
		return checkEnabled() ? items.get(element) : null;
	}

	protected Iterable<SyncItem<M, T>> getItems() {
		return items;
	}

	public T findBackend(final Predicate<? super T> predicate) {
		Predicate<SyncItem<M, T>> itemPredicate = new Predicate<SyncItem<M, T>>() {
			@Override
			public boolean apply(SyncItem<M, T> input) {
				return predicate.apply(input.getBackend());
			}
		};
		SyncItem<M, T> item = Iterables.find(getItems(), itemPredicate, null);
		return (item == null) ? null : item.getBackend();
	}

	/**
	 * Adds the specified element to this bucket to be synchronize
	 *
	 * @param element
	 *            A backend element
	 * @throws IllegalArgumentException
	 *             When the specified element is null
	 */
	public SyncItem<M, T> add(T element) throws IllegalArgumentException {
		if (element == null) {
			throw new IllegalArgumentException("The specified element must not be null");
		}
		if (!checkEnabled()) {
			return null;
		}

		SyncItem<M, T> result = items.get(element);
		if (result == null) {
			result = encapsulate(element);
			items.add(result);
			onNew(result);
		}

		return result;
	}

	/**
	 * Removes the specified element from this bucket
	 *
	 * @param element
	 *            An element to remove
	 * @return the corresponding sync item that was removed, if any
	 * @throws IllegalArgumentException
	 *             When the specified element is null
	 */
	public SyncItem<M, T> remove(T element) throws IllegalArgumentException {
		if (element == null) {
			throw new IllegalArgumentException("The specified element must not be null");
		}
		if (!checkEnabled()) {
			return null;
		}

		SyncItem<M, T> result = items.get(element);
		if (result != null) {
			items.remove(result);
		}

		return result;
	}

	/**
	 * Queries whether the specified back-end element is synchronized by this bucket.
	 * 
	 * @param backEnd
	 *            a potentially synchronized back-end
	 * 
	 * @return whether the back-end is synchronized by this bucket, even if this is now obsolete
	 *         knowledge because either or both are no longer {@linkplain ISyncObject#isActive() active}
	 */
	public boolean synchronizes(T backend) {
		return items.get(backend) != null;
	}

	protected Iterable<SyncFeature<M, T, X>> getFeatures() {
		return features;
	}

	/**
	 * Adds the specified synchronization feature to this bucket
	 *
	 * @param feature
	 *            A synchronization feature
	 * @throws IllegalArgumentException
	 *             When the specified feature is null
	 */
	public void add(SyncFeature<M, T, X> feature) throws IllegalArgumentException {
		if (feature == null) {
			throw new IllegalArgumentException("The specified element must not be null");
		}
		if (!checkEnabled()) {
			return;
		}
		features.add(feature);
		onNew(feature);
	}

	/**
	 * Removes the specified synchronization feature from this bucket
	 *
	 * @param feature
	 *            A synchronization feature
	 * @throws IllegalArgumentException
	 *             When the specified feature is null
	 */
	public void remove(SyncFeature<M, T, X> feature) throws IllegalArgumentException {
		if (feature == null) {
			throw new IllegalArgumentException("The specified element must not be null");
		}
		if (!checkEnabled()) {
			return;
		}
		features.remove(feature);
	}

	/**
	 * Clears this bucket
	 */
	public void clear() {
		for (SyncFeature<M, T, X> feature : features) {
			feature.clear();
		}
		model = null;
		features.clear();
		items.clear();
		requests.clear();
	}

	/**
	 * Propagates the specified change
	 *
	 * @param feature
	 *            The propagating feature
	 * @param origin
	 *            The item at the origin of the change
	 * @param message
	 *            The change message
	 */
	public void propagate(SyncFeature<M, T, X> feature, SyncItem<M, T> origin, X message) {
		if (!accept(feature, origin, message)) {
			return;
		}

		requests.add(new SyncRequest<M, T, X>(feature, origin, message));

		// If we are not already processing requests, process them now
		if (currentRequest == null) {
			try {
				for (currentRequest = requests.poll(); currentRequest != null; currentRequest = requests.poll()) {
					executeCurrentRequest();
				}
			} finally {
				// In case of exception
				currentRequest = null;
			}
		}
	}

	/**
	 * Executes a synchronization request
	 *
	 * @param request
	 *            The request to execute
	 */
	private void executeCurrentRequest() {
		final SyncItem<M, T> from = currentRequest.getOrigin();
		final SyncFeature<M, T, X> feature = currentRequest.getFeature();

		for (SyncItem<M, T> to : getSyncService().getSyncPolicy().filter(from, getItems(), feature)) {
			feature.synchronize(from, to, currentRequest.getMessage());
		}
	}

	/**
	 * Encapsulates the specified element into an synchronized item for this bucket
	 *
	 * @param element
	 *            An element to synchronize
	 * @return The encapsulating item
	 */
	protected abstract SyncItem<M, T> encapsulate(T element);

	/**
	 * Callback for the initialization of new synchronized items
	 *
	 * @param item
	 *            The new item to initialize
	 */
	protected void onNew(SyncItem<M, T> item) {
		// by default, do nothing
	}

	/**
	 * Callback for the initialization of new synchronization features
	 *
	 * @param feature
	 *            The new synchronization feature
	 */
	protected void onNew(SyncFeature<M, T, X> feature) {
		// by default, do nothing
	}

	/**
	 * Determines whether to accept the specified incoming synchronization request for processing.
	 *
	 * @param feature
	 *            The propagating feature
	 * @param origin
	 *            The item at the origin of the change
	 * @param message
	 *            The change message
	 * @return <code>true</code> if the request shall be processed
	 */
	protected boolean accept(SyncFeature<M, T, X> feature, SyncItem<M, T> origin, X message) {
		// by default, accept all
		return true;
	}
}

Back to the top