001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.core.spi;
015
016import java.util.ArrayList;
017import java.util.List;
018
019import ch.qos.logback.core.filter.Filter;
020import ch.qos.logback.core.util.COWArrayList;
021
022/**
023 * Implementation of FilterAttachable.
024 * 
025 * @author Ceki Gülcü
026 */
027final public class FilterAttachableImpl<E> implements FilterAttachable<E> {
028
029    @SuppressWarnings("unchecked")
030    COWArrayList<Filter<E>> filterList = new COWArrayList<Filter<E>>(new Filter[0]);
031
032    /**
033     * Add a filter to end of the filter list.
034     */
035    public void addFilter(Filter<E> newFilter) {
036        filterList.add(newFilter);
037    }
038
039    /**
040     * Clear the filter chain
041     */
042    public void clearAllFilters() {
043        filterList.clear();
044    }
045
046    /**
047     * Loop through the filters in the list. As soon as a filter decides on ACCEPT
048     * or DENY, then that value is returned. If all of the filters return NEUTRAL,
049     * then NEUTRAL is returned.
050     */
051    public FilterReply getFilterChainDecision(E event) {
052
053        final Filter<E>[] filterArrray = filterList.asTypedArray();
054        final int len = filterArrray.length;
055
056        for (int i = 0; i < len; i++) {
057            final FilterReply r = filterArrray[i].decide(event);
058            if (r == FilterReply.DENY || r == FilterReply.ACCEPT) {
059                return r;
060            }
061        }
062
063        // no decision
064        return FilterReply.NEUTRAL;
065    }
066
067    public List<Filter<E>> getCopyOfAttachedFiltersList() {
068        return new ArrayList<Filter<E>>(filterList);
069    }
070}