Skip to main content
summaryrefslogtreecommitdiffstats
blob: 0373d7ff94006932c34fa1afe83921ff4a1209a7 (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
/*******************************************************************************
 * Copyright (c) 2012, 2013 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.common.utility.internal.command;

import org.eclipse.jpt.common.utility.command.Command;
import org.eclipse.jpt.common.utility.command.StatefulCommandContext;
import org.eclipse.jpt.common.utility.internal.ObjectTools;
import org.eclipse.jpt.common.utility.internal.collection.LinkedQueue;
import org.eclipse.jpt.common.utility.internal.reference.SynchronizedBoolean;

/**
 * This is a command context that queues up any commands
 * that are {@link #execute(Command) executed} while the context is
 * {@link #stop() stopped}. These commands will be
 * executed when the context is {@link #start() started}.
 */
public abstract class AbstractQueueingCommandContext<E extends StatefulCommandContext>
	implements StatefulCommandContext
{
	protected final E commandContext;
	protected final SynchronizedBoolean active = new SynchronizedBoolean(false);
	private LinkedQueue<Command> queue = new LinkedQueue<Command>();


	protected AbstractQueueingCommandContext(E commandContext) {
		super();
		if (commandContext == null) {
			throw new NullPointerException();
		}
		this.commandContext = commandContext;
	}

	/**
	 * Start the command context, executing all the commands that were
	 * queued up while the context was {@link #stop() stopped}.
	 */
	public synchronized void start() {
		if (this.active.isTrue()) {
			throw new IllegalStateException("Not stopped."); //$NON-NLS-1$
		}
		this.commandContext.start();
		while ( ! this.queue.isEmpty()) {
			this.commandContext.execute(this.queue.dequeue());
		}
		this.active.setTrue();
	}

	/**
	 * If the command context is active, execute the specified command;
	 * otherwise, queue the command to be executed once the context is
	 * {@link #start() started}.
	 */
	public void execute(Command command) {
		if (this.commandIsToBeExecuted(command)) {
			this.commandContext.execute(command);
		}
	}

	/**
	 * Return whether the command context is active and, if it is <em>in</em>active,
	 * place the specified command in the queue for later execution.
	 */
	private synchronized boolean commandIsToBeExecuted(Command command) {
		if (this.active.isFalse()) {
			this.queue.enqueue(command);
			return false;
		}
		return true;
	}

	/**
	 * Stop the command context. Any further requests to
	 * {@link #execute(Command) execute} a command will result in the command
	 * being queued up to be executed once the command context is
	 * {@link #start() restarted}.
	 */
	public synchronized void stop() throws InterruptedException {
		if (this.active.isFalse()) {
			throw new IllegalStateException("Not started."); //$NON-NLS-1$
		}
		this.active.setFalse();
		this.commandContext.stop();
	}

	@Override
	public String toString() {
		return ObjectTools.toString(this, this.queue);
	}
}

Back to the top