1 /**
2  * Logback: the reliable, generic, fast and flexible logging framework.
3  * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4  *
5  * This program and the accompanying materials are dual-licensed under
6  * either the terms of the Eclipse Public License v1.0 as published by
7  * the Eclipse Foundation
8  *
9  *   or (per the licensee's choosing)
10  *
11  * under the terms of the GNU Lesser General Public License version 2.1
12  * as published by the Free Software Foundation.
13  */

14 package ch.qos.logback.core.spi;
15
16 import java.util.ArrayList;
17 import java.util.List;
18
19 import ch.qos.logback.core.filter.Filter;
20 import ch.qos.logback.core.util.COWArrayList;
21
22 /**
23  * Implementation of FilterAttachable.
24  * 
25  * @author Ceki Gülcü
26  */

27 final public class FilterAttachableImpl<E> implements FilterAttachable<E> {
28
29     @SuppressWarnings("unchecked")
30     COWArrayList<Filter<E>> filterList = new COWArrayList<Filter<E>>(new Filter[0]);
31
32     /**
33      * Add a filter to end of the filter list.
34      */

35     public void addFilter(Filter<E> newFilter) {
36         filterList.add(newFilter);
37     }
38
39     /**
40      * Clear the filter chain
41      */

42     public void clearAllFilters() {
43         filterList.clear();
44     }
45
46     /**
47      * Loop through the filters in the list. As soon as a filter decides on
48      * ACCEPT or DENY, then that value is returned. If all of the filters return
49      * NEUTRAL, then NEUTRAL is returned.
50      */

51     public FilterReply getFilterChainDecision(E event) {
52
53         final Filter<E>[] filterArrray = filterList.asTypedArray();
54         final int len = filterArrray.length;
55
56         for (int i = 0; i < len; i++) {
57             final FilterReply r = filterArrray[i].decide(event);
58             if (r == FilterReply.DENY || r == FilterReply.ACCEPT) {
59                 return r;
60             }
61         }
62
63         // no decision
64         return FilterReply.NEUTRAL;
65     }
66
67     public List<Filter<E>> getCopyOfAttachedFiltersList() {
68         return new ArrayList<Filter<E>>(filterList);
69     }
70 }
71