diff options
author | Brian Vosburgh | 2013-08-22 16:00:49 +0000 |
---|---|---|
committer | Brian Vosburgh | 2013-08-26 19:14:48 +0000 |
commit | 09c644f5bbbabe38c56edf626611bcf6eefc880f (patch) | |
tree | 7d869ef3e760a86da9044f421ee37cdcb7659303 | |
parent | ff7e4bbf7148817aeb728e3fbc7062891084ee61 (diff) | |
download | webtools.dali-09c644f5bbbabe38c56edf626611bcf6eefc880f.tar.gz webtools.dali-09c644f5bbbabe38c56edf626611bcf6eefc880f.tar.xz webtools.dali-09c644f5bbbabe38c56edf626611bcf6eefc880f.zip |
various implementations and adapters for: Closure, Command, Factory,
Transformer
59 files changed, 2601 insertions, 72 deletions
diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/ClosureTools.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/ClosureTools.java index e56d0e24ec..98bf249409 100644 --- a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/ClosureTools.java +++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/ClosureTools.java @@ -13,6 +13,7 @@ import org.eclipse.jpt.common.utility.Association; import org.eclipse.jpt.common.utility.closure.Closure; import org.eclipse.jpt.common.utility.command.Command; import org.eclipse.jpt.common.utility.exception.ExceptionHandler; +import org.eclipse.jpt.common.utility.factory.Factory; import org.eclipse.jpt.common.utility.internal.ArrayTools; import org.eclipse.jpt.common.utility.internal.ClassTools; import org.eclipse.jpt.common.utility.internal.ObjectTools; @@ -26,6 +27,64 @@ import org.eclipse.jpt.common.utility.transformer.Transformer; */ public final class ClosureTools { + // ********** adapters ********** + + /** + * Adapt the specified {@link Command} to the {@link Closure} interface. + * The closure's argument is ignored. + * @param <A> the type of the object passed to the closure + */ + public static <A> Closure<A> adapt(Command command) { + return new CommandClosure<A>(command); + } + + /** + * Adapt the specified {@link Factory} to the {@link Closure} interface. + * The closure's argument and the factory's output are ignored. This really + * only useful for a factory that has side-effects. + * @param <A> the type of the object passed to the closure + */ + public static <A> Closure<A> adapt(Factory<?> factory) { + return new FactoryClosure<A>(factory); + } + + /** + * Adapt the specified {@link Transformer} to the {@link Closure} interface. + * The transformer's output is ignored. This really only useful for a + * transformer that has side-effects. + * @param <A> the type of the object passed to the closure and forwarded to + * the transformer + */ + public static <A> Closure<A> adapt(Transformer<? super A, ?> transformer) { + return new TransformerClosure<A>(transformer); + } + + + // ********** thread local ********** + + /** + * Return a closure that allows the client to specify a different closure + * for each thread. If there is no closure for the current thread, the + * closure will do nothing. + * @param <A> the type of the object passed to the closure + * @see ThreadLocalClosure + */ + public static <A> ThreadLocalClosure<A> threadLocalClosure() { + return threadLocalClosure(NullClosure.instance()); + } + + /** + * Return a closure that allows the client to specify a different closure + * for each thread. If there is no closure for the current thread, the + * specified default closure is executed. + * @param <A> the type of the object passed to the closure + * @see ThreadLocalClosure + */ + public static <A> ThreadLocalClosure<A> threadLocalClosure(Closure<? super A> defaultClosure) { + return new ThreadLocalClosure<A>(defaultClosure); + } + + // ********** wrappers ********** /** @@ -57,8 +116,9 @@ public final class ClosureTools { * allowing a client to change a previously-supplied closure's * behavior mid-stream. * @param <A> the type of the object passed to the closure + * @see ClosureWrapper */ - public static <A> Closure<A> wrap(Closure<? super A> closure) { + public static <A> ClosureWrapper<A> wrap(Closure<? super A> closure) { return new ClosureWrapper<A>(closure); } @@ -66,7 +126,7 @@ public final class ClosureTools { // ********** safe ********** /** - * Return closure will handle any exceptions thrown by the specified + * Return closure that will handle any exceptions thrown by the specified * closure. If the wrapped closure throws an exception, * the exception's stack trace will be printed to {@link System#err * the "standard" error output stream}. @@ -78,8 +138,8 @@ public final class ClosureTools { } /** - * Return closure will handle any exceptions thrown by the specified - * closure with an {@link ExceptionHandler exception handler}. If the + * Return closure that will handle any exceptions thrown by the specified + * closure with the specified exception handler. If the * wrapped closure throws an exception, the safe closure will handle * the exception and return. * @param <A> the type of the object passed to the closure @@ -153,21 +213,6 @@ public final class ClosureTools { } - // ********** transformer ********** - - /** - * Adapt the specified {@link Transformer} to the {@link Closure} interface. - * The transformer's output is ignored. This really only useful for a - * transformer that has side-effects. - * @param <A> the type of the object passed to the closure and forwarded to the - * transformer - * @see TransformerClosure - */ - public static <A, O> Closure<A> adapt(Transformer<? super A, ? extends O> transformer) { - return new TransformerClosure<A>(transformer); - } - - // ********** conditional ********** /** diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/CommandClosure.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/CommandClosure.java new file mode 100644 index 0000000000..7f95c66ecb --- /dev/null +++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/CommandClosure.java @@ -0,0 +1,47 @@ +/******************************************************************************* + * Copyright (c) 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.closure; + +import org.eclipse.jpt.common.utility.closure.Closure; +import org.eclipse.jpt.common.utility.command.Command; +import org.eclipse.jpt.common.utility.internal.ObjectTools; + +/** + * Adapt a {@link Command} to the {@link Closure} interface. + * The closure's argument is ignored. + * + * @param <A> the type of the object passed to the closure; + * ignored + * + * @see org.eclipse.jpt.common.utility.internal.command.ClosureCommand + */ +public class CommandClosure<A> + implements Closure<A> +{ + private final Command command; + + + public CommandClosure(Command command) { + super(); + if (command == null) { + throw new NullPointerException(); + } + this.command = command; + } + + public void execute(A argument) { + this.command.execute(); + } + + @Override + public String toString() { + return ObjectTools.toString(this, this.command); + } +} diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/FactoryClosure.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/FactoryClosure.java new file mode 100644 index 0000000000..dcec866695 --- /dev/null +++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/FactoryClosure.java @@ -0,0 +1,48 @@ +/******************************************************************************* + * Copyright (c) 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.closure; + +import org.eclipse.jpt.common.utility.closure.Closure; +import org.eclipse.jpt.common.utility.factory.Factory; +import org.eclipse.jpt.common.utility.internal.ObjectTools; + +/** + * Adapt a {@link Factory} to the {@link Closure} interface. + * The closure's argument and the factory's output are ignored. + * This really only useful for a factory that has side-effects. + * + * @param <A> the type of the object passed to the closure; + * ignored + * + * @see org.eclipse.jpt.common.utility.internal.factory.ClosureFactory + */ +public class FactoryClosure<A> + implements Closure<A> +{ + private final Factory<?> factory; + + + public FactoryClosure(Factory<?> factory) { + super(); + if (factory == null) { + throw new NullPointerException(); + } + this.factory = factory; + } + + public void execute(A argument) { + this.factory.create(); + } + + @Override + public String toString() { + return ObjectTools.toString(this, this.factory); + } +} diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/InterruptibleCommandClosure.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/InterruptibleCommandClosure.java new file mode 100644 index 0000000000..04b123591a --- /dev/null +++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/InterruptibleCommandClosure.java @@ -0,0 +1,41 @@ +/******************************************************************************* + * Copyright (c) 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.closure; + +import org.eclipse.jpt.common.utility.closure.InterruptibleClosure; +import org.eclipse.jpt.common.utility.command.InterruptibleCommand; +import org.eclipse.jpt.common.utility.internal.ObjectTools; + +/** + * @see CommandClosure + */ +public class InterruptibleCommandClosure<A> + implements InterruptibleClosure<A> +{ + private final InterruptibleCommand command; + + + public InterruptibleCommandClosure(InterruptibleCommand command) { + super(); + if (command == null) { + throw new NullPointerException(); + } + this.command = command; + } + + public void execute(A argument) throws InterruptedException { + this.command.execute(); + } + + @Override + public String toString() { + return ObjectTools.toString(this, this.command); + } +} diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/InterruptibleFactoryClosure.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/InterruptibleFactoryClosure.java new file mode 100644 index 0000000000..deaa3c01c9 --- /dev/null +++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/InterruptibleFactoryClosure.java @@ -0,0 +1,41 @@ +/******************************************************************************* + * Copyright (c) 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.closure; + +import org.eclipse.jpt.common.utility.closure.InterruptibleClosure; +import org.eclipse.jpt.common.utility.factory.InterruptibleFactory; +import org.eclipse.jpt.common.utility.internal.ObjectTools; + +/** + * @see FactoryClosure + */ +public class InterruptibleFactoryClosure<A> + implements InterruptibleClosure<A> +{ + private final InterruptibleFactory<?> factory; + + + public InterruptibleFactoryClosure(InterruptibleFactory<?> factory) { + super(); + if (factory == null) { + throw new NullPointerException(); + } + this.factory = factory; + } + + public void execute(A argument) throws InterruptedException { + this.factory.create(); + } + + @Override + public String toString() { + return ObjectTools.toString(this, this.factory); + } +} diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/InterruptibleTransformerClosure.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/InterruptibleTransformerClosure.java index 6d3f3ddae8..33f1df4b08 100644 --- a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/InterruptibleTransformerClosure.java +++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/InterruptibleTransformerClosure.java @@ -15,7 +15,6 @@ import org.eclipse.jpt.common.utility.transformer.InterruptibleTransformer; /** * @see TransformerClosure - * @see org.eclipse.jpt.common.utility.internal.transformer.InterruptibleClosureTransformer */ public class InterruptibleTransformerClosure<A> implements InterruptibleClosure<A> diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/ThreadLocalClosure.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/ThreadLocalClosure.java new file mode 100644 index 0000000000..70ef9eb6cf --- /dev/null +++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/ThreadLocalClosure.java @@ -0,0 +1,68 @@ +/******************************************************************************* + * Copyright (c) 2008, 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.closure; + +import org.eclipse.jpt.common.utility.closure.Closure; + +/** + * This closure allows the client to specify a different closure for each + * thread. If there is no closure for the current thread, the configured default + * closure is executed. + * + * @param <A> the type of the object passed to the closure + * + * @see #set(Closure) + */ +public class ThreadLocalClosure<A> + implements Closure<A> +{ + private final ThreadLocal<Closure<? super A>> threadLocal; + private final Closure<? super A> defaultClosure; + + public ThreadLocalClosure(Closure<? super A> defaultClosure) { + super(); + if (defaultClosure == null) { + throw new NullPointerException(); + } + this.defaultClosure = defaultClosure; + this.threadLocal = this.buildThreadLocal(); + } + + private ThreadLocal<Closure<? super A>> buildThreadLocal() { + return new ThreadLocal<Closure<? super A>>(); + } + + public void execute(A argument) { + this.get().execute(argument); + } + + private Closure<? super A> get() { + Closure<? super A> closure = this.threadLocal.get(); + if (closure != null) { + return closure; + } + return this.defaultClosure; + } + + /** + * Set the current thread's closure to the specified value. + */ + public void set(Closure<? super A> closure) { + this.threadLocal.set(closure); + } + + /** + * Return the string representation of the current thread's closure. + */ + @Override + public String toString() { + return this.get().toString(); + } +} diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/ThreadLocalInterruptibleClosure.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/ThreadLocalInterruptibleClosure.java new file mode 100644 index 0000000000..dce4099f2b --- /dev/null +++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/ThreadLocalInterruptibleClosure.java @@ -0,0 +1,62 @@ +/******************************************************************************* + * Copyright (c) 2008, 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.closure; + +import org.eclipse.jpt.common.utility.closure.InterruptibleClosure; + +/** + * @see ThreadLocalClosure + */ +public class ThreadLocalInterruptibleClosure<A> + implements InterruptibleClosure<A> +{ + private final ThreadLocal<InterruptibleClosure<? super A>> threadLocal; + private final InterruptibleClosure<? super A> defaultInterruptibleClosure; + + public ThreadLocalInterruptibleClosure(InterruptibleClosure<? super A> defaultInterruptibleClosure) { + super(); + if (defaultInterruptibleClosure == null) { + throw new NullPointerException(); + } + this.defaultInterruptibleClosure = defaultInterruptibleClosure; + this.threadLocal = this.buildThreadLocal(); + } + + private ThreadLocal<InterruptibleClosure<? super A>> buildThreadLocal() { + return new ThreadLocal<InterruptibleClosure<? super A>>(); + } + + public void execute(A argument) throws InterruptedException { + this.get().execute(argument); + } + + private InterruptibleClosure<? super A> get() { + InterruptibleClosure<? super A> closure = this.threadLocal.get(); + if (closure != null) { + return closure; + } + return this.defaultInterruptibleClosure; + } + + /** + * Set the current thread's closure to the specified value. + */ + public void set(InterruptibleClosure<? super A> closure) { + this.threadLocal.set(closure); + } + + /** + * Return the string representation of the current thread's closure. + */ + @Override + public String toString() { + return this.get().toString(); + } +} diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/TransformerClosure.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/TransformerClosure.java index c336a972d9..a6ec35bd45 100644 --- a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/TransformerClosure.java +++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/TransformerClosure.java @@ -15,7 +15,8 @@ import org.eclipse.jpt.common.utility.transformer.Transformer; /** * Adapt a {@link Transformer} to the {@link Closure} interface. - * The transformer's output is ignored. This really only useful for a + * The closure's argument is forwarded to the the transformer and the + * transformer's output is ignored. This really only useful for a * transformer that has side-effects. * * @param <A> the type of the object passed to the closure and forwarded to the diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/command/ClosureCommand.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/command/ClosureCommand.java new file mode 100644 index 0000000000..71b8bf25f0 --- /dev/null +++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/command/ClosureCommand.java @@ -0,0 +1,46 @@ +/******************************************************************************* + * Copyright (c) 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.closure.Closure; +import org.eclipse.jpt.common.utility.command.Command; +import org.eclipse.jpt.common.utility.internal.ObjectTools; + +/** + * Adapt a {@link Closure} to the {@link Command} interface. + * The closure is passed <code>null</code> for an argument. + * This really only useful for a closure that accepts a + * <code>null</code> argument. + * + * @see org.eclipse.jpt.common.utility.internal.closure.CommandClosure + */ +public class ClosureCommand + implements Command +{ + private final Closure<?> closure; + + + public ClosureCommand(Closure<?> closure) { + super(); + if (closure == null) { + throw new NullPointerException(); + } + this.closure = closure; + } + + public void execute() { + this.closure.execute(null); + } + + @Override + public String toString() { + return ObjectTools.toString(this, this.closure); + } +} diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/command/CommandTools.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/command/CommandTools.java new file mode 100644 index 0000000000..643491a899 --- /dev/null +++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/command/CommandTools.java @@ -0,0 +1,231 @@ +/******************************************************************************* + * Copyright (c) 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 java.lang.reflect.Method; +import org.eclipse.jpt.common.utility.closure.Closure; +import org.eclipse.jpt.common.utility.command.Command; +import org.eclipse.jpt.common.utility.exception.ExceptionHandler; +import org.eclipse.jpt.common.utility.factory.Factory; +import org.eclipse.jpt.common.utility.internal.ArrayTools; +import org.eclipse.jpt.common.utility.internal.ClassTools; +import org.eclipse.jpt.common.utility.internal.ObjectTools; +import org.eclipse.jpt.common.utility.internal.exception.DefaultExceptionHandler; +import org.eclipse.jpt.common.utility.transformer.Transformer; + +/** + * {@link Command} utility methods. + */ +public final class CommandTools { + + // ********** adapters ********** + + /** + * Adapt a {@link Runnable} to the {@link Command} interface. + */ + public static Command adapt(Runnable runnable) { + return new RunnableCommand(runnable); + } + + /** + * Adapt a {@link Command} to the {@link Runnable} interface. + */ + public static Runnable runnable(Command command) { + return new CommandRunnable(command); + } + + /** + * Adapt a {@link Closure} to the {@link Command} interface. + * The closure is passed <code>null</code> for input. + * This really only useful for a transformer that accepts + * <code>null</code> input. + */ + public static Command adapt(Closure<?> closure) { + return new ClosureCommand(closure); + } + + /** + * Adapt a {@link Factory} to the {@link Command} interface. + * The factory's output is ignored. This really only useful + * for a factory that has side-effects. + */ + public static Command adapt(Factory<?> factory) { + return new FactoryCommand(factory); + } + + /** + * Adapt a {@link Transformer} to the {@link Command} interface. + * The transformer is passed <code>null</code> for input and its output is + * ignored. This really only useful for a transformer that accepts + * <code>null</code> input and has side-effects. + */ + public static Command adapt(Transformer<?, ?> transformer) { + return new TransformerCommand(transformer); + } + + + // ********** composite ********** + + /** + * @see #composite(Iterable) + */ + public static Command composite(Command... commands) { + return composite(ArrayTools.iterable(commands)); + } + + /** + * Return a composite of the specified commands. The commands will be + * executed in sequence. + */ + public static Command composite(Iterable<Command> commands) { + return new CompositeCommand(commands); + } + + + // ********** thread local ********** + + /** + * Return a command that allows the client to specify a different command + * for each thread. If there is no command for the current thread, the + * command will do nothing. + * @see ThreadLocalCommand + */ + public static ThreadLocalCommand threadLocalCommand() { + return threadLocalCommand(NullCommand.instance()); + } + + /** + * Return a command that allows the client to specify a different command + * for each thread. If there is no command for the current thread, the + * specified default command is executed. + * @see ThreadLocalCommand + */ + public static ThreadLocalCommand threadLocalCommand(Command defaultCommand) { + return new ThreadLocalCommand(defaultCommand); + } + + + // ********** wrappers ********** + + /** + * Return a command that can have its wrapped command changed, + * allowing a client to change a previously-supplied command's + * behavior mid-stream. + * @see CommandWrapper + */ + public static CommandWrapper wrap(Command command) { + return new CommandWrapper(command); + } + + + // ********** safe ********** + + /** + * Return command that will handle any exceptions thrown by the specified + * command. If the wrapped command throws an exception, + * the exception's stack trace will be printed to {@link System#err + * the "standard" error output stream}. + * @see #safe(Command, ExceptionHandler) + */ + public static Command safe(Command command) { + return safe(command, DefaultExceptionHandler.instance()); + } + + /** + * Return command that will handle any exceptions thrown by the specified + * command with the specified exception handler. + * @see #safe(Command) + */ + public static Command safe(Command command, ExceptionHandler exceptionHandler) { + return new SafeCommandWrapper(command, exceptionHandler); + } + + + // ********** looping ********** + + /** + * Return a command that executes the specified command the specified + * number of times. + */ + public static Command repeat(Command command, int count) { + return new RepeatingCommand(command, count); + } + + + // ********** disabled ********** + + /** + * Return a command that will throw an + * {@link UnsupportedOperationException exception} when executed. + */ + public static Command disabledCommand() { + return DisabledCommand.instance(); + } + + + // ********** null ********** + + /** + * Return a command that does nothing. + */ + public static Command nullCommand() { + return NullCommand.instance(); + } + + + // ********** reflection ********** + + /** + * Return a command that uses Java reflection to execute + * the specified zero-argument static method. + * @see StaticMethodCommand + */ + public static Command execute(Class<?> clazz, String methodName) { + return execute(clazz, methodName, ClassTools.EMPTY_ARRAY, ObjectTools.EMPTY_OBJECT_ARRAY); + } + + /** + * Return a command that uses Java reflection to execute + * the specified single-argument static method. + * @see StaticMethodCommand + */ + public static Command execute(Class<?> clazz, String methodName, Class<?> parameterType, Object argument) { + return execute(clazz, methodName, new Class<?>[] { parameterType }, new Object[] { argument }); + } + + /** + * Return a command that uses Java reflection to execute + * the specified static method. + * @see StaticMethodCommand + */ + public static Command execute(Class<?> clazz, String methodName, Class<?>[] parameterTypes, Object[] arguments) { + return execute(ClassTools.method(clazz, methodName, parameterTypes), arguments); + } + + /** + * Return a command that uses Java reflection to execute + * the specified static method. + * @see StaticMethodCommand + */ + public static Command execute(Method method, Object[] arguments) { + return new StaticMethodCommand(method, arguments); + } + + + // ********** constructor ********** + + /** + * Suppress default constructor, ensuring non-instantiability. + */ + private CommandTools() { + super(); + throw new UnsupportedOperationException(); + } +} diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/command/CommandWrapper.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/command/CommandWrapper.java index 09fd2762a7..f77b2265b3 100644 --- a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/command/CommandWrapper.java +++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/command/CommandWrapper.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2012 Oracle. All rights reserved. + * 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. diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/command/CompositeCommand.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/command/CompositeCommand.java index 6d49887a29..ad6d523bdd 100644 --- a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/command/CompositeCommand.java +++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/command/CompositeCommand.java @@ -14,21 +14,17 @@ import org.eclipse.jpt.common.utility.internal.ObjectTools; import org.eclipse.jpt.common.utility.internal.iterable.IterableTools; /** - * <code>CompositeCommand</code> provides support for treating a collection of - * {@link Command}s as a single command. + * Command that provides support for treating a collection of + * commands as a single command. */ public class CompositeCommand implements Command { private final Iterable<Command> commands; - public CompositeCommand(Command... commands) { - this(IterableTools.iterable(commands)); - } - public CompositeCommand(Iterable<Command> commands) { super(); - if (commands == null) { + if (IterableTools.isOrContainsNull(commands)) { throw new NullPointerException(); } this.commands = commands; diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/command/CompositeInterruptibleCommand.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/command/CompositeInterruptibleCommand.java new file mode 100644 index 0000000000..03781c783a --- /dev/null +++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/command/CompositeInterruptibleCommand.java @@ -0,0 +1,42 @@ +/******************************************************************************* + * Copyright (c) 2009, 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.InterruptibleCommand; +import org.eclipse.jpt.common.utility.internal.ObjectTools; +import org.eclipse.jpt.common.utility.internal.iterable.IterableTools; + +/** + * @see CompositeCommand + */ +public class CompositeInterruptibleCommand + implements InterruptibleCommand +{ + private final Iterable<InterruptibleCommand> commands; + + public CompositeInterruptibleCommand(Iterable<InterruptibleCommand> commands) { + super(); + if (IterableTools.isOrContainsNull(commands)) { + throw new NullPointerException(); + } + this.commands = commands; + } + + public void execute() throws InterruptedException { + for (InterruptibleCommand command : this.commands) { + command.execute(); + } + } + + @Override + public String toString() { + return ObjectTools.toString(this, this.commands); + } +} diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/command/FactoryCommand.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/command/FactoryCommand.java new file mode 100644 index 0000000000..5f3e407295 --- /dev/null +++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/command/FactoryCommand.java @@ -0,0 +1,45 @@ +/******************************************************************************* + * Copyright (c) 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.factory.Factory; +import org.eclipse.jpt.common.utility.internal.ObjectTools; + +/** + * Adapt a {@link Factory} to the {@link Command} interface. + * The factory's output is ignored. + * This really only useful for a factory that has side-effects. + * + * @see org.eclipse.jpt.common.utility.internal.factory.CommandFactory + */ +public class FactoryCommand + implements Command +{ + private final Factory<?> factory; + + + public FactoryCommand(Factory<?> factory) { + super(); + if (factory == null) { + throw new NullPointerException(); + } + this.factory = factory; + } + + public void execute() { + this.factory.create(); + } + + @Override + public String toString() { + return ObjectTools.toString(this, this.factory); + } +} diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/command/InterruptibleClosureCommand.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/command/InterruptibleClosureCommand.java new file mode 100644 index 0000000000..fd9bd10007 --- /dev/null +++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/command/InterruptibleClosureCommand.java @@ -0,0 +1,41 @@ +/******************************************************************************* + * Copyright (c) 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.closure.InterruptibleClosure; +import org.eclipse.jpt.common.utility.command.InterruptibleCommand; +import org.eclipse.jpt.common.utility.internal.ObjectTools; + +/** + * @see ClosureCommand + */ +public class InterruptibleClosureCommand + implements InterruptibleCommand +{ + private final InterruptibleClosure<?> closure; + + + public InterruptibleClosureCommand(InterruptibleClosure<?> closure) { + super(); + if (closure == null) { + throw new NullPointerException(); + } + this.closure = closure; + } + + public void execute() throws InterruptedException { + this.closure.execute(null); + } + + @Override + public String toString() { + return ObjectTools.toString(this, this.closure); + } +} diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/command/InterruptibleFactoryCommand.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/command/InterruptibleFactoryCommand.java new file mode 100644 index 0000000000..acc4ac26d0 --- /dev/null +++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/command/InterruptibleFactoryCommand.java @@ -0,0 +1,41 @@ +/******************************************************************************* + * Copyright (c) 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.InterruptibleCommand; +import org.eclipse.jpt.common.utility.factory.InterruptibleFactory; +import org.eclipse.jpt.common.utility.internal.ObjectTools; + +/** + * @see FactoryCommand + */ +public class InterruptibleFactoryCommand + implements InterruptibleCommand +{ + private final InterruptibleFactory<?> factory; + + + public InterruptibleFactoryCommand(InterruptibleFactory<?> factory) { + super(); + if (factory == null) { + throw new NullPointerException(); + } + this.factory = factory; + } + + public void execute() throws InterruptedException { + this.factory.create(); + } + + @Override + public String toString() { + return ObjectTools.toString(this, this.factory); + } +} diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/command/InterruptibleTransformerCommand.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/command/InterruptibleTransformerCommand.java new file mode 100644 index 0000000000..e610da318e --- /dev/null +++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/command/InterruptibleTransformerCommand.java @@ -0,0 +1,41 @@ +/******************************************************************************* + * Copyright (c) 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.InterruptibleCommand; +import org.eclipse.jpt.common.utility.internal.ObjectTools; +import org.eclipse.jpt.common.utility.transformer.InterruptibleTransformer; + +/** + * @see TransformerCommand + */ +public class InterruptibleTransformerCommand + implements InterruptibleCommand +{ + private final InterruptibleTransformer<?, ?> transformer; + + + public InterruptibleTransformerCommand(InterruptibleTransformer<?, ?> transformer) { + super(); + if (transformer == null) { + throw new NullPointerException(); + } + this.transformer = transformer; + } + + public void execute() throws InterruptedException { + this.transformer.transform(null); + } + + @Override + public String toString() { + return ObjectTools.toString(this, this.transformer); + } +} diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/command/RepeatingCommand.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/command/RepeatingCommand.java new file mode 100644 index 0000000000..6b5d978f36 --- /dev/null +++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/command/RepeatingCommand.java @@ -0,0 +1,46 @@ +/******************************************************************************* + * Copyright (c) 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.internal.ObjectTools; + +/** + * Command that executes another command a specified number of times. + */ +public class RepeatingCommand + implements Command +{ + private final int count; + private final Command command; + + public RepeatingCommand(Command command, int count) { + super(); + if (command == null) { + throw new NullPointerException(); + } + if (count <= 0) { + throw new IndexOutOfBoundsException("invalid count: " + count); //$NON-NLS-1$ + } + this.command = command; + this.count = count; + } + + public void execute() { + for (int i = this.count; i-- > 0;) { + this.command.execute(); + } + } + + @Override + public String toString() { + return ObjectTools.toString(this, this.command); + } +} diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/command/RepeatingInterruptibleCommand.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/command/RepeatingInterruptibleCommand.java new file mode 100644 index 0000000000..085a1d5134 --- /dev/null +++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/command/RepeatingInterruptibleCommand.java @@ -0,0 +1,46 @@ +/******************************************************************************* + * Copyright (c) 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.InterruptibleCommand; +import org.eclipse.jpt.common.utility.internal.ObjectTools; + +/** + * @see RepeatingCommand + */ +public class RepeatingInterruptibleCommand + implements InterruptibleCommand +{ + private final int count; + private final InterruptibleCommand command; + + public RepeatingInterruptibleCommand(InterruptibleCommand command, int count) { + super(); + if (command == null) { + throw new NullPointerException(); + } + if (count <= 0) { + throw new IndexOutOfBoundsException("invalid count: " + count); //$NON-NLS-1$ + } + this.command = command; + this.count = count; + } + + public void execute() throws InterruptedException { + for (int i = this.count; i-- > 0;) { + this.command.execute(); + } + } + + @Override + public String toString() { + return ObjectTools.toString(this, this.command); + } +} diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/command/SafeCommandWrapper.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/command/SafeCommandWrapper.java new file mode 100644 index 0000000000..de0555bf3f --- /dev/null +++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/command/SafeCommandWrapper.java @@ -0,0 +1,48 @@ +/******************************************************************************* + * Copyright (c) 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.exception.ExceptionHandler; +import org.eclipse.jpt.common.utility.internal.ObjectTools; +import org.eclipse.jpt.common.utility.command.Command; + +/** + * Command wrapper that will handle any exceptions thrown by the wrapped + * command with an {@link ExceptionHandler exception handler}. + */ +public class SafeCommandWrapper + implements Command +{ + private final Command command; + private final ExceptionHandler exceptionHandler; + + + public SafeCommandWrapper(Command command, ExceptionHandler exceptionHandler) { + super(); + if ((command == null) || (exceptionHandler == null)) { + throw new NullPointerException(); + } + this.command = command; + this.exceptionHandler = exceptionHandler; + } + + public void execute() { + try { + this.command.execute(); + } catch (Throwable ex) { + this.exceptionHandler.handleException(ex); + } + } + + @Override + public String toString() { + return ObjectTools.toString(this, this.command); + } +} diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/command/SafeInterruptibleCommandWrapper.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/command/SafeInterruptibleCommandWrapper.java new file mode 100644 index 0000000000..2d69f0530b --- /dev/null +++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/command/SafeInterruptibleCommandWrapper.java @@ -0,0 +1,50 @@ +/******************************************************************************* + * Copyright (c) 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.exception.ExceptionHandler; +import org.eclipse.jpt.common.utility.internal.ObjectTools; +import org.eclipse.jpt.common.utility.command.InterruptibleCommand; + +/** + * Interruptible command wrapper that will handle any exceptions thrown by the wrapped + * command with an {@link ExceptionHandler exception handler}. + */ +public class SafeInterruptibleCommandWrapper + implements InterruptibleCommand +{ + private final InterruptibleCommand command; + private final ExceptionHandler exceptionHandler; + + + public SafeInterruptibleCommandWrapper(InterruptibleCommand command, ExceptionHandler exceptionHandler) { + super(); + if ((command == null) || (exceptionHandler == null)) { + throw new NullPointerException(); + } + this.command = command; + this.exceptionHandler = exceptionHandler; + } + + public void execute() throws InterruptedException { + try { + this.command.execute(); + } catch (InterruptedException ex) { + throw ex; + } catch (Throwable ex) { + this.exceptionHandler.handleException(ex); + } + } + + @Override + public String toString() { + return ObjectTools.toString(this, this.command); + } +} diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/command/StaticMethodCommand.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/command/StaticMethodCommand.java new file mode 100644 index 0000000000..095ba87be4 --- /dev/null +++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/command/StaticMethodCommand.java @@ -0,0 +1,62 @@ +/******************************************************************************* + * Copyright (c) 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 java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import org.eclipse.jpt.common.utility.command.Command; +import org.eclipse.jpt.common.utility.internal.ObjectTools; + +/** + * A command that uses Java reflection to execute a static method. + * Checked exceptions are converted to {@link RuntimeException}s. + * + * @see java.lang.reflect.Method#invoke(Object, Object...) + */ +public class StaticMethodCommand + implements Command +{ + private final Method method; + private final Object[] arguments; + + + public StaticMethodCommand(Method method, Object[] arguments) { + super(); + if ((method == null) || (arguments == null)) { + throw new NullPointerException(); + } + if ( ! Modifier.isStatic(method.getModifiers())) { + throw new IllegalArgumentException("method must be static: " + method); //$NON-NLS-1$ + } + if ( ! method.isAccessible()) { + throw new IllegalArgumentException("method must be accessible: " + method); //$NON-NLS-1$ + } + this.method = method; + this.arguments = arguments; + } + + public void execute() { + try { + this.method.invoke(null, this.arguments); + } catch (IllegalArgumentException ex) { + throw new RuntimeException(ex); + } catch (IllegalAccessException ex) { + throw new RuntimeException(ex); + } catch (InvocationTargetException ex) { + throw new RuntimeException(ex); + } + } + + @Override + public String toString() { + return ObjectTools.toString(this, this.method); + } +} diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/command/ThreadLocalCommand.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/command/ThreadLocalCommand.java index 10953b7f86..99dda06e64 100644 --- a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/command/ThreadLocalCommand.java +++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/command/ThreadLocalCommand.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2008, 2012 Oracle. All rights reserved. + * Copyright (c) 2008, 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. @@ -12,21 +12,16 @@ package org.eclipse.jpt.common.utility.internal.command; import org.eclipse.jpt.common.utility.command.Command; /** - * This implementation of the Command interface allows the client to - * specify a different Command for each thread. + * This command allows the client to specify a different command for each + * thread. If there is no command for the current thread, the configured default + * command is executed. + * @see #set(Command) */ public class ThreadLocalCommand implements Command { - protected final ThreadLocal<Command> threadLocal; - protected final Command defaultCommand; - - /** - * The default command does nothing. - */ - public ThreadLocalCommand() { - this(NullCommand.instance()); - } + private final ThreadLocal<Command> threadLocal; + private final Command defaultCommand; public ThreadLocalCommand(Command defaultCommand) { super(); @@ -37,7 +32,7 @@ public class ThreadLocalCommand this.threadLocal = this.buildThreadLocal(); } - protected ThreadLocal<Command> buildThreadLocal() { + private ThreadLocal<Command> buildThreadLocal() { return new ThreadLocal<Command>(); } @@ -45,7 +40,7 @@ public class ThreadLocalCommand this.get().execute(); } - protected Command get() { + private Command get() { Command command = this.threadLocal.get(); return (command != null) ? command : this.defaultCommand; } diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/command/ThreadLocalInterruptibleCommand.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/command/ThreadLocalInterruptibleCommand.java new file mode 100644 index 0000000000..b39218767a --- /dev/null +++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/command/ThreadLocalInterruptibleCommand.java @@ -0,0 +1,60 @@ +/******************************************************************************* + * Copyright (c) 2008, 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.InterruptibleCommand; + +/** + * @see ThreadLocalCommand + * @see #set(InterruptibleCommand) + */ +public class ThreadLocalInterruptibleCommand + implements InterruptibleCommand +{ + private final ThreadLocal<InterruptibleCommand> threadLocal; + private final InterruptibleCommand defaultCommand; + + public ThreadLocalInterruptibleCommand(InterruptibleCommand defaultCommand) { + super(); + if (defaultCommand == null) { + throw new NullPointerException(); + } + this.defaultCommand = defaultCommand; + this.threadLocal = this.buildThreadLocal(); + } + + private ThreadLocal<InterruptibleCommand> buildThreadLocal() { + return new ThreadLocal<InterruptibleCommand>(); + } + + public void execute() throws InterruptedException { + this.get().execute(); + } + + private InterruptibleCommand get() { + InterruptibleCommand command = this.threadLocal.get(); + return (command != null) ? command : this.defaultCommand; + } + + /** + * Set the current thread's command to the specified value. + */ + public void set(InterruptibleCommand 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/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/command/TransformerCommand.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/command/TransformerCommand.java new file mode 100644 index 0000000000..2cc40d305a --- /dev/null +++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/command/TransformerCommand.java @@ -0,0 +1,46 @@ +/******************************************************************************* + * Copyright (c) 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.internal.ObjectTools; +import org.eclipse.jpt.common.utility.transformer.Transformer; + +/** + * Adapt a {@link Transformer} to the {@link Command} interface. + * The transformer is passed <code>null</code> for input and its output is + * ignored. This really only useful for a transformer that accepts + * <code>null</code> input and has side-effects. + * + * @see org.eclipse.jpt.common.utility.internal.transformer.CommandTransformer + */ +public class TransformerCommand + implements Command +{ + private final Transformer<?, ?> transformer; + + + public TransformerCommand(Transformer<?, ?> transformer) { + super(); + if (transformer == null) { + throw new NullPointerException(); + } + this.transformer = transformer; + } + + public void execute() { + this.transformer.transform(null); + } + + @Override + public String toString() { + return ObjectTools.toString(this, this.transformer); + } +} diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/factory/ClosureFactory.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/factory/ClosureFactory.java new file mode 100644 index 0000000000..d4491e222e --- /dev/null +++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/factory/ClosureFactory.java @@ -0,0 +1,50 @@ +/******************************************************************************* + * Copyright (c) 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.factory; + +import org.eclipse.jpt.common.utility.closure.Closure; +import org.eclipse.jpt.common.utility.factory.Factory; +import org.eclipse.jpt.common.utility.internal.ObjectTools; + +/** + * Adapt a {@link Closure} to the {@link Factory} interface. + * The closure is passed <code>null</code> for an argument and the factory + * always returns <code>null</code>. + * This really only useful for a closure that accepts a + * <code>null</code> argument. + * + * @param <T> the type of the object returned by the factory + * + * @see org.eclipse.jpt.common.utility.internal.closure.FactoryClosure + */ +public class ClosureFactory<T> + implements Factory<T> +{ + private final Closure<?> closure; + + + public ClosureFactory(Closure<?> closure) { + super(); + if (closure == null) { + throw new NullPointerException(); + } + this.closure = closure; + } + + public T create() { + this.closure.execute(null); + return null; + } + + @Override + public String toString() { + return ObjectTools.toString(this, this.closure); + } +} diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/factory/CommandFactory.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/factory/CommandFactory.java new file mode 100644 index 0000000000..d65ea591f1 --- /dev/null +++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/factory/CommandFactory.java @@ -0,0 +1,47 @@ +/******************************************************************************* + * Copyright (c) 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.factory; + +import org.eclipse.jpt.common.utility.command.Command; +import org.eclipse.jpt.common.utility.factory.Factory; +import org.eclipse.jpt.common.utility.internal.ObjectTools; + +/** + * Adapt a {@link Command} to the {@link Factory} interface. + * The factory always returns <code>null</code>. + * + * @param <T> the type of the object returned by the factory + * + * @see org.eclipse.jpt.common.utility.internal.command.FactoryCommand + */ +public class CommandFactory<T> + implements Factory<T> +{ + private final Command command; + + + public CommandFactory(Command command) { + super(); + if (command == null) { + throw new NullPointerException(); + } + this.command = command; + } + + public T create() { + this.command.execute(); + return null; + } + + @Override + public String toString() { + return ObjectTools.toString(this, this.command); + } +} diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/factory/FactoryTools.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/factory/FactoryTools.java index e1b566a627..df656e64ea 100644 --- a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/factory/FactoryTools.java +++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/factory/FactoryTools.java @@ -12,11 +12,14 @@ package org.eclipse.jpt.common.utility.internal.factory; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.Iterator; +import org.eclipse.jpt.common.utility.closure.Closure; +import org.eclipse.jpt.common.utility.command.Command; import org.eclipse.jpt.common.utility.exception.ExceptionHandler; import org.eclipse.jpt.common.utility.factory.Factory; import org.eclipse.jpt.common.utility.internal.ClassTools; import org.eclipse.jpt.common.utility.internal.ObjectTools; import org.eclipse.jpt.common.utility.internal.exception.DefaultExceptionHandler; +import org.eclipse.jpt.common.utility.transformer.Transformer; /** * {@link Factory} utility methods. @@ -49,6 +52,60 @@ public class FactoryTools { return new IteratorFactory<T>(iterator); } + /** + * Adapt the specified {@link Closure} to the {@link Factory} interface. + * The closure will always passed a <code>null</code> argument. + * The factory will always return <code>null</code>. + * @param <T> the type of the object returned by the factory + */ + public static <T> Factory<T> adapt(Closure<?> closure) { + return new ClosureFactory<T>(closure); + } + + /** + * Adapt the specified {@link Command} to the {@link Factory} interface. + * The factory will always return <code>null</code>. + * @param <T> the type of the object returned by the factory + */ + public static <T> Factory<T> adapt(Command command) { + return new CommandFactory<T>(command); + } + + /** + * Adapt the specified {@link Transformer} to the {@link Factory} interface. + * The transformer will always passed a <code>null</code> input and its + * output will be returned as the factory's output. + * @param <T> the type of the object returned by the factory + */ + public static <T> Factory<T> adapt(Transformer<?, ? extends T> transformer) { + return new TransformerFactory<T>(transformer); + } + + + // ********** thread local ********** + + /** + * Return a factory that allows the client to specify a different factory + * for each thread. If there is no factory for the current thread, the + * factory will return <code>null</code>. + * @param <T> the type of the object returned by the factory + * @see ThreadLocalFactory + */ + public static <T> ThreadLocalFactory<T> threadLocalFactory() { + return threadLocalFactory(NullFactory.<T>instance()); + } + + /** + * Return a factory that allows the client to specify a different factory + * for each thread. If there is no factory for the current thread, the + * specified default factory is used. + * @param <T> the type of the object returned by the factory + * @see ThreadLocalFactory + */ + public static <T> ThreadLocalFactory<T> threadLocalFactory(Factory<? extends T> defaultFactory) { + return new ThreadLocalFactory<T>(defaultFactory); + } + // ********** wrappers ********** diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/factory/InterruptibleClosureFactory.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/factory/InterruptibleClosureFactory.java new file mode 100644 index 0000000000..89135029d5 --- /dev/null +++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/factory/InterruptibleClosureFactory.java @@ -0,0 +1,41 @@ +/******************************************************************************* + * Copyright (c) 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.factory; + +import org.eclipse.jpt.common.utility.closure.InterruptibleClosure; +import org.eclipse.jpt.common.utility.factory.InterruptibleFactory; +import org.eclipse.jpt.common.utility.internal.ObjectTools; + +/** + * @see ClosureFactory + */ +public class InterruptibleClosureFactory<T> + implements InterruptibleFactory<T> +{ + private final InterruptibleClosure<?> closure; + + public InterruptibleClosureFactory(InterruptibleClosure<?> closure) { + super(); + if (closure == null) { + throw new NullPointerException(); + } + this.closure = closure; + } + + public T create() throws InterruptedException { + this.closure.execute(null); + return null; + } + + @Override + public String toString() { + return ObjectTools.toString(this, this.closure); + } +} diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/factory/InterruptibleCommandFactory.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/factory/InterruptibleCommandFactory.java new file mode 100644 index 0000000000..4e147b5826 --- /dev/null +++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/factory/InterruptibleCommandFactory.java @@ -0,0 +1,41 @@ +/******************************************************************************* + * Copyright (c) 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.factory; + +import org.eclipse.jpt.common.utility.command.InterruptibleCommand; +import org.eclipse.jpt.common.utility.factory.InterruptibleFactory; +import org.eclipse.jpt.common.utility.internal.ObjectTools; + +/** + * @see CommandFactory + */ +public class InterruptibleCommandFactory<T> + implements InterruptibleFactory<T> +{ + private final InterruptibleCommand command; + + public InterruptibleCommandFactory(InterruptibleCommand command) { + super(); + if (command == null) { + throw new NullPointerException(); + } + this.command = command; + } + + public T create() throws InterruptedException { + this.command.execute(); + return null; + } + + @Override + public String toString() { + return ObjectTools.toString(this, this.command); + } +} diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/factory/InterruptibleFactoryAdapter.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/factory/InterruptibleFactoryAdapter.java new file mode 100644 index 0000000000..d62881a0c8 --- /dev/null +++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/factory/InterruptibleFactoryAdapter.java @@ -0,0 +1,29 @@ +/******************************************************************************* + * Copyright (c) 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.factory; + +import org.eclipse.jpt.common.utility.factory.InterruptibleFactory; +import org.eclipse.jpt.common.utility.internal.ObjectTools; + +/** + * @see FactoryAdapter + */ +public class InterruptibleFactoryAdapter<T> + implements InterruptibleFactory<T> +{ + public T create() throws InterruptedException { + return null; + } + + @Override + public String toString() { + return ObjectTools.toString(this); + } +} diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/factory/InterruptibleTransformerFactory.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/factory/InterruptibleTransformerFactory.java new file mode 100644 index 0000000000..2c7c753c32 --- /dev/null +++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/factory/InterruptibleTransformerFactory.java @@ -0,0 +1,40 @@ +/******************************************************************************* + * Copyright (c) 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.factory; + +import org.eclipse.jpt.common.utility.factory.InterruptibleFactory; +import org.eclipse.jpt.common.utility.internal.ObjectTools; +import org.eclipse.jpt.common.utility.transformer.InterruptibleTransformer; + +/** + * @see TransformerFactory + */ +public class InterruptibleTransformerFactory<T> + implements InterruptibleFactory<T> +{ + private final InterruptibleTransformer<?, ? extends T> transformer; + + public InterruptibleTransformerFactory(InterruptibleTransformer<?, ? extends T> transformer) { + super(); + if (transformer == null) { + throw new NullPointerException(); + } + this.transformer = transformer; + } + + public T create() throws InterruptedException { + return this.transformer.transform(null); + } + + @Override + public String toString() { + return ObjectTools.toString(this, this.transformer); + } +} diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/factory/StaticMethodFactory.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/factory/StaticMethodFactory.java index 1393098821..128815e603 100644 --- a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/factory/StaticMethodFactory.java +++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/factory/StaticMethodFactory.java @@ -16,13 +16,13 @@ import org.eclipse.jpt.common.utility.factory.Factory; import org.eclipse.jpt.common.utility.internal.ObjectTools; /** - * A factory that uses Java reflection to return the value of a static field. - * Checked exceptions are converted to {@link RuntimeException}s. + * A factory that uses Java reflection to return the value returned by a static + * method. Checked exceptions are converted to {@link RuntimeException}s. * * @param <T> the type of objects returned by the factory * - * @see StaticMethodFactory - * @see java.lang.reflect.Field#get(Object) + * @see StaticFieldFactory + * @see java.lang.reflect.Method#invoke(Object, Object...) */ public class StaticMethodFactory<T> implements Factory<T> diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/factory/ThreadLocalFactory.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/factory/ThreadLocalFactory.java new file mode 100644 index 0000000000..4dd5341085 --- /dev/null +++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/factory/ThreadLocalFactory.java @@ -0,0 +1,65 @@ +/******************************************************************************* + * Copyright (c) 2008, 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.factory; + +import org.eclipse.jpt.common.utility.factory.Factory; + +/** + * This factory allows the client to specify a different factory for each + * thread. If there is no factory for the current thread, the configured default + * factory is used. + * + * @param <T> the type of the object returned by the factory + * + * @see #set(Factory) + */ +public class ThreadLocalFactory<T> + implements Factory<T> +{ + private final ThreadLocal<Factory<? extends T>> threadLocal; + private final Factory<? extends T> defaultFactory; + + public ThreadLocalFactory(Factory<? extends T> defaultFactory) { + super(); + if (defaultFactory == null) { + throw new NullPointerException(); + } + this.defaultFactory = defaultFactory; + this.threadLocal = this.buildThreadLocal(); + } + + private ThreadLocal<Factory<? extends T>> buildThreadLocal() { + return new ThreadLocal<Factory<? extends T>>(); + } + + public T create() { + return this.get().create(); + } + + private Factory<? extends T> get() { + Factory<? extends T> factory = this.threadLocal.get(); + return (factory != null) ? factory : this.defaultFactory; + } + + /** + * Set the current thread's factory to the specified value. + */ + public void set(Factory<? extends T> factory) { + this.threadLocal.set(factory); + } + + /** + * Return the string representation of the current thread's factory. + */ + @Override + public String toString() { + return this.get().toString(); + } +} diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/factory/ThreadLocalInterruptibleFactory.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/factory/ThreadLocalInterruptibleFactory.java new file mode 100644 index 0000000000..f882fbc7ae --- /dev/null +++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/factory/ThreadLocalInterruptibleFactory.java @@ -0,0 +1,59 @@ +/******************************************************************************* + * Copyright (c) 2008, 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.factory; + +import org.eclipse.jpt.common.utility.factory.InterruptibleFactory; + +/** + * @see ThreadLocalFactory + */ +public class ThreadLocalInterruptibleFactory<T> + implements InterruptibleFactory<T> +{ + private final ThreadLocal<InterruptibleFactory<? extends T>> threadLocal; + private final InterruptibleFactory<? extends T> defaultInterruptibleFactory; + + public ThreadLocalInterruptibleFactory(InterruptibleFactory<? extends T> defaultInterruptibleFactory) { + super(); + if (defaultInterruptibleFactory == null) { + throw new NullPointerException(); + } + this.defaultInterruptibleFactory = defaultInterruptibleFactory; + this.threadLocal = this.buildThreadLocal(); + } + + private ThreadLocal<InterruptibleFactory<? extends T>> buildThreadLocal() { + return new ThreadLocal<InterruptibleFactory<? extends T>>(); + } + + public T create() throws InterruptedException { + return this.get().create(); + } + + private InterruptibleFactory<? extends T> get() { + InterruptibleFactory<? extends T> factory = this.threadLocal.get(); + return (factory != null) ? factory : this.defaultInterruptibleFactory; + } + + /** + * Set the current thread's factory to the specified value. + */ + public void set(InterruptibleFactory<? extends T> factory) { + this.threadLocal.set(factory); + } + + /** + * Return the string representation of the current thread's factory. + */ + @Override + public String toString() { + return this.get().toString(); + } +} diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/factory/TransformerFactory.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/factory/TransformerFactory.java new file mode 100644 index 0000000000..fcd64fd8d4 --- /dev/null +++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/factory/TransformerFactory.java @@ -0,0 +1,48 @@ +/******************************************************************************* + * Copyright (c) 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.factory; + +import org.eclipse.jpt.common.utility.factory.Factory; +import org.eclipse.jpt.common.utility.internal.ObjectTools; +import org.eclipse.jpt.common.utility.transformer.Transformer; + +/** + * Adapt a {@link Transformer} to the {@link Factory} interface. + * The transformer is passed <code>null</code> for input and its output is + * returned by the factory. This really only useful for a transformer that + * accepts <code>null</code> input. + * + * @param <T> the type of the object returned by the factory + * + * @see org.eclipse.jpt.common.utility.internal.transformer.FactoryTransformer + */ +public class TransformerFactory<T> + implements Factory<T> +{ + private final Transformer<?, ? extends T> transformer; + + + public TransformerFactory(Transformer<?, ? extends T> transformer) { + super(); + if (transformer == null) { + throw new NullPointerException(); + } + this.transformer = transformer; + } + + public T create() { + return this.transformer.transform(null); + } + + @Override + public String toString() { + return ObjectTools.toString(this, this.transformer); + } +} diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/transformer/AbstractInterruptibleTransformer.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/transformer/AbstractInterruptibleTransformer.java new file mode 100644 index 0000000000..16d7fb2964 --- /dev/null +++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/transformer/AbstractInterruptibleTransformer.java @@ -0,0 +1,28 @@ +/******************************************************************************* + * 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.transformer; + +/** + * @see AbstractTransformer + */ +public abstract class AbstractInterruptibleTransformer<I, O> + extends InterruptibleTransformerAdapter<I, O> +{ + @Override + public O transform(I input) throws InterruptedException { + return (input == null) ? null : this.transform_(input); + } + + /** + * Transform the specified input; its value is guaranteed to be not + * <code>null</code>. + */ + protected abstract O transform_(I input) throws InterruptedException; +} diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/transformer/CastingInterruptibleTransformerWrapper.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/transformer/CastingInterruptibleTransformerWrapper.java new file mode 100644 index 0000000000..b609632f39 --- /dev/null +++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/transformer/CastingInterruptibleTransformerWrapper.java @@ -0,0 +1,44 @@ +/******************************************************************************* + * Copyright (c) 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.transformer; + +import org.eclipse.jpt.common.utility.internal.ObjectTools; +import org.eclipse.jpt.common.utility.transformer.InterruptibleTransformer; + +/** + * @see CastingTransformerWrapper + */ +public class CastingInterruptibleTransformerWrapper<I, X, O> + implements InterruptibleTransformer<I, O> +{ + private final InterruptibleTransformer<? super I, ? extends X> transformer; + + + public CastingInterruptibleTransformerWrapper(InterruptibleTransformer<? super I, ? extends X> transformer) { + super(); + if (transformer == null) { + throw new NullPointerException(); + } + this.transformer = transformer; + } + + /** + * Cast the output and hope for the best. + */ + @SuppressWarnings("unchecked") + public O transform(I input) throws InterruptedException { + return (O) this.transformer.transform(input); + } + + @Override + public String toString() { + return ObjectTools.toString(this, this.transformer); + } +} diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/transformer/ClosureTransformer.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/transformer/ClosureTransformer.java index 7d0adc5107..60bd840679 100644 --- a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/transformer/ClosureTransformer.java +++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/transformer/ClosureTransformer.java @@ -17,10 +17,10 @@ import org.eclipse.jpt.common.utility.transformer.Transformer; * Adapt a {@link Closure} to the {@link Transformer} interface. * The transformer will always return <code>null</code>. * - * @param <I> input: the type of the object passed to the transformer and forwarded - * to the closure - * @param <O> output: the type of the object returned by the transformer; always - * <code>null</code> + * @param <I> input: the type of the object passed to the transformer and + * forwarded to the closure + * @param <O> output: the type of the object returned by the transformer; + * always <code>null</code> * * @see org.eclipse.jpt.common.utility.internal.closure.TransformerClosure */ diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/transformer/CommandTransformer.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/transformer/CommandTransformer.java new file mode 100644 index 0000000000..440143317e --- /dev/null +++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/transformer/CommandTransformer.java @@ -0,0 +1,49 @@ +/******************************************************************************* + * Copyright (c) 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.transformer; + +import org.eclipse.jpt.common.utility.command.Command; +import org.eclipse.jpt.common.utility.internal.ObjectTools; +import org.eclipse.jpt.common.utility.transformer.Transformer; + +/** + * Adapt a {@link Command} to the {@link Transformer} interface. + * The transformer will always return <code>null</code>. + * + * @param <I> input: the type of the object passed to the transformer; + * ignored + * @param <O> output: the type of the object returned by the transformer; + * always <code>null</code> + * + * @see org.eclipse.jpt.common.utility.internal.command.TransformerCommand + */ +public class CommandTransformer<I, O> + implements Transformer<I, O> +{ + private final Command command; + + public CommandTransformer(Command command) { + super(); + if (command == null) { + throw new NullPointerException(); + } + this.command = command; + } + + public O transform(I input) { + this.command.execute(); + return null; + } + + @Override + public String toString() { + return ObjectTools.toString(this, this.command); + } +} diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/transformer/ConditionalInterruptibleTransformer.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/transformer/ConditionalInterruptibleTransformer.java new file mode 100644 index 0000000000..ef042b91fb --- /dev/null +++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/transformer/ConditionalInterruptibleTransformer.java @@ -0,0 +1,46 @@ +/******************************************************************************* + * Copyright (c) 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.transformer; + +import org.eclipse.jpt.common.utility.transformer.InterruptibleTransformer; +import org.eclipse.jpt.common.utility.internal.ObjectTools; +import org.eclipse.jpt.common.utility.predicate.Predicate; + +/** + * @see ConditionalTransformer + */ +public class ConditionalInterruptibleTransformer<I, O> + implements InterruptibleTransformer<I, O> +{ + private final Predicate<? super I> predicate; + private final InterruptibleTransformer<? super I, ? extends O> trueTransformer; + private final InterruptibleTransformer<? super I, ? extends O> falseTransformer; + + public ConditionalInterruptibleTransformer(Predicate<? super I> predicate, InterruptibleTransformer<? super I, ? extends O> trueTransformer, InterruptibleTransformer<? super I, ? extends O> falseTransformer) { + super(); + if ((predicate == null) || (trueTransformer == null) || (falseTransformer == null)) { + throw new NullPointerException(); + } + this.predicate = predicate; + this.trueTransformer = trueTransformer; + this.falseTransformer = falseTransformer; + } + + public O transform(I input) throws InterruptedException { + return this.predicate.evaluate(input) ? + this.trueTransformer.transform(input) : + this.falseTransformer.transform(input); + } + + @Override + public String toString() { + return ObjectTools.toString(this, this.predicate); + } +} diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/transformer/DowncastingInterruptibleTransformerWrapper.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/transformer/DowncastingInterruptibleTransformerWrapper.java new file mode 100644 index 0000000000..1cb5b6af25 --- /dev/null +++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/transformer/DowncastingInterruptibleTransformerWrapper.java @@ -0,0 +1,44 @@ +/******************************************************************************* + * Copyright (c) 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.transformer; + +import org.eclipse.jpt.common.utility.internal.ObjectTools; +import org.eclipse.jpt.common.utility.transformer.InterruptibleTransformer; + +/** + * @see DowncastingTransformerWrapper + */ +public class DowncastingInterruptibleTransformerWrapper<I, X, O extends X> + implements InterruptibleTransformer<I, O> +{ + private final InterruptibleTransformer<? super I, ? extends X> transformer; + + + public DowncastingInterruptibleTransformerWrapper(InterruptibleTransformer<? super I, ? extends X> transformer) { + super(); + if (transformer == null) { + throw new NullPointerException(); + } + this.transformer = transformer; + } + + /** + * Cast the output and hope for the best. + */ + @SuppressWarnings("unchecked") + public O transform(I input) throws InterruptedException { + return (O) this.transformer.transform(input); + } + + @Override + public String toString() { + return ObjectTools.toString(this, this.transformer); + } +} diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/transformer/FactoryTransformer.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/transformer/FactoryTransformer.java new file mode 100644 index 0000000000..df77b9e198 --- /dev/null +++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/transformer/FactoryTransformer.java @@ -0,0 +1,47 @@ +/******************************************************************************* + * Copyright (c) 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.transformer; + +import org.eclipse.jpt.common.utility.factory.Factory; +import org.eclipse.jpt.common.utility.internal.ObjectTools; +import org.eclipse.jpt.common.utility.transformer.Transformer; + +/** + * Adapt a {@link Factory} to the {@link Transformer} interface. + * + * @param <I> input: the type of the object passed to the transformer; + * ignored + * @param <O> output: the type of the object returned by the transformer; + * as generated by the factory + * + * @see org.eclipse.jpt.common.utility.internal.factory.TransformerFactory + */ +public class FactoryTransformer<I, O> + implements Transformer<I, O> +{ + private final Factory<? extends O> factory; + + public FactoryTransformer(Factory<? extends O> factory) { + super(); + if (factory == null) { + throw new NullPointerException(); + } + this.factory = factory; + } + + public O transform(I input) { + return this.factory.create(); + } + + @Override + public String toString() { + return ObjectTools.toString(this, this.factory); + } +} diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/transformer/InterruptibleClosureTransformer.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/transformer/InterruptibleClosureTransformer.java new file mode 100644 index 0000000000..cf1c2d917e --- /dev/null +++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/transformer/InterruptibleClosureTransformer.java @@ -0,0 +1,41 @@ +/******************************************************************************* + * Copyright (c) 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.transformer; + +import org.eclipse.jpt.common.utility.closure.InterruptibleClosure; +import org.eclipse.jpt.common.utility.internal.ObjectTools; +import org.eclipse.jpt.common.utility.transformer.InterruptibleTransformer; + +/** + * @see ClosureTransformer + */ +public class InterruptibleClosureTransformer<I, O> + implements InterruptibleTransformer<I, O> +{ + private final InterruptibleClosure<? super I> closure; + + public InterruptibleClosureTransformer(InterruptibleClosure<? super I> closure) { + super(); + if (closure == null) { + throw new NullPointerException(); + } + this.closure = closure; + } + + public O transform(I input) throws InterruptedException { + this.closure.execute(input); + return null; + } + + @Override + public String toString() { + return ObjectTools.toString(this, this.closure); + } +} diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/transformer/InterruptibleCommandTransformer.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/transformer/InterruptibleCommandTransformer.java new file mode 100644 index 0000000000..8811f86ba1 --- /dev/null +++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/transformer/InterruptibleCommandTransformer.java @@ -0,0 +1,41 @@ +/******************************************************************************* + * Copyright (c) 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.transformer; + +import org.eclipse.jpt.common.utility.command.InterruptibleCommand; +import org.eclipse.jpt.common.utility.internal.ObjectTools; +import org.eclipse.jpt.common.utility.transformer.InterruptibleTransformer; + +/** + * @see CommandTransformer + */ +public class InterruptibleCommandTransformer<I, O> + implements InterruptibleTransformer<I, O> +{ + private final InterruptibleCommand command; + + public InterruptibleCommandTransformer(InterruptibleCommand command) { + super(); + if (command == null) { + throw new NullPointerException(); + } + this.command = command; + } + + public O transform(I input) throws InterruptedException { + this.command.execute(); + return null; + } + + @Override + public String toString() { + return ObjectTools.toString(this, this.command); + } +} diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/transformer/InterruptibleFactoryTransformer.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/transformer/InterruptibleFactoryTransformer.java new file mode 100644 index 0000000000..b6dd2819ad --- /dev/null +++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/transformer/InterruptibleFactoryTransformer.java @@ -0,0 +1,40 @@ +/******************************************************************************* + * Copyright (c) 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.transformer; + +import org.eclipse.jpt.common.utility.factory.InterruptibleFactory; +import org.eclipse.jpt.common.utility.internal.ObjectTools; +import org.eclipse.jpt.common.utility.transformer.InterruptibleTransformer; + +/** + * @see FactoryTransformer + */ +public class InterruptibleFactoryTransformer<I, O> + implements InterruptibleTransformer<I, O> +{ + private final InterruptibleFactory<? extends O> factory; + + public InterruptibleFactoryTransformer(InterruptibleFactory<? extends O> factory) { + super(); + if (factory == null) { + throw new NullPointerException(); + } + this.factory = factory; + } + + public O transform(I input) throws InterruptedException { + return this.factory.create(); + } + + @Override + public String toString() { + return ObjectTools.toString(this, this.factory); + } +} diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/transformer/InterruptibleTransformerAdapter.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/transformer/InterruptibleTransformerAdapter.java new file mode 100644 index 0000000000..209f8dcd2c --- /dev/null +++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/transformer/InterruptibleTransformerAdapter.java @@ -0,0 +1,29 @@ +/******************************************************************************* + * 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.transformer; + +import org.eclipse.jpt.common.utility.internal.ObjectTools; +import org.eclipse.jpt.common.utility.transformer.InterruptibleTransformer; + +/** + * @see TransformerAdapter + */ +public class InterruptibleTransformerAdapter<I, O> + implements InterruptibleTransformer<I, O> +{ + public O transform(I input) throws InterruptedException { + return null; + } + + @Override + public String toString() { + return ObjectTools.toString(this); + } +} diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/transformer/InterruptibleTransformerChain.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/transformer/InterruptibleTransformerChain.java new file mode 100644 index 0000000000..cc4e91e3b3 --- /dev/null +++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/transformer/InterruptibleTransformerChain.java @@ -0,0 +1,45 @@ +/******************************************************************************* + * Copyright (c) 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.transformer; + +import org.eclipse.jpt.common.utility.internal.ObjectTools; +import org.eclipse.jpt.common.utility.internal.iterable.IterableTools; +import org.eclipse.jpt.common.utility.transformer.InterruptibleTransformer; + +/** + * @see TransformerChain + */ +public class InterruptibleTransformerChain<I, O> + implements InterruptibleTransformer<I, O> +{ + private final Iterable<InterruptibleTransformer<?, ?>> transformers; + + public InterruptibleTransformerChain(Iterable<InterruptibleTransformer<?, ?>> transformers) { + super(); + if (IterableTools.isOrContainsNull(transformers)) { + throw new NullPointerException(); + } + this.transformers = transformers; + } + + @SuppressWarnings({ "unchecked", "rawtypes" }) + public O transform(I input) throws InterruptedException { + Object result = input; + for (InterruptibleTransformer transformer : this.transformers) { + result = transformer.transform(result); + } + return (O) result; + } + + @Override + public String toString() { + return ObjectTools.toString(this, this.transformers); + } +} diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/transformer/InterruptibleTransformerWrapper.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/transformer/InterruptibleTransformerWrapper.java new file mode 100644 index 0000000000..316aa22a8e --- /dev/null +++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/transformer/InterruptibleTransformerWrapper.java @@ -0,0 +1,43 @@ +/******************************************************************************* + * 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.transformer; + +import org.eclipse.jpt.common.utility.internal.ObjectTools; +import org.eclipse.jpt.common.utility.transformer.InterruptibleTransformer; + +/** + * @see TransformerWrapper + */ +public class InterruptibleTransformerWrapper<I, O> + implements InterruptibleTransformer<I, O> +{ + protected volatile InterruptibleTransformer<? super I, ? extends O> transformer; + + public InterruptibleTransformerWrapper(InterruptibleTransformer<? super I, ? extends O> transformer) { + super(); + this.setInterruptibleTransformer(transformer); + } + + public O transform(I input) throws InterruptedException { + return this.transformer.transform(input); + } + + public void setInterruptibleTransformer(InterruptibleTransformer<? super I, ? extends O> transformer) { + if (transformer == null) { + throw new NullPointerException(); + } + this.transformer = transformer; + } + + @Override + public String toString() { + return ObjectTools.toString(this, this.transformer); + } +} diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/transformer/NullCheckInterruptibleTransformerWrapper.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/transformer/NullCheckInterruptibleTransformerWrapper.java new file mode 100644 index 0000000000..ba755932bc --- /dev/null +++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/transformer/NullCheckInterruptibleTransformerWrapper.java @@ -0,0 +1,42 @@ +/******************************************************************************* + * Copyright (c) 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.transformer; + +import org.eclipse.jpt.common.utility.internal.ObjectTools; +import org.eclipse.jpt.common.utility.transformer.InterruptibleTransformer; + +/** + * @see NullCheckTransformerWrapper + */ +public class NullCheckInterruptibleTransformerWrapper<I, O> + implements InterruptibleTransformer<I, O> +{ + private final InterruptibleTransformer<? super I, ? extends O> transformer; + private final O nullOutput; + + + public NullCheckInterruptibleTransformerWrapper(InterruptibleTransformer<? super I, ? extends O> transformer, O nullOutput) { + super(); + if (transformer == null) { + throw new NullPointerException(); + } + this.transformer = transformer; + this.nullOutput = nullOutput; + } + + public O transform(I input) throws InterruptedException { + return (input == null) ? this.nullOutput : this.transformer.transform(input); + } + + @Override + public String toString() { + return ObjectTools.toString(this, this.transformer); + } +} diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/transformer/SafeInterruptibleTransformerWrapper.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/transformer/SafeInterruptibleTransformerWrapper.java new file mode 100644 index 0000000000..bf62cc7bf3 --- /dev/null +++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/transformer/SafeInterruptibleTransformerWrapper.java @@ -0,0 +1,52 @@ +/******************************************************************************* + * Copyright (c) 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.transformer; + +import org.eclipse.jpt.common.utility.exception.ExceptionHandler; +import org.eclipse.jpt.common.utility.internal.ObjectTools; +import org.eclipse.jpt.common.utility.transformer.InterruptibleTransformer; + +/** + * @see SafeTransformerWrapper + */ +public class SafeInterruptibleTransformerWrapper<I, O> + implements InterruptibleTransformer<I, O> +{ + private final InterruptibleTransformer<? super I, ? extends O> transformer; + private final ExceptionHandler exceptionHandler; + private final O exceptionOutput; + + + public SafeInterruptibleTransformerWrapper(InterruptibleTransformer<? super I, ? extends O> transformer, ExceptionHandler exceptionHandler, O exceptionOutput) { + super(); + if ((transformer == null) || (exceptionHandler == null)) { + throw new NullPointerException(); + } + this.transformer = transformer; + this.exceptionHandler = exceptionHandler; + this.exceptionOutput = exceptionOutput; + } + + public O transform(I input) throws InterruptedException { + try { + return this.transformer.transform(input); + } catch (InterruptedException ex) { + throw ex; + } catch (Throwable ex) { + this.exceptionHandler.handleException(ex); + return this.exceptionOutput; + } + } + + @Override + public String toString() { + return ObjectTools.toString(this, this.transformer); + } +} diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/transformer/SwitchInterruptibleTransformer.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/transformer/SwitchInterruptibleTransformer.java new file mode 100644 index 0000000000..e4d8a25d42 --- /dev/null +++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/transformer/SwitchInterruptibleTransformer.java @@ -0,0 +1,49 @@ +/******************************************************************************* + * Copyright (c) 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.transformer; + +import org.eclipse.jpt.common.utility.Association; +import org.eclipse.jpt.common.utility.transformer.InterruptibleTransformer; +import org.eclipse.jpt.common.utility.internal.ObjectTools; +import org.eclipse.jpt.common.utility.internal.iterable.IterableTools; +import org.eclipse.jpt.common.utility.predicate.Predicate; + +/** + * @see SwitchTransformer + */ +public class SwitchInterruptibleTransformer<I, O> + implements InterruptibleTransformer<I, O> +{ + private final Iterable<Association<Predicate<? super I>, InterruptibleTransformer<? super I, ? extends O>>> transformers; + private final InterruptibleTransformer<? super I, ? extends O> defaultInterruptibleTransformer; + + public SwitchInterruptibleTransformer(Iterable<Association<Predicate<? super I>, InterruptibleTransformer<? super I, ? extends O>>> transformers, InterruptibleTransformer<? super I, ? extends O> defaultInterruptibleTransformer) { + super(); + if (IterableTools.isOrContainsNull(transformers) || (defaultInterruptibleTransformer == null)) { + throw new NullPointerException(); + } + this.transformers = transformers; + this.defaultInterruptibleTransformer = defaultInterruptibleTransformer; + } + + public O transform(I input) throws InterruptedException { + for (Association<Predicate<? super I>, InterruptibleTransformer<? super I, ? extends O>> association : this.transformers) { + if (association.getKey().evaluate(input)) { + return association.getValue().transform(input); // execute only one transformer + } + } + return this.defaultInterruptibleTransformer.transform(input); + } + + @Override + public String toString() { + return ObjectTools.toString(this, this.transformers); + } +} diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/transformer/ThreadLocalInterruptibleTransformer.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/transformer/ThreadLocalInterruptibleTransformer.java new file mode 100644 index 0000000000..8ff8031228 --- /dev/null +++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/transformer/ThreadLocalInterruptibleTransformer.java @@ -0,0 +1,62 @@ +/******************************************************************************* + * Copyright (c) 2008, 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.transformer; + +import org.eclipse.jpt.common.utility.transformer.InterruptibleTransformer; + +/** + * @see ThreadLocalTransformer + */ +public class ThreadLocalInterruptibleTransformer<I, O> + implements InterruptibleTransformer<I, O> +{ + private final ThreadLocal<InterruptibleTransformer<? super I, ? extends O>> threadLocal; + private final InterruptibleTransformer<? super I, ? extends O> defaultInterruptibleTransformer; + + public ThreadLocalInterruptibleTransformer(InterruptibleTransformer<? super I, ? extends O> defaultInterruptibleTransformer) { + super(); + if (defaultInterruptibleTransformer == null) { + throw new NullPointerException(); + } + this.defaultInterruptibleTransformer = defaultInterruptibleTransformer; + this.threadLocal = this.buildThreadLocal(); + } + + private ThreadLocal<InterruptibleTransformer<? super I, ? extends O>> buildThreadLocal() { + return new ThreadLocal<InterruptibleTransformer<? super I, ? extends O>>(); + } + + public O transform(I input) throws InterruptedException { + return this.get().transform(input); + } + + private InterruptibleTransformer<? super I, ? extends O> get() { + InterruptibleTransformer<? super I, ? extends O> transformer = this.threadLocal.get(); + if (transformer != null) { + return transformer; + } + return this.defaultInterruptibleTransformer; + } + + /** + * Set the current thread's transformer to the specified value. + */ + public void set(InterruptibleTransformer<? super I, ? extends O> transformer) { + this.threadLocal.set(transformer); + } + + /** + * Return the string representation of the current thread's transformer. + */ + @Override + public String toString() { + return this.get().toString(); + } +} diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/transformer/ThreadLocalTransformer.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/transformer/ThreadLocalTransformer.java new file mode 100644 index 0000000000..9280301faf --- /dev/null +++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/transformer/ThreadLocalTransformer.java @@ -0,0 +1,69 @@ +/******************************************************************************* + * Copyright (c) 2008, 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.transformer; + +import org.eclipse.jpt.common.utility.transformer.Transformer; + +/** + * This transformer allows the client to specify a different transformer for each + * thread. If there is no transformer for the current thread, the configured default + * transformer is used. + * + * @param <I> input: the type of the object passed to the transformer + * @param <O> output: the type of the object returned by the transformer + * + * @see #set(Transformer) + */ +public class ThreadLocalTransformer<I, O> + implements Transformer<I, O> +{ + private final ThreadLocal<Transformer<? super I, ? extends O>> threadLocal; + private final Transformer<? super I, ? extends O> defaultTransformer; + + public ThreadLocalTransformer(Transformer<? super I, ? extends O> defaultTransformer) { + super(); + if (defaultTransformer == null) { + throw new NullPointerException(); + } + this.defaultTransformer = defaultTransformer; + this.threadLocal = this.buildThreadLocal(); + } + + private ThreadLocal<Transformer<? super I, ? extends O>> buildThreadLocal() { + return new ThreadLocal<Transformer<? super I, ? extends O>>(); + } + + public O transform(I input) { + return this.get().transform(input); + } + + private Transformer<? super I, ? extends O> get() { + Transformer<? super I, ? extends O> transformer = this.threadLocal.get(); + if (transformer != null) { + return transformer; + } + return this.defaultTransformer; + } + + /** + * Set the current thread's transformer to the specified value. + */ + public void set(Transformer<? super I, ? extends O> transformer) { + this.threadLocal.set(transformer); + } + + /** + * Return the string representation of the current thread's transformer. + */ + @Override + public String toString() { + return this.get().toString(); + } +} diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/transformer/TransformerTools.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/transformer/TransformerTools.java index 74f1db863c..9fc7a86733 100644 --- a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/transformer/TransformerTools.java +++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/transformer/TransformerTools.java @@ -13,7 +13,9 @@ import java.util.Iterator; import java.util.Map; import org.eclipse.jpt.common.utility.Association; import org.eclipse.jpt.common.utility.closure.Closure; +import org.eclipse.jpt.common.utility.command.Command; import org.eclipse.jpt.common.utility.exception.ExceptionHandler; +import org.eclipse.jpt.common.utility.factory.Factory; import org.eclipse.jpt.common.utility.internal.ArrayTools; import org.eclipse.jpt.common.utility.internal.ClassTools; import org.eclipse.jpt.common.utility.internal.ObjectTools; @@ -26,6 +28,41 @@ import org.eclipse.jpt.common.utility.transformer.Transformer; */ public final class TransformerTools { + // ********** adapters ********** + + /** + * Adapt the specified {@link Closure} to the {@link Transformer} interface. + * The returned transformer will always return <code>null</code>. + * @param <I> input: the type of the object passed to the transformer + * @param <O> output: the type of the object returned by the transformer + */ + public static <I, O> Transformer<I, O> adapt(Closure<? super I> closure) { + return new ClosureTransformer<I, O>(closure); + } + + /** + * Adapt the specified {@link Command} to the {@link Transformer} interface. + * The returned transformer will always return <code>null</code> and its + * input will be ignored. + * @param <I> input: the type of the object passed to the transformer + * @param <O> output: the type of the object returned by the transformer + */ + public static <I, O> Transformer<I, O> adapt(Command command) { + return new CommandTransformer<I, O>(command); + } + + /** + * Adapt the specified {@link Factory} to the {@link Transformer} interface. + * The returned transformer will return the factory's output and its + * input will be ignored. + * @param <I> input: the type of the object passed to the transformer + * @param <O> output: the type of the object returned by the transformer + */ + public static <I, O> Transformer<I, O> adapt(Factory<? extends O> factory) { + return new FactoryTransformer<I, O>(factory); + } + + // ********** object to string ********** /** @@ -344,6 +381,33 @@ public final class TransformerTools { } + // ********** thread local ********** + + /** + * Return a transformer that allows the client to specify a different transformer + * for each thread. If there is no transformer for the current thread, the + * transformer will return <code>null</code>. + * @param <I> input: the type of the object passed to the transformer + * @param <O> output: the type of the object returned by the transformer + * @see ThreadLocalTransformer + */ + public static <I, O> ThreadLocalTransformer<I, O> threadLocalTransformer() { + return threadLocalTransformer(NullOutputTransformer.<I, O>instance()); + } + + /** + * Return a transformer that allows the client to specify a different transformer + * for each thread. If there is no transformer for the current thread, the + * specified default transformer is used. + * @param <I> input: the type of the object passed to the transformer + * @param <O> output: the type of the object returned by the transformer + * @see ThreadLocalTransformer + */ + public static <I, O> ThreadLocalTransformer<I, O> threadLocalTransformer(Transformer<? super I, ? extends O> defaultTransformer) { + return new ThreadLocalTransformer<I, O>(defaultTransformer); + } + + // ********** wrappers ********** /** @@ -394,7 +458,7 @@ public final class TransformerTools { * @param <O> output: the type of the object returned by the transformer * @see TransformerWrapper */ - public static <I, O> Transformer<I, O> wrap(Transformer<? super I, ? extends O> transformer) { + public static <I, O> TransformerWrapper<I, O> wrap(Transformer<? super I, ? extends O> transformer) { return new TransformerWrapper<I, O>(transformer); } @@ -653,20 +717,6 @@ public final class TransformerTools { } - // ********** closure ********** - - /** - * Adapt the specified {@link Closure} to the {@link Transformer} interface. - * The returned transformer will always return <code>null</code>. - * @param <I> input: the type of the object passed to the transformer - * @param <O> output: the type of the object returned by the transformer - * @see ClosureTransformer - */ - public static <I, O> Transformer<I, O> adapt(Closure<? super I> closure) { - return new ClosureTransformer<I, O>(closure); - } - - // ********** conditional ********** /** diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/transformer/UpcastingInterruptibleTransformerWrapper.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/transformer/UpcastingInterruptibleTransformerWrapper.java new file mode 100644 index 0000000000..13a38ce5c2 --- /dev/null +++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/transformer/UpcastingInterruptibleTransformerWrapper.java @@ -0,0 +1,43 @@ +/******************************************************************************* + * Copyright (c) 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.transformer; + +import org.eclipse.jpt.common.utility.internal.ObjectTools; +import org.eclipse.jpt.common.utility.transformer.InterruptibleTransformer; + +/** + * @see UpcastingTransformerWrapper + */ +public class UpcastingInterruptibleTransformerWrapper<I, O, X extends O> + implements InterruptibleTransformer<I, O> +{ + private final InterruptibleTransformer<? super I, ? extends X> transformer; + + public UpcastingInterruptibleTransformerWrapper(InterruptibleTransformer<? super I, ? extends X> transformer) { + super(); + if (transformer == null) { + throw new NullPointerException(); + } + this.transformer = transformer; + } + + /** + * No need for casting as the type is guaranteed by the generic type + * argument. + */ + public O transform(I input) throws InterruptedException { + return this.transformer.transform(input); + } + + @Override + public String toString() { + return ObjectTools.toString(this, this.transformer); + } +} diff --git a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/command/CommandTests.java b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/command/CommandTests.java index 00c9e90d34..da16842288 100644 --- a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/command/CommandTests.java +++ b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/command/CommandTests.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2008, 2012 Oracle. All rights reserved. + * Copyright (c) 2008, 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. @@ -12,6 +12,7 @@ package org.eclipse.jpt.common.utility.tests.internal.command; import org.eclipse.jpt.common.utility.command.Command; import org.eclipse.jpt.common.utility.command.CommandContext; import org.eclipse.jpt.common.utility.internal.command.CommandRunnable; +import org.eclipse.jpt.common.utility.internal.command.CommandTools; import org.eclipse.jpt.common.utility.internal.command.DisabledCommand; import org.eclipse.jpt.common.utility.internal.command.NullCommand; import org.eclipse.jpt.common.utility.internal.command.RunnableCommand; @@ -96,7 +97,7 @@ public class CommandTests } public void testThreadLocalCommand() throws Exception { - ThreadLocalCommand threadLocalCommand = new ThreadLocalCommand(); + ThreadLocalCommand threadLocalCommand = CommandTools.threadLocalCommand(); TestRunnable testRunnable1 = new TestRunnable(threadLocalCommand, 1); Thread thread1 = this.buildThread(testRunnable1); thread1.run(); diff --git a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/command/CompositeCommandTests.java b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/command/CompositeCommandTests.java index 69aa1f8c8d..a6e2a53cd4 100644 --- a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/command/CompositeCommandTests.java +++ b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/command/CompositeCommandTests.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2010, 2012 Oracle. All rights reserved. + * Copyright (c) 2010, 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. @@ -10,10 +10,9 @@ package org.eclipse.jpt.common.utility.tests.internal.command; import junit.framework.TestCase; - import org.eclipse.jpt.common.utility.command.Command; import org.eclipse.jpt.common.utility.internal.command.CommandRunnable; -import org.eclipse.jpt.common.utility.internal.command.CompositeCommand; +import org.eclipse.jpt.common.utility.internal.command.CommandTools; public class CompositeCommandTests extends TestCase { boolean command1Executed = false; @@ -36,7 +35,7 @@ public class CompositeCommandTests extends TestCase { } private Command buildCompositeCommand() { - return new CompositeCommand( + return CommandTools.composite( this.buildCommand1(), this.buildCommand2() ); |