1 | /* |
2 | * $Id: ChainFunction.java 562 2010-11-11 00:28:13Z PSpeed $ |
3 | * |
4 | * The Filament BSD license. |
5 | * |
6 | * Copyright (c) 2009-2010, the original author or authors |
7 | * |
8 | * Redistribution and use in source and binary forms, with or without |
9 | * modification, are permitted provided that the following conditions |
10 | * are met: |
11 | * |
12 | * 1) Redistributions of source code must retain the above copyright notice, |
13 | * this list of conditions and the following disclaimer. |
14 | * 2) Redistributions in binary form must reproduce the above copyright |
15 | * notice, this list of conditions and the following disclaimer in the |
16 | * documentation and/or other materials provided with the distribution. |
17 | * 3) Neither the names "Filament", "fgraph.org", "filamentgraph.org", nor the |
18 | * names of its contributors may be used to endorse or promote products |
19 | * derived from this software without specific prior written permission. |
20 | * |
21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
22 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
25 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
26 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
27 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
28 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
29 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
30 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
31 | * POSSIBILITY OF SUCH DAMAGE. |
32 | */ |
33 | |
34 | package org.fgraph.steps; |
35 | |
36 | import java.io.Serializable; |
37 | |
38 | import com.google.common.base.Function; |
39 | import com.google.common.collect.Iterables; |
40 | |
41 | |
42 | /** |
43 | * Chains the output of one StepFunction to the input |
44 | * of another. |
45 | * |
46 | * @version $Revision: 562 $ |
47 | * @author Paul Speed |
48 | */ |
49 | public class ChainFunction<A, B, C> implements StepFunction<A,C>, Serializable |
50 | { |
51 | static final long serialVersionUID = 1L; |
52 | |
53 | // Note for future explorers: |
54 | // I'm not sure that <A,? extends B> -> <B,C> |
55 | // is materially different than <A,B> -> <? super B,C> |
56 | // I can't think of use-cases right now that illustrate |
57 | // which one might be better so I'm opting for the |
58 | // guava/GCL convention. Given that this is normally |
59 | // resolved by the factory method, I'm not sure it matters |
60 | // either... since B can be anything and is never exposed |
61 | // to the 'user' of the returned Function. |
62 | private Function<A,? extends Iterable<? extends B>> f1; |
63 | private Function<B,? extends Iterable<C>> f2; |
64 | |
65 | /** |
66 | * Creates a function that feeds the results of the first |
67 | * function into the second function and provides a single concatenated |
68 | * iterable over those results. |
69 | */ |
70 | public ChainFunction( Function<A,? extends Iterable<? extends B>> f1, |
71 | Function<B,? extends Iterable<C>> f2 ) |
72 | { |
73 | if( f1 == null || f2 == null ) |
74 | throw new IllegalArgumentException( "Supplied functions cannot be null." ); |
75 | |
76 | this.f1 = f1; |
77 | this.f2 = f2; |
78 | } |
79 | |
80 | /** |
81 | * Returns an iterable that chains the two chained functions |
82 | * together. It does this by getting the result Iterable<B> by |
83 | * applying function 1 to the supplied start. Each of the |
84 | * results in this iterator is fed through function two and |
85 | * all of the Iterable<C>s are concatenated together. This |
86 | * is all done lazily on-demand. |
87 | */ |
88 | public Iterable<C> apply( A start ) |
89 | { |
90 | Iterable<? extends B> result1 = f1.apply(start); |
91 | |
92 | // A powerful set of commands. |
93 | // It runs f2 on every result of f1 which produces an iterable |
94 | // of iterables. Then concat mashes them back together again. |
95 | // ...and all of this is evaluated lazily. |
96 | return Iterables.concat( Iterables.transform( result1, f2 ) ); |
97 | } |
98 | |
99 | /** |
100 | * {@inheritDoc} |
101 | */ |
102 | public String toString() |
103 | { |
104 | return "ChainFunction[" + f1 + " -> " + f2 + "]"; |
105 | } |
106 | } |