01: // Copyright 2006, 2007 The Apache Software Foundation
02: //
03: // Licensed under the Apache License, Version 2.0 (the "License");
04: // you may not use this file except in compliance with the License.
05: // You may obtain a copy of the License at
06: //
07: // http://www.apache.org/licenses/LICENSE-2.0
08: //
09: // Unless required by applicable law or agreed to in writing, software
10: // distributed under the License is distributed on an "AS IS" BASIS,
11: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12: // See the License for the specific language governing permissions and
13: // limitations under the License.
14:
15: package org.apache.tapestry.ioc.internal.services;
16:
17: import org.apache.tapestry.ioc.services.Coercion;
18:
19: /**
20: * Combines two coercions to create a coercsion through an intermediate type.
21: *
22: * @param <S>
23: * The source (input) type
24: * @param <I>
25: * The intermediate type
26: * @param <T>
27: * The target (output) type
28: */
29: public class CompoundCoercion<S, I, T> implements Coercion<S, T> {
30: private final Coercion<S, I> _op1;
31:
32: private final Coercion<I, T> _op2;
33:
34: public CompoundCoercion(Coercion<S, I> op1, Coercion<I, T> op2) {
35: _op1 = op1;
36: _op2 = op2;
37: }
38:
39: public T coerce(S input) {
40: // Run the input through the first operation (S --> I), then run the result of that through
41: // the second operation (I --> T).
42:
43: I intermediate = _op1.coerce(input);
44:
45: return _op2.coerce(intermediate);
46: }
47:
48: @Override
49: public String toString() {
50: return String.format("%s, %s", _op1, _op2);
51: }
52: }
|