1 | // Copyright (C) 2007 Google Inc. |
2 | // |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | // you may not use this file except in compliance with the License. |
5 | // You may obtain a copy of the License at |
6 | // |
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | // |
9 | // 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 com.google.caja.util; |
16 | |
17 | import java.util.ArrayList; |
18 | import java.util.List; |
19 | |
20 | /** |
21 | * A pipeline takes an input and passes it through multiple stages, like a |
22 | * conveyor belt that passes a bucket of spare parts through a factory until |
23 | * the bucket reaches the end hopefully containing something resembling a |
24 | * bicycle. |
25 | * |
26 | * @author mikesamuel@gmail.com |
27 | */ |
28 | public class Pipeline<T> { |
29 | private final List<Stage<T>> stages = new ArrayList<Stage<T>>(); |
30 | |
31 | public final List<Stage<T>> getStages() { return stages; } |
32 | |
33 | public final boolean apply(T input) { |
34 | for (Stage<T> stage : stages) { |
35 | if (!applyStage(stage, input)) { return false; } |
36 | } |
37 | return true; |
38 | } |
39 | |
40 | protected boolean applyStage(Stage<? super T> stage, T input) { |
41 | return stage.apply(input); |
42 | } |
43 | |
44 | public interface Stage<S> { |
45 | /** |
46 | * Operates on an input and returns true iff processing should proceed to |
47 | * the next stage. |
48 | */ |
49 | boolean apply(S input); |
50 | } |
51 | } |