001    // Copyright 2006, 2007 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.internal.services;
016    
017    import org.apache.tapestry5.ioc.services.Coercion;
018    
019    /**
020     * Combines two coercions to create a coercsion through an intermediate type.
021     *
022     * @param <S> The source (input) type
023     * @param <I> The intermediate type
024     * @param <T> The target (output) type
025     */
026    public class CompoundCoercion<S, I, T> implements Coercion<S, T>
027    {
028        private final Coercion<S, I> op1;
029    
030        private final Coercion<I, T> op2;
031    
032        public CompoundCoercion(Coercion<S, I> op1, Coercion<I, T> op2)
033        {
034            this.op1 = op1;
035            this.op2 = op2;
036        }
037    
038        public T coerce(S input)
039        {
040            // Run the input through the first operation (S --> I), then run the result of that through
041            // the second operation (I --> T).
042    
043            I intermediate = op1.coerce(input);
044    
045            return op2.coerce(intermediate);
046        }
047    
048        @Override
049        public String toString()
050        {
051            return String.format("%s, %s", op1, op2);
052        }
053    }