1 | /* |
2 | * $Id: AbstractStepFunction.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.util.*; |
37 | |
38 | |
39 | /** |
40 | * Provides some default behavior for parts of the |
41 | * StepFunction interface. This is primarily useful for |
42 | * StepFunction implementations that need to implement their own |
43 | * custom Iterator. Subclasses need only implement the stepIterator() |
44 | * method to return an appropriate Iterator. This base class provides an |
45 | * Iterable<> wrapper that delegates to it. |
46 | * |
47 | * @version $Revision: 562 $ |
48 | * @author Paul Speed |
49 | */ |
50 | public abstract class AbstractStepFunction<S,T> implements StepFunction<S,T> |
51 | { |
52 | /** |
53 | * Returns an implementation specific Iterable for the |
54 | * supplied starting object. |
55 | */ |
56 | public Iterable<T> apply( S start ) |
57 | { |
58 | return new StepIterable(start); |
59 | } |
60 | |
61 | /** |
62 | * Implemented by subclasses to provide the actual |
63 | * iterator. |
64 | */ |
65 | protected abstract Iterator<T> stepIterator( S start ); |
66 | |
67 | /** |
68 | * Internal implementation of Iterable that delegates |
69 | * to the abstract stepIterator() method. |
70 | */ |
71 | protected class StepIterable implements Iterable<T> |
72 | { |
73 | private S start; |
74 | |
75 | /** |
76 | * Constructs an iterable that simply delegates |
77 | * to the stepIterator() method. |
78 | */ |
79 | public StepIterable( S start ) |
80 | { |
81 | this.start = start; |
82 | } |
83 | |
84 | /** |
85 | * {@inheritDoc} |
86 | */ |
87 | public Iterator<T> iterator() |
88 | { |
89 | return stepIterator(start); |
90 | } |
91 | } |
92 | } |
93 | |