001 // Copyright 2006 The Apache Software Foundation
002 //
003 // Licensed under the Apache License, Version 2.0 (the "License");
004 // you may not use this file except in compliance with the License.
005 // You may obtain a copy of the License at
006 //
007 // http://www.apache.org/licenses/LICENSE-2.0
008 //
009 // Unless required by applicable law or agreed to in writing, software
010 // distributed under the License is distributed on an "AS IS" BASIS,
011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012 // See the License for the specific language governing permissions and
013 // limitations under the License.
014
015 package org.apache.tapestry5.ioc.services;
016
017 import java.util.List;
018
019 /**
020 * A service which can assemble an implementation based on a command interface, and an ordered list of objects
021 * implementing that interface (the "commands"). This is an implementation of the Gang of Four Chain Of Command
022 * pattern.
023 * <p/>
024 * For each method in the interface, the chain implementation will call the corresponding method on each command object
025 * in turn (with the order defined by the list). If any of the command objects return true, then the chain of command
026 * stops and the initial method invocation returns true. Otherwise, the chain of command continues to the next command
027 * (and will return false if none of the commands returns true).
028 * <p/>
029 * For methods whose return type is not boolean, the chain stops with the first non-null (for object types), or non-zero
030 * (for numeric types). The chain returns the value that was returned by the command. If the method return type is void,
031 * all commands will be invoked.
032 * <p/>
033 * Method invocations will also be terminated if an exception is thrown.
034 */
035 public interface ChainBuilder
036 {
037 /**
038 * Creates a chain instance from a command interface and a list of commands (implementing the interface).
039 */
040 <T> T build(Class<T> commandInterface, List<T> commands);
041 }