Author: simonetripodi Date: Tue Jul 24 12:57:56 2012 New Revision: 1365025 URL: http://svn.apache.org/viewvc?rev=1365025&view=rev Log: [CHAIN-70] Add a small EDSL to simplify Catalog setup
Added: commons/proper/chain/trunk/core/src/main/java/org/apache/commons/chain2/NameSetter.java (with props) commons/proper/chain/trunk/core/src/main/java/org/apache/commons/chain2/NamedCommandSetter.java (with props) Modified: commons/proper/chain/trunk/core/src/main/java/org/apache/commons/chain2/Chains.java commons/proper/chain/trunk/core/src/main/java/org/apache/commons/chain2/CommandSetter.java commons/proper/chain/trunk/core/src/test/java/org/apache/commons/chain2/impl/FluentInterfacesTestCase.java commons/proper/chain/trunk/src/changes/changes.xml Modified: commons/proper/chain/trunk/core/src/main/java/org/apache/commons/chain2/Chains.java URL: http://svn.apache.org/viewvc/commons/proper/chain/trunk/core/src/main/java/org/apache/commons/chain2/Chains.java?rev=1365025&r1=1365024&r2=1365025&view=diff ============================================================================== --- commons/proper/chain/trunk/core/src/main/java/org/apache/commons/chain2/Chains.java (original) +++ commons/proper/chain/trunk/core/src/main/java/org/apache/commons/chain2/Chains.java Tue Jul 24 12:57:56 2012 @@ -16,6 +16,8 @@ */ package org.apache.commons.chain2; +import static java.lang.String.format; + import java.util.Map; /** @@ -41,6 +43,20 @@ public final class Chains { } /** + * Defines the target {@link Catalog} has to be invoked. + * + * @param <K> Context key type + * @param <V> Context value type + * @param <C> Type of the context associated with this command + * @param <CH> Type of the {@link Chain} to execute + * @param catalog the catalog instance reference to be setup + * @return next chain builder + */ + public static <K, V, C extends Map<K, V>, CA extends Catalog<K, V, C>> NamedCommandSetter<K, V, C> on(CA catalog) { + return new DefaultNamedCommandSetter<K, V, C>(checkNotNullArgument(catalog, "Null Catalog can not be setup")); + } + + /** * Private constructor, this class cannot be instantiated directly. */ private Chains() { @@ -81,9 +97,44 @@ public final class Chains { } - private static <T> T checkNotNullArgument(T reference, String message) { + private static final class DefaultNamedCommandSetter<K, V, C extends Map<K, V>> + implements NamedCommandSetter<K, V, C> { + + private final Catalog<K, V, C> catalog; + + public DefaultNamedCommandSetter(Catalog<K, V, C> catalog) { + this.catalog = catalog; + } + + public <CMD extends Command<K, V, C>> NameSetter<K, V, C> addCommand(CMD command) { + CMD checkedCommand = checkNotNullArgument( command, "Catalog does not accept null Command instances" ); + return new DefaultNameSetter<K, V, C>(catalog, checkedCommand); + } + + } + + private static final class DefaultNameSetter<K, V, C extends Map<K, V>> implements NameSetter<K, V, C> { + + private final Catalog<K, V, C> catalog; + + private final Command<K, V, C> command; + + public DefaultNameSetter(Catalog<K, V, C> catalog, Command<K, V, C> command) { + this.catalog = catalog; + this.command = command; + } + + public NamedCommandSetter<K, V, C> identifiedBy(String name) { + catalog.addCommand(checkNotNullArgument(name, "Command <%s> cannot be identified by a null name", command), + command); + return new DefaultNamedCommandSetter<K, V, C>(catalog); + } + + } + + private static <T> T checkNotNullArgument(T reference, String message, Object...args) { if (reference == null) { - throw new IllegalArgumentException(message); + throw new IllegalArgumentException(format(message, args)); } return reference; } Modified: commons/proper/chain/trunk/core/src/main/java/org/apache/commons/chain2/CommandSetter.java URL: http://svn.apache.org/viewvc/commons/proper/chain/trunk/core/src/main/java/org/apache/commons/chain2/CommandSetter.java?rev=1365025&r1=1365024&r2=1365025&view=diff ============================================================================== --- commons/proper/chain/trunk/core/src/main/java/org/apache/commons/chain2/CommandSetter.java (original) +++ commons/proper/chain/trunk/core/src/main/java/org/apache/commons/chain2/CommandSetter.java Tue Jul 24 12:57:56 2012 @@ -24,11 +24,11 @@ import java.util.Map; * @param <K> Context key type * @param <V> Context value type * @param <C> Type of the context associated with this command setter - * @param <CS> Type of the next chain builder + * @param <R> Type of the next chain builder * @since 2.0 * @version $Id$ */ -public interface CommandSetter<K, V, C extends Map<K, V>, CS extends CommandSetter<K, V, C, CS>> { +public interface CommandSetter<K, V, C extends Map<K, V>, R> { /** * Add the given command to the target {@link Chain} has to be executed. @@ -38,6 +38,6 @@ public interface CommandSetter<K, V, C e * @return next chain builder * @see Chain#addCommand(Command) */ - <CMD extends Command<K, V, C>> CS addCommand(CMD command); + <CMD extends Command<K, V, C>> R addCommand(CMD command); } Added: commons/proper/chain/trunk/core/src/main/java/org/apache/commons/chain2/NameSetter.java URL: http://svn.apache.org/viewvc/commons/proper/chain/trunk/core/src/main/java/org/apache/commons/chain2/NameSetter.java?rev=1365025&view=auto ============================================================================== --- commons/proper/chain/trunk/core/src/main/java/org/apache/commons/chain2/NameSetter.java (added) +++ commons/proper/chain/trunk/core/src/main/java/org/apache/commons/chain2/NameSetter.java Tue Jul 24 12:57:56 2012 @@ -0,0 +1,40 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.chain2; + +import java.util.Map; + +/** + * Allows specifying a name for a {@link Command} in a {@link Catalog} instance. + * + * @param <K> Context key type + * @param <V> Context value type + * @param <C> Type of the context associated with this name setter + * @since 2.0 + * @version $Id$ + */ +public interface NameSetter<K, V, C extends Map<K, V>> { + + /** + * Specifies a name for a {@link Command} in a {@link Catalog} instance. + * + * @param name the name of the previous set {@link Command} + * @return a new builder to add a new {@link Command} + */ + NamedCommandSetter<K, V, C> identifiedBy(String name); + +} Propchange: commons/proper/chain/trunk/core/src/main/java/org/apache/commons/chain2/NameSetter.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: commons/proper/chain/trunk/core/src/main/java/org/apache/commons/chain2/NameSetter.java ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision HeadURL Propchange: commons/proper/chain/trunk/core/src/main/java/org/apache/commons/chain2/NameSetter.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: commons/proper/chain/trunk/core/src/main/java/org/apache/commons/chain2/NamedCommandSetter.java URL: http://svn.apache.org/viewvc/commons/proper/chain/trunk/core/src/main/java/org/apache/commons/chain2/NamedCommandSetter.java?rev=1365025&view=auto ============================================================================== --- commons/proper/chain/trunk/core/src/main/java/org/apache/commons/chain2/NamedCommandSetter.java (added) +++ commons/proper/chain/trunk/core/src/main/java/org/apache/commons/chain2/NamedCommandSetter.java Tue Jul 24 12:57:56 2012 @@ -0,0 +1,33 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.chain2; + +import java.util.Map; + +/** + * Allows adding a command in a {@link Catalog} identified by a name. + * + * @param <K> Context key type + * @param <V> Context value type + * @param <C> Type of the context associated with this command setter + * @since 2.0 + * @version $Id$ + */ +public interface NamedCommandSetter<K, V, C extends Map<K, V>> + extends CommandSetter<K, V, C, NameSetter<K, V, C>> { + +} Propchange: commons/proper/chain/trunk/core/src/main/java/org/apache/commons/chain2/NamedCommandSetter.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: commons/proper/chain/trunk/core/src/main/java/org/apache/commons/chain2/NamedCommandSetter.java ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision HeadURL Propchange: commons/proper/chain/trunk/core/src/main/java/org/apache/commons/chain2/NamedCommandSetter.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Modified: commons/proper/chain/trunk/core/src/test/java/org/apache/commons/chain2/impl/FluentInterfacesTestCase.java URL: http://svn.apache.org/viewvc/commons/proper/chain/trunk/core/src/test/java/org/apache/commons/chain2/impl/FluentInterfacesTestCase.java?rev=1365025&r1=1365024&r2=1365025&view=diff ============================================================================== --- commons/proper/chain/trunk/core/src/test/java/org/apache/commons/chain2/impl/FluentInterfacesTestCase.java (original) +++ commons/proper/chain/trunk/core/src/test/java/org/apache/commons/chain2/impl/FluentInterfacesTestCase.java Tue Jul 24 12:57:56 2012 @@ -16,14 +16,15 @@ */ package org.apache.commons.chain2.impl; -import static org.junit.Assert.assertTrue; import static org.apache.commons.chain2.Chains.on; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import org.apache.commons.chain2.Catalog; +import org.apache.commons.chain2.Chain; import org.apache.commons.chain2.Context; import org.junit.Test; -import org.apache.commons.chain2.Chain; - public final class FluentInterfacesTestCase { @Test(expected = IllegalArgumentException.class) @@ -32,6 +33,11 @@ public final class FluentInterfacesTestC } @Test(expected = IllegalArgumentException.class) + public void doesNotAcceptNullCatalog() { + on((Catalog<String, Object, Context<String, Object>>) null); + } + + @Test(expected = IllegalArgumentException.class) public void doesNotAcceptNullCommand() { on(new ChainBase<String, Object, Context<String, Object>>()) .addCommand(null); @@ -44,6 +50,18 @@ public final class FluentInterfacesTestC .execute(null); } + @Test(expected = IllegalArgumentException.class) + public void doesNotAcceptNullCommandInCatalog() { + on(new CatalogBase<String, Object, Context<String, Object>>()) + .addCommand(null); + } + + @Test(expected = IllegalArgumentException.class) + public void doesNotAcceptNullName() { + on(new CatalogBase<String, Object, Context<String, Object>>()) + .addCommand(new DelegatingFilter("1", "a")).identifiedBy(null); + } + @Test public void justMakeSureChainIsExecuted() { ContextBase context = new ContextBase(); @@ -57,4 +75,29 @@ public final class FluentInterfacesTestC assertTrue(context.containsKey("log")); } + @Test + public void justMakeSureCatalogIsSetup() { + CatalogBase<String, Object, Context<String, Object>> catalog = + new CatalogBase<String, Object, Context<String, Object>>(); + + on(catalog) + .addCommand(new AddingCommand("", null)).identifiedBy("AddingCommand") + .addCommand(new DelegatingCommand("")).identifiedBy("DelegatingCommand") + .addCommand(new DelegatingFilter("", "")).identifiedBy("DelegatingFilter") + .addCommand(new ExceptionCommand("")).identifiedBy("ExceptionCommand") + .addCommand(new ExceptionFilter("", "")).identifiedBy("ExceptionFilter") + .addCommand(new NonDelegatingCommand("")).identifiedBy("NonDelegatingCommand") + .addCommand(new NonDelegatingFilter("", "")).identifiedBy("NonDelegatingFilter") + .addCommand(new ChainBase<String, Object, Context<String, Object>>()).identifiedBy("ChainBase"); + + assertNotNull(catalog.getCommand("AddingCommand")); + assertNotNull(catalog.getCommand("DelegatingCommand")); + assertNotNull(catalog.getCommand("DelegatingFilter")); + assertNotNull(catalog.getCommand("ExceptionCommand")); + assertNotNull(catalog.getCommand("ExceptionFilter")); + assertNotNull(catalog.getCommand("NonDelegatingCommand")); + assertNotNull(catalog.getCommand("NonDelegatingFilter")); + assertNotNull(catalog.getCommand("ChainBase")); + } + } Modified: commons/proper/chain/trunk/src/changes/changes.xml URL: http://svn.apache.org/viewvc/commons/proper/chain/trunk/src/changes/changes.xml?rev=1365025&r1=1365024&r2=1365025&view=diff ============================================================================== --- commons/proper/chain/trunk/src/changes/changes.xml (original) +++ commons/proper/chain/trunk/src/changes/changes.xml Tue Jul 24 12:57:56 2012 @@ -41,6 +41,9 @@ The <action> type attribute can be add,u <body> <release version="2.0" description="Major release"> + <action dev="simonetripodi" type="add" issue="CHAIN-73"> + Add a small EDSL to simplify Catalog setup. + </action> <action dev="elijah" type="update" issue="CHAIN-71"> Removed methods deprecated in 1.xx releases. </action>