Skip to main content
summaryrefslogtreecommitdiffstats
blob: 7b5a459ec684e9a8cfe356078e8c710e9022ce80 (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
/*******************************************************************************
 * Copyright (c) 2008, 2009 Oracle. 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:
 *     Oracle - initial API and implementation
 ******************************************************************************/
package org.eclipse.jpt.utility.internal.synchronizers;

import java.io.Serializable;

/**
 * This interface defines the protocol for starting, stopping, and executing a
 * long-running, repeatable, and possibly recursive "synchronization" process.
 * The intent is for the synchronizer to synchronize a "secondary" model with
 * a "primary" model. Any change to the "primary" model will trigger the
 * synchronization. The synchronizer implementation will determine whether the
 * "secondary" model remains in sync synchronously or asynchronously.
 * <p>
 * The assumption is that the {@link #start()} and {@link #stop()} methods will be called from
 * a single master thread that would control the synchronizer's lifecycle and
 * the {@link #synchronize()} method will be called multiple times, possibly from
 * multiple threads.
 */
public interface Synchronizer {

	/**
	 * Enable the synchronizer to allow future synchronizations as requested
	 * by calls to {@link #synchronize()}.
	 */
	void start();

	/**
	 * Synchronize the dependent model with the primary model. Do nothing if
	 * {@link #start()} has not previously been called. Do nothing if {@link #stop}
	 * has been called (without any intermediate call to {@link #start()}.
	 */
	void synchronize();

	/**
	 * Stop the synchronizer immediately or, if a synchronization is currently
	 * in progress, when it completes. Return when the synchronizer is stopped.
	 * No further synchonizations will performed until {@link #start()} is called.
	 */
	void stop();


	/**
	 * Singleton implementation of the {@link Synchronizer} interface that will do
	 * nothing.
	 */
	final class Null implements Synchronizer, Serializable {
		public static final Synchronizer INSTANCE = new Null();
		public static Synchronizer instance() {
			return INSTANCE;
		}
		// ensure single instance
		private Null() {
			super();
		}
		public void start() {
			// do nothing
		}
		public void synchronize() {
			// do nothing
		}
		public void stop() {
			// do nothing
		}
		@Override
		public String toString() {
			return "Synchronizer.Null"; //$NON-NLS-1$
		}
		private static final long serialVersionUID = 1L;
		private Object readResolve() {
			// replace this object with the singleton
			return INSTANCE;
		}
	}

}

Back to the top