diff options
author | bvosburgh | 2008-12-19 16:30:56 +0000 |
---|---|---|
committer | bvosburgh | 2008-12-19 16:30:56 +0000 |
commit | 6333d08a6739f26e3bafb9a3e906877f17832aef (patch) | |
tree | d26dbf6514fc0dfd672165958dca71d20002eb0c /jpa | |
parent | 1318d978885709acb55d37ea8e18490c6a80d6ac (diff) | |
download | webtools.dali-6333d08a6739f26e3bafb9a3e906877f17832aef.tar.gz webtools.dali-6333d08a6739f26e3bafb9a3e906877f17832aef.tar.xz webtools.dali-6333d08a6739f26e3bafb9a3e906877f17832aef.zip |
added ThreadLocalCommand and other minor clean-up on CommandExecutor
Diffstat (limited to 'jpa')
5 files changed, 132 insertions, 17 deletions
diff --git a/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/GenericJpaProject.java b/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/GenericJpaProject.java index b398f144ca..cf3383d8f0 100644 --- a/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/GenericJpaProject.java +++ b/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/GenericJpaProject.java @@ -831,7 +831,7 @@ public class GenericJpaProject // ********** support for modifying documents shared with the UI ********** public void setThreadLocalModifySharedDocumentCommandExecutor(CommandExecutor commandExecutor) { - this.modifySharedDocumentCommandExecutor.setThreadLocalCommandExecutor(commandExecutor); + this.modifySharedDocumentCommandExecutor.set(commandExecutor); } public CommandExecutor getModifySharedDocumentCommandExecutor() { diff --git a/jpa/plugins/org.eclipse.jpt.utility/src/org/eclipse/jpt/utility/internal/ThreadLocalCommand.java b/jpa/plugins/org.eclipse.jpt.utility/src/org/eclipse/jpt/utility/internal/ThreadLocalCommand.java new file mode 100644 index 0000000000..c12038e502 --- /dev/null +++ b/jpa/plugins/org.eclipse.jpt.utility/src/org/eclipse/jpt/utility/internal/ThreadLocalCommand.java @@ -0,0 +1,63 @@ +/******************************************************************************* + * Copyright (c) 2008 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; + +import org.eclipse.jpt.utility.Command; + +/** + * This implementation of the Command interface allows the client to + * specify a different Command for each thread. + */ +public class ThreadLocalCommand implements Command { + protected final ThreadLocal<Command> threadLocal; + protected final Command defaultCommand; + + /** + * The default command does nothing. + */ + public ThreadLocalCommand() { + this(Command.Null.instance()); + } + + public ThreadLocalCommand(Command defaultCommand) { + super(); + this.defaultCommand = defaultCommand; + this.threadLocal = this.buildThreadLocal(); + } + + protected ThreadLocal<Command> buildThreadLocal() { + return new ThreadLocal<Command>(); + } + + public void execute() { + this.get().execute(); + } + + protected Command get() { + Command command = this.threadLocal.get(); + return (command != null) ? command : this.defaultCommand; + } + + /** + * Set the current thread's command to the specified value. + */ + public void set(Command command) { + this.threadLocal.set(command); + } + + /** + * Return the string representation of the current thread's command. + */ + @Override + public String toString() { + return this.get().toString(); + } + +} diff --git a/jpa/plugins/org.eclipse.jpt.utility/src/org/eclipse/jpt/utility/internal/ThreadLocalCommandExecutor.java b/jpa/plugins/org.eclipse.jpt.utility/src/org/eclipse/jpt/utility/internal/ThreadLocalCommandExecutor.java index 3e459a9fcb..7cb49b4e93 100644 --- a/jpa/plugins/org.eclipse.jpt.utility/src/org/eclipse/jpt/utility/internal/ThreadLocalCommandExecutor.java +++ b/jpa/plugins/org.eclipse.jpt.utility/src/org/eclipse/jpt/utility/internal/ThreadLocalCommandExecutor.java @@ -17,9 +17,12 @@ import org.eclipse.jpt.utility.CommandExecutor; * specify a different Command Executor for each thread. */ public class ThreadLocalCommandExecutor implements CommandExecutor { - protected final ThreadLocal<CommandExecutor> threadLocalCommandExecutor; + protected final ThreadLocal<CommandExecutor> threadLocal; protected final CommandExecutor defaultCommandExecutor; + /** + * The default command executor simply executes the command directly. + */ public ThreadLocalCommandExecutor() { this(CommandExecutor.Default.instance()); } @@ -27,32 +30,36 @@ public class ThreadLocalCommandExecutor implements CommandExecutor { public ThreadLocalCommandExecutor(CommandExecutor defaultCommandExecutor) { super(); this.defaultCommandExecutor = defaultCommandExecutor; - this.threadLocalCommandExecutor = this.buildThreadLocalCommandExecutor(); + this.threadLocal = this.buildThreadLocal(); } - protected ThreadLocal<CommandExecutor> buildThreadLocalCommandExecutor() { + protected ThreadLocal<CommandExecutor> buildThreadLocal() { return new ThreadLocal<CommandExecutor>(); } public void execute(Command command) { - this.getThreadLocalCommandExecutor().execute(command); + this.get().execute(command); } - protected CommandExecutor getThreadLocalCommandExecutor() { - CommandExecutor ce = this.threadLocalCommandExecutor.get(); + protected CommandExecutor get() { + CommandExecutor ce = this.threadLocal.get(); return (ce != null) ? ce : this.defaultCommandExecutor; } /** * Set the current thread's command executor to the specified value. */ - public void setThreadLocalCommandExecutor(CommandExecutor commandExecutor) { - this.threadLocalCommandExecutor.set(commandExecutor); + public void set(CommandExecutor commandExecutor) { + this.threadLocal.set(commandExecutor); } + /** + * Return the string representation of the current thread's command + * executor. + */ @Override public String toString() { - return this.getThreadLocalCommandExecutor().toString(); + return this.get().toString(); } } diff --git a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/CommandExecutorTests.java b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/CommandExecutorTests.java index eeef9623fe..f27f8830ba 100644 --- a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/CommandExecutorTests.java +++ b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/CommandExecutorTests.java @@ -75,7 +75,7 @@ public class CommandExecutorTests extends TestCase { this.executionCount = executionCount; } public void run() { - this.threadLocalCommandExecutor.setThreadLocalCommandExecutor(this.testCommandExecutor); + this.threadLocalCommandExecutor.set(this.testCommandExecutor); for (int i = 0; i < this.executionCount; i++) { this.threadLocalCommandExecutor.execute(this.testCommand); } diff --git a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/CommandTests.java b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/CommandTests.java index 04879f20f1..160c77f3c2 100644 --- a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/CommandTests.java +++ b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/CommandTests.java @@ -12,8 +12,10 @@ package org.eclipse.jpt.utility.tests.internal; import junit.framework.TestCase; import org.eclipse.jpt.utility.Command; +import org.eclipse.jpt.utility.CommandExecutor; import org.eclipse.jpt.utility.internal.CommandRunnable; import org.eclipse.jpt.utility.internal.RunnableCommand; +import org.eclipse.jpt.utility.internal.ThreadLocalCommand; public class CommandTests extends TestCase { @@ -39,14 +41,14 @@ public class CommandTests extends TestCase { } public void testRunnableCommand() { - TestRunnable testRunnable = new TestRunnable(); + SimpleTestRunnable testRunnable = new SimpleTestRunnable(); assertFalse(testRunnable.ran); Command command = new RunnableCommand(testRunnable); command.execute(); assertTrue(testRunnable.ran); } - static class TestRunnable implements Runnable { + static class SimpleTestRunnable implements Runnable { boolean ran = false; public void run() { this.ran = true; @@ -55,16 +57,59 @@ public class CommandTests extends TestCase { public void testCommandRunnable() { TestCommand testCommand = new TestCommand(); - assertFalse(testCommand.executed); + assertEquals(0, testCommand.count); Runnable runnable = new CommandRunnable(testCommand); runnable.run(); - assertTrue(testCommand.executed); + assertEquals(1, testCommand.count); } static class TestCommand implements Command { - boolean executed = false; + int count = 0; public void execute() { - this.executed = true; + this.count++; + } + } + + public void testThreadLocalCommand() throws Exception { + ThreadLocalCommand threadLocalCommand = new ThreadLocalCommand(); + TestRunnable testRunnable1 = new TestRunnable(threadLocalCommand, 1); + Thread thread1 = new Thread(testRunnable1); + thread1.run(); + + TestRunnable testRunnable2 = new TestRunnable(threadLocalCommand, 2); + Thread thread2 = new Thread(testRunnable2); + thread2.run(); + + thread1.join(); + thread2.join(); + + assertEquals(1, testRunnable1.testCommand.count); + + assertEquals(2, testRunnable2.testCommand.count); + } + + static class TestCommandExecutor implements CommandExecutor { + int count = 0; + public void execute(Command command) { + this.count++; + command.execute(); + } + } + + static class TestRunnable implements Runnable { + final ThreadLocalCommand threadLocalCommand; + final int executionCount; + final TestCommand testCommand = new TestCommand(); + TestRunnable(ThreadLocalCommand threadLocalCommand, int executionCount) { + super(); + this.threadLocalCommand = threadLocalCommand; + this.executionCount = executionCount; + } + public void run() { + this.threadLocalCommand.set(this.testCommand); + for (int i = 0; i < this.executionCount; i++) { + this.threadLocalCommand.execute(); + } } } |