Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/command/SingleUseQueueingExtendedCommandExecutor.java')
-rw-r--r--common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/command/SingleUseQueueingExtendedCommandExecutor.java78
1 files changed, 78 insertions, 0 deletions
diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/command/SingleUseQueueingExtendedCommandExecutor.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/command/SingleUseQueueingExtendedCommandExecutor.java
new file mode 100644
index 0000000000..ab7b130b40
--- /dev/null
+++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/command/SingleUseQueueingExtendedCommandExecutor.java
@@ -0,0 +1,78 @@
+/*******************************************************************************
+ * Copyright (c) 2012 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.ExtendedCommandExecutor;
+import org.eclipse.jpt.common.utility.command.StatefulExtendedCommandExecutor;
+
+/**
+ * Calls to {@link #waitToExecute(Command)} will suspend the current thread
+ * until the command executor is {@link #start() started} and any previously-
+ * dispatched commands have executed.
+ * <p>
+ * <strong>NB:</strong> Calls to {@link #waitToExecute(Command)} will suspend
+ * the current thread <em>indefinitely</em> if the command executor is
+ * {@link #stop() stopped}.
+ *
+ * @see AbstractSingleUseQueueingCommandExecutor
+ */
+public class SingleUseQueueingExtendedCommandExecutor
+ extends AbstractSingleUseQueueingCommandExecutor<StatefulExtendedCommandExecutor>
+ implements StatefulExtendedCommandExecutor
+{
+ public SingleUseQueueingExtendedCommandExecutor() {
+ this(ExtendedCommandExecutor.Default.instance());
+ }
+
+ public SingleUseQueueingExtendedCommandExecutor(ExtendedCommandExecutor commandExecutor) {
+ this(new SimpleStatefulExtendedCommandExecutor(commandExecutor));
+ }
+
+ public SingleUseQueueingExtendedCommandExecutor(StatefulExtendedCommandExecutor commandExecutor) {
+ super(commandExecutor);
+ }
+
+ public void waitToExecute(Command command) throws InterruptedException {
+ SynchronizingCommand syncCommand = new SynchronizingCommand();
+
+ this.execute(syncCommand); // put the sync command on the queue and wait until it has executed
+
+ try {
+ syncCommand.waitForExecution();
+ this.commandExecutor.waitToExecute(command);
+ } finally {
+ syncCommand.release();
+ }
+ }
+
+ public boolean waitToExecute(Command command, long timeout) throws InterruptedException {
+ if (timeout == 0L) {
+ this.waitToExecute(command);
+ return true;
+ }
+
+ long stop = System.currentTimeMillis() + timeout;
+ SynchronizingCommand syncCommand = new SynchronizingCommand();
+
+ this.execute(syncCommand); // dispatch the sync command to the other thread
+
+ try {
+ if (syncCommand.waitForExecution(timeout)) {
+ // adjust the time
+ timeout = stop - System.currentTimeMillis();
+ return (timeout > 0) && this.commandExecutor.waitToExecute(command, timeout);
+ }
+ return false;
+ } finally {
+ syncCommand.release();
+ }
+ }
+}

Back to the top