| Revision | 270 (tree) |
|---|---|
| Time | 2016-05-03 20:58:20 |
| Author | t_nakayama1971 |
(empty log message)
| @@ -14,6 +14,7 @@ | ||
| 14 | 14 | * See the License for the specific language governing permissions and |
| 15 | 15 | * limitations under the License. |
| 16 | 16 | */ |
| 17 | + | |
| 17 | 18 | package org.apache.commons.chain2; |
| 18 | 19 | |
| 19 | 20 | import java.util.Map; |
| @@ -21,11 +22,11 @@ | ||
| 21 | 22 | /** |
| 22 | 23 | * Allows specifying a name for a {@link Command} in a {@link Catalog} instance. |
| 23 | 24 | * |
| 25 | + * @version $Id$ | |
| 24 | 26 | * @param <K> Context key type |
| 25 | 27 | * @param <V> Context value type |
| 26 | 28 | * @param <C> Type of the context associated with this name setter |
| 27 | 29 | * @since 2.0 |
| 28 | - * @version $Id$ | |
| 29 | 30 | */ |
| 30 | 31 | public interface NameSetter<K, V, C extends Map<K, V>> { |
| 31 | 32 |
| @@ -27,11 +27,11 @@ | ||
| 27 | 27 | * is optional, but convenient when there are multiple possible chains |
| 28 | 28 | * that can be selected and executed based on environmental conditions.</p> |
| 29 | 29 | * |
| 30 | + * @version $Id$ | |
| 31 | + * | |
| 30 | 32 | * @param <K> the type of keys maintained by the context associated with this command |
| 31 | 33 | * @param <V> the type of mapped values |
| 32 | 34 | * @param <C> Type of the context associated with this command |
| 33 | - * | |
| 34 | - * @version $Id$ | |
| 35 | 35 | */ |
| 36 | 36 | public interface Catalog<K, V, C extends Map<K, V>> { |
| 37 | 37 |
| @@ -14,6 +14,7 @@ | ||
| 14 | 14 | * See the License for the specific language governing permissions and |
| 15 | 15 | * limitations under the License. |
| 16 | 16 | */ |
| 17 | + | |
| 17 | 18 | package org.apache.commons.chain2; |
| 18 | 19 | |
| 19 | 20 | import java.util.Map; |
| @@ -21,11 +22,11 @@ | ||
| 21 | 22 | /** |
| 22 | 23 | * Allows adding a command in a {@link Catalog} identified by a name. |
| 23 | 24 | * |
| 25 | + * @version $Id$ | |
| 24 | 26 | * @param <K> Context key type |
| 25 | 27 | * @param <V> Context value type |
| 26 | 28 | * @param <C> Type of the context associated with this command setter |
| 27 | 29 | * @since 2.0 |
| 28 | - * @version $Id$ | |
| 29 | 30 | */ |
| 30 | 31 | public interface NamedCommandSetter<K, V, C extends Map<K, V>> |
| 31 | 32 | extends CommandSetter<K, V, C, NameSetter<K, V, C>> { |
| @@ -14,11 +14,12 @@ | ||
| 14 | 14 | * See the License for the specific language governing permissions and |
| 15 | 15 | * limitations under the License. |
| 16 | 16 | */ |
| 17 | + | |
| 17 | 18 | package org.apache.commons.chain2; |
| 18 | 19 | |
| 19 | 20 | /** |
| 20 | 21 | * A <code>Processing</code> encapsulates states that can be returned by |
| 21 | - * commands. | |
| 22 | + * commands. | |
| 22 | 23 | * <p> |
| 23 | 24 | * {@link Command}s should either return <code>FINISHED</code> if the |
| 24 | 25 | * processing of the given context has been completed, or return |
| @@ -30,7 +31,7 @@ | ||
| 30 | 31 | public enum Processing { |
| 31 | 32 | |
| 32 | 33 | /** |
| 33 | - * Commands should return continue if the processing of the given | |
| 34 | + * Commands should return continue if the processing of the given | |
| 34 | 35 | * context should be delegated to a subsequent command in an enclosing chain. |
| 35 | 36 | * |
| 36 | 37 | * @since Chain 2.0 |
| @@ -14,12 +14,11 @@ | ||
| 14 | 14 | * See the License for the specific language governing permissions and |
| 15 | 15 | * limitations under the License. |
| 16 | 16 | */ |
| 17 | + | |
| 17 | 18 | package org.apache.commons.chain2; |
| 18 | 19 | |
| 19 | 20 | import java.util.Map; |
| 20 | 21 | |
| 21 | -import org.apache.commons.chain2.Processing; | |
| 22 | - | |
| 23 | 22 | /** |
| 24 | 23 | * <p>A {@link Command} encapsulates a unit of processing work to be |
| 25 | 24 | * performed, whose purpose is to examine and/or modify the state of a |
| @@ -76,33 +75,33 @@ | ||
| 76 | 75 | * being used in this fashion, as opposed to JavaBeans properties that simply |
| 77 | 76 | * configure the internal operation of this {@link Command}.</p> |
| 78 | 77 | * |
| 78 | + * @version $Id$ | |
| 79 | + * | |
| 79 | 80 | * @param <K> the type of keys maintained by the context associated with this command |
| 80 | 81 | * @param <V> the type of mapped values |
| 81 | 82 | * @param <C> Type of the context associated with this command |
| 82 | - * | |
| 83 | - * @version $Id$ | |
| 84 | 83 | */ |
| 85 | 84 | public interface Command<K, V, C extends Map<K, V>> { |
| 86 | 85 | |
| 87 | 86 | /** |
| 88 | - * Execute a unit of processing work to be performed. | |
| 87 | + * Execute a unit of processing work to be performed. | |
| 89 | 88 | * <p> |
| 90 | - * A command may either complete the required processing and return | |
| 91 | - * finished, or delegate remaining processing to the subsequent command | |
| 89 | + * A command may either complete the required processing and return | |
| 90 | + * finished, or delegate remaining processing to the subsequent command | |
| 92 | 91 | * in the enclosing {@link Chain} by returning continue. |
| 93 | 92 | * |
| 94 | 93 | * @param context The {@link Context} to be processed by this |
| 95 | 94 | * {@link Command} |
| 96 | 95 | * |
| 96 | + * @return {@link Processing#FINISHED} if the processing of this contex | |
| 97 | + * has been completed. Returns {@link Processing#CONTINUE} if the processing | |
| 98 | + * of this context should be delegated to a subsequent command in an | |
| 99 | + * enclosing chain. | |
| 100 | + * | |
| 97 | 101 | * @exception ChainException general purpose exception return |
| 98 | 102 | * to indicate abnormal termination |
| 99 | 103 | * @exception IllegalArgumentException if <code>context</code> |
| 100 | 104 | * is <code>null</code> |
| 101 | - * | |
| 102 | - * @return {@link Processing#FINISHED} if the processing of this contex | |
| 103 | - * has been completed. Returns {@link Processing#CONTINUE} if the processing | |
| 104 | - * of this context should be delegated to a subsequent command in an | |
| 105 | - * enclosing chain. | |
| 106 | 105 | */ |
| 107 | 106 | Processing execute(C context); |
| 108 | 107 |
| @@ -14,6 +14,7 @@ | ||
| 14 | 14 | * See the License for the specific language governing permissions and |
| 15 | 15 | * limitations under the License. |
| 16 | 16 | */ |
| 17 | + | |
| 17 | 18 | package org.apache.commons.chain2; |
| 18 | 19 | |
| 19 | 20 | import java.util.Map; |
| @@ -48,11 +49,11 @@ | ||
| 48 | 49 | * the <code>execute()</code> method of the {@link Chain} is first called. |
| 49 | 50 | * After that, the configuration of the {@link Chain} is frozen.</p> |
| 50 | 51 | * |
| 52 | + * @version $Id$ | |
| 53 | + * | |
| 51 | 54 | * @param <K> Context key type |
| 52 | 55 | * @param <V> Context value type |
| 53 | 56 | * @param <C> Type of the context associated with this chain |
| 54 | - * | |
| 55 | - * @version $Id$ | |
| 56 | 57 | */ |
| 57 | 58 | public interface Chain<K, V, C extends Map<K, V>> extends Command<K, V, C> { |
| 58 | 59 |
| @@ -63,7 +64,7 @@ | ||
| 63 | 64 | * at least once, it is no longer possible to add additional |
| 64 | 65 | * {@link Command}s; instead, an exception will be thrown.</p> |
| 65 | 66 | * |
| 66 | - * @param <CMD> the {@link Command} type to be added in the {@link Chain} | |
| 67 | + * @param <T> the {@link Command} type to be added in the {@link Chain} | |
| 67 | 68 | * @param command The {@link Command} to be added |
| 68 | 69 | * |
| 69 | 70 | * @exception IllegalArgumentException if <code>command</code> |
| @@ -71,7 +72,7 @@ | ||
| 71 | 72 | * @exception IllegalStateException if this {@link Chain} has already |
| 72 | 73 | * been executed at least once, so no further configuration is allowed |
| 73 | 74 | */ |
| 74 | - <CMD extends Command<K, V, C>> void addCommand(CMD command); | |
| 75 | + <T extends Command<K, V, C>> void addCommand(T command); | |
| 75 | 76 | |
| 76 | 77 | /** |
| 77 | 78 | * <p>Execute the processing represented by this {@link Chain} according |
| @@ -14,6 +14,7 @@ | ||
| 14 | 14 | * See the License for the specific language governing permissions and |
| 15 | 15 | * limitations under the License. |
| 16 | 16 | */ |
| 17 | + | |
| 17 | 18 | package org.apache.commons.chain2; |
| 18 | 19 | |
| 19 | 20 | import java.util.Map; |
| @@ -54,9 +55,9 @@ | ||
| 54 | 55 | * simultaneously unless this is explicitly documented for a particular |
| 55 | 56 | * implementation.</p> |
| 56 | 57 | * |
| 58 | + * @version $Id$ | |
| 57 | 59 | * @param <K> the type of keys maintained by the context associated with this catalog |
| 58 | 60 | * @param <V> the type of mapped values |
| 59 | - * @version $Id$ | |
| 60 | 61 | */ |
| 61 | 62 | public interface Context<K, V> extends Map<K, V> { |
| 62 | 63 |
| @@ -39,11 +39,11 @@ | ||
| 39 | 39 | * can reliably release such resources in the <code>postprocess()</code> |
| 40 | 40 | * method, which is guaranteed to be called by the owning {@link Chain}.</p> |
| 41 | 41 | * |
| 42 | + * @version $Id$ | |
| 43 | + * | |
| 42 | 44 | * @param <K> the type of keys maintained by the context associated with this command |
| 43 | 45 | * @param <V> the type of mapped values |
| 44 | 46 | * @param <C> Type of the context associated with this command |
| 45 | - * | |
| 46 | - * @version $Id$ | |
| 47 | 47 | */ |
| 48 | 48 | public interface Filter<K, V, C extends Map<K, V>> extends Command<K, V, C> { |
| 49 | 49 |
| @@ -58,12 +58,12 @@ | ||
| 58 | 58 | * by the last {@link Command} that was executed; otherwise |
| 59 | 59 | * <code>null</code> |
| 60 | 60 | * |
| 61 | - * @exception IllegalArgumentException if <code>context</code> | |
| 62 | - * is <code>null</code> | |
| 63 | - * | |
| 64 | 61 | * @return If a non-null <code>exception</code> was "handled" by this |
| 65 | 62 | * method (and therefore need not be rethrown), return <code>FINISHED</code>; |
| 66 | 63 | * otherwise return <code>CONTINUE</code> |
| 64 | + * | |
| 65 | + * @exception IllegalArgumentException if <code>context</code> | |
| 66 | + * is <code>null</code> | |
| 67 | 67 | */ |
| 68 | 68 | boolean postprocess(C context, Exception exception); |
| 69 | 69 |
| @@ -14,6 +14,7 @@ | ||
| 14 | 14 | * See the License for the specific language governing permissions and |
| 15 | 15 | * limitations under the License. |
| 16 | 16 | */ |
| 17 | + | |
| 17 | 18 | package org.apache.commons.chain2; |
| 18 | 19 | |
| 19 | 20 | import java.util.Map; |
| @@ -21,11 +22,11 @@ | ||
| 21 | 22 | /** |
| 22 | 23 | * First builder that allows adding a command in the target chain. |
| 23 | 24 | * |
| 25 | + * @version $Id$ | |
| 24 | 26 | * @param <K> Context key type |
| 25 | 27 | * @param <V> Context value type |
| 26 | 28 | * @param <C> Type of the context associated with this chain executor |
| 27 | 29 | * @since 2.0 |
| 28 | - * @version $Id$ | |
| 29 | 30 | */ |
| 30 | 31 | public interface ToExecutorCommandSetter<K, V, C extends Map<K, V>> |
| 31 | 32 | extends CommandSetter<K, V, C, ChainExecutor<K, V, C>> { |
| @@ -14,6 +14,7 @@ | ||
| 14 | 14 | * See the License for the specific language governing permissions and |
| 15 | 15 | * limitations under the License. |
| 16 | 16 | */ |
| 17 | + | |
| 17 | 18 | package org.apache.commons.chain2; |
| 18 | 19 | |
| 19 | 20 | import java.util.Map; |
| @@ -27,19 +28,13 @@ | ||
| 27 | 28 | */ |
| 28 | 29 | public class ChainException extends RuntimeException { |
| 29 | 30 | |
| 30 | - /** | |
| 31 | - * | |
| 32 | - */ | |
| 31 | + /** serialVersionUID */ | |
| 33 | 32 | private static final long serialVersionUID = 20120724L; |
| 34 | 33 | |
| 35 | - /** | |
| 36 | - * Context used when exception occurred. | |
| 37 | - */ | |
| 34 | + /** Context used when exception occurred. */ | |
| 38 | 35 | private final Map<?, ?> context; |
| 39 | 36 | |
| 40 | - /** | |
| 41 | - * Command that failed when exception occurred. | |
| 42 | - */ | |
| 37 | + /** Command that failed when exception occurred. */ | |
| 43 | 38 | private final transient Command<?, ?, ?> failedCommand; |
| 44 | 39 | |
| 45 | 40 | /** |
| @@ -46,7 +41,7 @@ | ||
| 46 | 41 | * Create an exception object with a message. |
| 47 | 42 | * @param message Message to associate with exception |
| 48 | 43 | */ |
| 49 | - public ChainException(String message) { | |
| 44 | + public ChainException(final String message) { | |
| 50 | 45 | super(message); |
| 51 | 46 | this.context = null; |
| 52 | 47 | this.failedCommand = null; |
| @@ -57,7 +52,7 @@ | ||
| 57 | 52 | * @param message Message to associate with exception |
| 58 | 53 | * @param cause Exception to chain to this exception |
| 59 | 54 | */ |
| 60 | - public ChainException(String message, Throwable cause) { | |
| 55 | + public ChainException(final String message, final Throwable cause) { | |
| 61 | 56 | super(message, cause); |
| 62 | 57 | this.context = null; |
| 63 | 58 | this.failedCommand = null; |
| @@ -75,16 +70,16 @@ | ||
| 75 | 70 | * {@link #getCause()} method). (A <tt>null</tt> value is |
| 76 | 71 | * permitted, and indicates that the cause is nonexistent or |
| 77 | 72 | * unknown.) |
| 78 | - * @param context The Context object passed to the {@link Command} in | |
| 73 | + * @param ctx The Context object passed to the {@link Command} in | |
| 79 | 74 | * which the exception occurred. |
| 80 | - * @param failedCommand The Command object in which the exception was | |
| 75 | + * @param failed The Command object in which the exception was | |
| 81 | 76 | * thrown. |
| 82 | 77 | */ |
| 83 | - public <K, V, C extends Map<K, V>> ChainException(String message, Throwable cause, | |
| 84 | - C context, Command<K, V, C> failedCommand) { | |
| 78 | + public <K, V, C extends Map<K, V>> ChainException(final String message, | |
| 79 | + final Throwable cause, final C ctx, final Command<K, V, C> failed) { | |
| 85 | 80 | super(message, cause); |
| 86 | - this.context = context; | |
| 87 | - this.failedCommand = failedCommand; | |
| 81 | + this.context = ctx; | |
| 82 | + this.failedCommand = failed; | |
| 88 | 83 | } |
| 89 | 84 | |
| 90 | 85 | /** |
| @@ -91,8 +86,8 @@ | ||
| 91 | 86 | * @return The context object passed when the {@link Command} |
| 92 | 87 | * threw an exception. |
| 93 | 88 | */ |
| 94 | - public Map<?, ?> getContext() { | |
| 95 | - return context; | |
| 89 | + public final Map<?, ?> getContext() { | |
| 90 | + return this.context; | |
| 96 | 91 | } |
| 97 | 92 | |
| 98 | 93 | /** |
| @@ -99,8 +94,8 @@ | ||
| 99 | 94 | * @return The {@link Command} object in which the original exception was |
| 100 | 95 | * thrown. |
| 101 | 96 | */ |
| 102 | - public Command<?, ?, ?> getFailedCommand() { | |
| 103 | - return failedCommand; | |
| 97 | + public final Command<?, ?, ?> getFailedCommand() { | |
| 98 | + return this.failedCommand; | |
| 104 | 99 | } |
| 105 | 100 | |
| 106 | 101 | } |
| @@ -14,22 +14,22 @@ | ||
| 14 | 14 | * See the License for the specific language governing permissions and |
| 15 | 15 | * limitations under the License. |
| 16 | 16 | */ |
| 17 | + | |
| 17 | 18 | package org.apache.commons.chain2; |
| 18 | 19 | |
| 19 | 20 | import java.util.Map; |
| 20 | 21 | |
| 21 | -import org.apache.commons.chain2.Processing; | |
| 22 | - | |
| 23 | 22 | /** |
| 24 | 23 | * Builder that allows continue adding a command in the target chain and execute it. |
| 25 | 24 | * |
| 25 | + * @version $Id$ | |
| 26 | 26 | * @param <K> Context key type |
| 27 | 27 | * @param <V> Context value type |
| 28 | 28 | * @param <C> Type of the context associated with this chain executor |
| 29 | - * @version $Id$ | |
| 30 | 29 | * @since 2.0 |
| 31 | 30 | */ |
| 32 | -public interface ChainExecutor<K, V, C extends Map<K, V>> extends CommandSetter<K, V, C, ChainExecutor<K, V, C>> { | |
| 31 | +public interface ChainExecutor<K, V, C extends Map<K, V>> | |
| 32 | + extends CommandSetter<K, V, C, ChainExecutor<K, V, C>> { | |
| 33 | 33 | |
| 34 | 34 | /** |
| 35 | 35 | * Execute the processing represented by the target chain. |
| @@ -39,7 +39,7 @@ | ||
| 39 | 39 | * has been completed. Returns {@link Processing#CONTINUE} if the processing |
| 40 | 40 | * of this context should be delegated to a subsequent command in an |
| 41 | 41 | * enclosing chain. |
| 42 | - * | |
| 42 | + * | |
| 43 | 43 | * @see Chain#execute(Map) |
| 44 | 44 | */ |
| 45 | 45 | Processing execute(C context); |
| @@ -29,11 +29,11 @@ | ||
| 29 | 29 | * The base implementation provides command lookup based on a single |
| 30 | 30 | * String. This String encodes both the catalog and command names. |
| 31 | 31 | * |
| 32 | + * @version $Id: CatalogFactory.java 1486528 2013-05-27 07:38:38Z simonetripodi $ | |
| 33 | + * | |
| 32 | 34 | * @param <K> the type of keys maintained by the context associated with this {@link Command} |
| 33 | 35 | * @param <V> the type of mapped values |
| 34 | 36 | * @param <C> Type of the context associated with this command |
| 35 | - * | |
| 36 | - * @version $Id: CatalogFactory.java 1486528 2013-05-27 07:38:38Z simonetripodi $ | |
| 37 | 37 | */ |
| 38 | 38 | public interface CatalogFactory<K, V, C extends Map<K, V>> { |
| 39 | 39 |
| @@ -14,32 +14,42 @@ | ||
| 14 | 14 | * See the License for the specific language governing permissions and |
| 15 | 15 | * limitations under the License. |
| 16 | 16 | */ |
| 17 | + | |
| 17 | 18 | package org.apache.commons.chain2; |
| 18 | 19 | |
| 19 | -import static java.lang.String.format; | |
| 20 | - | |
| 21 | 20 | import java.util.Map; |
| 22 | 21 | |
| 23 | 22 | /** |
| 24 | 23 | * Simple fluent chain EDSL to simplify {@link Chain} instances invocation. |
| 25 | 24 | * |
| 25 | + * @version $Id$ | |
| 26 | 26 | * @since 2.0 |
| 27 | - * @version $Id$ | |
| 28 | 27 | */ |
| 29 | 28 | public final class Chains { |
| 30 | 29 | |
| 31 | 30 | /** |
| 31 | + * Private constructor, this class cannot be instantiated directly. | |
| 32 | + */ | |
| 33 | + private Chains() { | |
| 34 | + throw new AssertionError(); | |
| 35 | + } | |
| 36 | + | |
| 37 | + /** | |
| 32 | 38 | * Defines the target chain has to be invoked. |
| 33 | 39 | * |
| 34 | 40 | * @param <K> Context key type |
| 35 | 41 | * @param <V> Context value type |
| 36 | 42 | * @param <C> Type of the context associated with this command |
| 37 | - * @param <CH> Type of the {@link Chain} to execute | |
| 43 | + * @param <U> Type of the {@link Chain} to execute | |
| 38 | 44 | * @param chain the chain instance reference to execute |
| 39 | 45 | * @return next chain builder |
| 40 | 46 | */ |
| 41 | - public static <K, V, C extends Map<K, V>, CH extends Chain<K, V, C>> ToExecutorCommandSetter<K, V, C> on(CH chain) { | |
| 42 | - return new DefaultCommandSetter<>(checkNotNullArgument(chain, "Null Chain can not be executed")); | |
| 47 | + public static <K, V, C extends Map<K, V>, U extends Chain<K, V, C>> | |
| 48 | + ToExecutorCommandSetter<K, V, C> on(final U chain) { | |
| 49 | + if (chain == null) { | |
| 50 | + throw new IllegalArgumentException(String.format("Null Chain can not be executed")); | |
| 51 | + } | |
| 52 | + return new DefaultCommandSetter<>(chain); | |
| 43 | 53 | } |
| 44 | 54 | |
| 45 | 55 | /** |
| @@ -48,53 +58,79 @@ | ||
| 48 | 58 | * @param <K> Context key type |
| 49 | 59 | * @param <V> Context value type |
| 50 | 60 | * @param <C> Type of the context associated with this command |
| 51 | - * @param <CA> Type of the {@link Catalog} to execute | |
| 61 | + * @param <S> Type of the {@link Catalog} to execute | |
| 52 | 62 | * @param catalog the catalog instance reference to be setup |
| 53 | 63 | * @return next chain builder |
| 54 | 64 | */ |
| 55 | - public static <K, V, C extends Map<K, V>, CA extends Catalog<K, V, C>> NamedCommandSetter<K, V, C> on(CA catalog) { | |
| 56 | - return new DefaultNamedCommandSetter<>(checkNotNullArgument(catalog, "Null Catalog can not be setup")); | |
| 65 | + public static <K, V, C extends Map<K, V>, S extends Catalog<K, V, C>> | |
| 66 | + NamedCommandSetter<K, V, C> on(final S catalog) { | |
| 67 | + if (catalog == null) { | |
| 68 | + throw new IllegalArgumentException(String.format("Null Catalog can not be setup")); | |
| 69 | + } | |
| 70 | + return new DefaultNamedCommandSetter<>(catalog); | |
| 57 | 71 | } |
| 58 | 72 | |
| 59 | - /** | |
| 60 | - * Private constructor, this class cannot be instantiated directly. | |
| 61 | - */ | |
| 62 | - private Chains() { | |
| 63 | - // do nothing | |
| 64 | - } | |
| 73 | + private static class DefaultCommandSetter<K, V, C extends Map<K, V>> | |
| 74 | + implements ToExecutorCommandSetter<K, V, C> { | |
| 65 | 75 | |
| 66 | - private static class DefaultCommandSetter<K, V, C extends Map<K, V>> implements ToExecutorCommandSetter<K, V, C> { | |
| 76 | + private final Chain<K, V, C> chain; | |
| 67 | 77 | |
| 68 | - private final Chain<K, V, C> chain; | |
| 69 | - /** @param chain Chain */ | |
| 70 | - public DefaultCommandSetter(Chain<K, V, C> chain) { | |
| 71 | - this.chain = chain; | |
| 78 | + /** | |
| 79 | + * @param chn Chain | |
| 80 | + */ | |
| 81 | + DefaultCommandSetter(final Chain<K, V, C> chn) { | |
| 82 | + this.chain = chn; | |
| 72 | 83 | } |
| 73 | - /** @see org.apache.commons.chain2.CommandSetter#add(org.apache.commons.chain2.Command) */ | |
| 74 | - @Override public <CMD extends Command<K, V, C>> ChainExecutor<K, V, C> add(CMD command) { | |
| 75 | - chain.addCommand(checkNotNullArgument(command, "Chain does not accept null Command instances")); | |
| 76 | - return new DefaultChainExecutor<>(chain); | |
| 84 | + | |
| 85 | + /** | |
| 86 | + * @see org.apache.commons.chain2.CommandSetter#add(org.apache.commons.chain2.Command) | |
| 87 | + */ | |
| 88 | + @Override | |
| 89 | + public <T extends Command<K, V, C>> ChainExecutor<K, V, C> add(final T command) { | |
| 90 | + if (command == null) { | |
| 91 | + throw new IllegalArgumentException( | |
| 92 | + String.format("Chain does not accept null Command instances")); | |
| 93 | + } | |
| 94 | + this.chain.addCommand(command); | |
| 95 | + return new DefaultChainExecutor<>(this.chain); | |
| 77 | 96 | } |
| 78 | - | |
| 79 | 97 | } |
| 80 | 98 | |
| 81 | - private static final class DefaultChainExecutor<K, V, C extends Map<K, V>> implements ChainExecutor<K, V, C> { | |
| 99 | + private static final class DefaultChainExecutor<K, V, C extends Map<K, V>> | |
| 100 | + implements ChainExecutor<K, V, C> { | |
| 82 | 101 | |
| 83 | 102 | private final Chain<K, V, C> chain; |
| 84 | - /** @param chain Chain */ | |
| 85 | - public DefaultChainExecutor(Chain<K, V, C> chain) { | |
| 86 | - this.chain = chain; | |
| 103 | + | |
| 104 | + /** | |
| 105 | + * @param chn Chain | |
| 106 | + */ | |
| 107 | + DefaultChainExecutor(final Chain<K, V, C> chn) { | |
| 108 | + this.chain = chn; | |
| 87 | 109 | } |
| 88 | - /** @see org.apache.commons.chain2.CommandSetter#add(org.apache.commons.chain2.Command) */ | |
| 89 | - @Override public <CMD extends Command<K, V, C>> ChainExecutor<K, V, C> add(CMD command) { | |
| 90 | - chain.addCommand(checkNotNullArgument(command, "Chain does not accept null Command instances")); | |
| 110 | + | |
| 111 | + /** | |
| 112 | + * @see org.apache.commons.chain2.CommandSetter#add(org.apache.commons.chain2.Command) | |
| 113 | + */ | |
| 114 | + @Override | |
| 115 | + public <T extends Command<K, V, C>> ChainExecutor<K, V, C> add(final T command) { | |
| 116 | + if (command == null) { | |
| 117 | + throw new IllegalArgumentException( | |
| 118 | + String.format("Chain does not accept null Command instances")); | |
| 119 | + } | |
| 120 | + this.chain.addCommand(command); | |
| 91 | 121 | return this; |
| 92 | 122 | } |
| 93 | - /** @see org.apache.commons.chain2.ChainExecutor#execute(java.util.Map) */ | |
| 94 | - @Override public Processing execute(C context) { | |
| 95 | - return chain.execute(checkNotNullArgument(context, "Chain cannot be applied to a null context.")); | |
| 123 | + | |
| 124 | + /** | |
| 125 | + * @see org.apache.commons.chain2.ChainExecutor#execute(java.util.Map) | |
| 126 | + */ | |
| 127 | + @Override public Processing execute(final C context) { | |
| 128 | + if (context == null) { | |
| 129 | + throw new IllegalArgumentException( | |
| 130 | + String.format("Chain cannot be applied to a null context.")); | |
| 131 | + } | |
| 132 | + return this.chain.execute(context); | |
| 96 | 133 | } |
| 97 | - | |
| 98 | 134 | } |
| 99 | 135 | |
| 100 | 136 | private static final class DefaultNamedCommandSetter<K, V, C extends Map<K, V>> |
| @@ -101,42 +137,52 @@ | ||
| 101 | 137 | implements NamedCommandSetter<K, V, C> { |
| 102 | 138 | |
| 103 | 139 | private final Catalog<K, V, C> catalog; |
| 104 | - /** @param catalog Catalog */ | |
| 105 | - public DefaultNamedCommandSetter(Catalog<K, V, C> catalog) { | |
| 106 | - this.catalog = catalog; | |
| 140 | + | |
| 141 | + /** | |
| 142 | + * @param ctlg Catalog | |
| 143 | + */ | |
| 144 | + DefaultNamedCommandSetter(final Catalog<K, V, C> ctlg) { | |
| 145 | + this.catalog = ctlg; | |
| 107 | 146 | } |
| 108 | - /** @see org.apache.commons.chain2.CommandSetter#add(org.apache.commons.chain2.Command) */ | |
| 109 | - @Override public <CMD extends Command<K, V, C>> NameSetter<K, V, C> add(CMD command) { | |
| 110 | - CMD checkedCommand = checkNotNullArgument( command, "Catalog does not accept null Command instances" ); | |
| 111 | - return new DefaultNameSetter<>(catalog, checkedCommand); | |
| 147 | + | |
| 148 | + /** | |
| 149 | + * @see org.apache.commons.chain2.CommandSetter#add(org.apache.commons.chain2.Command) | |
| 150 | + */ | |
| 151 | + @Override | |
| 152 | + public <T extends Command<K, V, C>> NameSetter<K, V, C> add(final T command) { | |
| 153 | + if (command == null) { | |
| 154 | + throw new IllegalArgumentException( | |
| 155 | + String.format("Catalog does not accept null Command instances")); | |
| 156 | + } | |
| 157 | + return new DefaultNameSetter<>(this.catalog, command); | |
| 112 | 158 | } |
| 113 | - | |
| 114 | 159 | } |
| 115 | 160 | |
| 116 | - private static final class DefaultNameSetter<K, V, C extends Map<K, V>> implements NameSetter<K, V, C> { | |
| 161 | + private static final class DefaultNameSetter<K, V, C extends Map<K, V>> | |
| 162 | + implements NameSetter<K, V, C> { | |
| 117 | 163 | private final Catalog<K, V, C> catalog; |
| 118 | 164 | private final Command<K, V, C> command; |
| 165 | + | |
| 119 | 166 | /** |
| 120 | - * @param catalog Catalog | |
| 121 | - * @param command Command */ | |
| 122 | - public DefaultNameSetter(Catalog<K, V, C> catalog, Command<K, V, C> command) { | |
| 123 | - this.catalog = catalog; | |
| 124 | - this.command = command; | |
| 167 | + * @param ctlg Catalog | |
| 168 | + * @param cmd Command */ | |
| 169 | + DefaultNameSetter(final Catalog<K, V, C> ctlg, final Command<K, V, C> cmd) { | |
| 170 | + this.catalog = ctlg; | |
| 171 | + this.command = cmd; | |
| 125 | 172 | } |
| 126 | - /** @see org.apache.commons.chain2.NameSetter#identifiedBy(java.lang.String) */ | |
| 127 | - @Override public NamedCommandSetter<K, V, C> identifiedBy(String name) { | |
| 128 | - catalog.addCommand(checkNotNullArgument(name, "Command <%s> cannot be identified by a null name", command), | |
| 129 | - command); | |
| 130 | - return new DefaultNamedCommandSetter<>(catalog); | |
| 131 | - } | |
| 132 | 173 | |
| 133 | - } | |
| 134 | - | |
| 135 | - protected static <T> T checkNotNullArgument(T reference, String message, Object...args) { | |
| 136 | - if (reference == null) { | |
| 137 | - throw new IllegalArgumentException(format(message, args)); | |
| 174 | + /** | |
| 175 | + * @see org.apache.commons.chain2.NameSetter#identifiedBy(java.lang.String) | |
| 176 | + */ | |
| 177 | + @Override | |
| 178 | + public NamedCommandSetter<K, V, C> identifiedBy(final String name) { | |
| 179 | + if (name == null) { | |
| 180 | + throw new IllegalArgumentException( | |
| 181 | + String.format("Command <%s> cannot be identified by a null name", this.command)); | |
| 182 | + } | |
| 183 | + this.catalog.addCommand(name, this.command); | |
| 184 | + return new DefaultNamedCommandSetter<>(this.catalog); | |
| 138 | 185 | } |
| 139 | - return reference; | |
| 140 | 186 | } |
| 141 | 187 | |
| 142 | 188 | } |
| @@ -14,6 +14,7 @@ | ||
| 14 | 14 | * See the License for the specific language governing permissions and |
| 15 | 15 | * limitations under the License. |
| 16 | 16 | */ |
| 17 | + | |
| 17 | 18 | package org.apache.commons.chain2; |
| 18 | 19 | |
| 19 | 20 | import java.util.Map; |
| @@ -21,12 +22,12 @@ | ||
| 21 | 22 | /** |
| 22 | 23 | * Generic builder that allows adding commands to the target {@link Chain} has to be executed. |
| 23 | 24 | * |
| 25 | + * @version $Id$ | |
| 24 | 26 | * @param <K> Context key type |
| 25 | 27 | * @param <V> Context value type |
| 26 | 28 | * @param <C> Type of the context associated with this command setter |
| 27 | 29 | * @param <R> Type of the next chain builder |
| 28 | 30 | * @since 2.0 |
| 29 | - * @version $Id$ | |
| 30 | 31 | */ |
| 31 | 32 | public interface CommandSetter<K, V, C extends Map<K, V>, R> { |
| 32 | 33 |
| @@ -33,11 +34,11 @@ | ||
| 33 | 34 | /** |
| 34 | 35 | * Add the given command to the target {@link Chain} has to be executed. |
| 35 | 36 | * |
| 36 | - * @param <CMD> Type of the command has to be added | |
| 37 | + * @param <T> Type of the command has to be added | |
| 37 | 38 | * @param command the command has to be added in the target chain |
| 38 | 39 | * @return next chain builder |
| 39 | 40 | * @see Chain#addCommand(Command) |
| 40 | 41 | */ |
| 41 | - <CMD extends Command<K, V, C>> R add(CMD command); | |
| 42 | + <T extends Command<K, V, C>> R add(T command); | |
| 42 | 43 | |
| 43 | 44 | } |