1 | // Copyright 2004-2007 Jean-Francois Poilpret |
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 net.sourceforge.hiveevents; |
16 | |
17 | import java.util.ArrayList; |
18 | import java.util.List; |
19 | |
20 | /** |
21 | * Chain of event filters. Allows to daisy chain several filters (makes it |
22 | * easier to build complex filters). |
23 | * |
24 | * @author Jean-Francois Poilpret |
25 | */ |
26 | public class FiltersChain<T> implements Filter<T> |
27 | { |
28 | /** |
29 | * Create a chain of filters. |
30 | */ |
31 | static public<T> Filter<T> create(Filter<T>... filters) |
32 | { |
33 | FiltersChain<T> chain = new FiltersChain<T>(); |
34 | for (Filter<T> filter: filters) |
35 | { |
36 | chain.addFilter(filter); |
37 | } |
38 | return chain; |
39 | } |
40 | |
41 | /** |
42 | * Add a filter at the end of the chain. |
43 | */ |
44 | public void addFilter(Filter<T> filter) |
45 | { |
46 | _filters.add(filter); |
47 | } |
48 | |
49 | /** |
50 | * Checks if an event passes through ALL the filters in the chain. |
51 | * If one only filter does not allow the event to pass, then the event will |
52 | * NOT pass. |
53 | */ |
54 | public boolean passEvent(T event) |
55 | { |
56 | for (Filter<T> filter: _filters) |
57 | { |
58 | if (!filter.passEvent(event)) |
59 | { |
60 | return false; |
61 | } |
62 | } |
63 | return true; |
64 | } |
65 | |
66 | private final List<Filter<T>> _filters = new ArrayList<Filter<T>>(); |
67 | } |