Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 643491a8997971a81b34e2d8ba9922c3510a95c0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
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();
	}
}

Back to the top