001/**
002 *
003 * Copyright 2015 Florian Schmaus
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.jivesoftware.smack.filter;
019
020import java.util.ArrayList;
021import java.util.Arrays;
022import java.util.Iterator;
023import java.util.List;
024
025import org.jivesoftware.smack.util.Objects;
026
027/**
028 * 
029 */
030public abstract class AbstractListFilter implements StanzaFilter {
031
032    /**
033     * The list of filters.
034     */
035    protected final List<StanzaFilter> filters;
036
037    /**
038     * Creates an empty filter.
039     */
040    protected AbstractListFilter() {
041        filters = new ArrayList<StanzaFilter>();
042    }
043
044    /**
045     * Creates an filter using the specified filters.
046     *
047     * @param filters the filters to add.
048     */
049    protected AbstractListFilter(StanzaFilter... filters) {
050        Objects.requireNonNull(filters, "Parameter must not be null.");
051        for(StanzaFilter filter : filters) {
052            Objects.requireNonNull(filter, "Parameter must not be null.");
053        }
054        this.filters = new ArrayList<StanzaFilter>(Arrays.asList(filters));
055    }
056
057    /**
058     * Adds a filter to the filter list. A stanza will pass the filter if all of the filters in the
059     * list accept it.
060     *
061     * @param filter a filter to add to the filter list.
062     */
063    public void addFilter(StanzaFilter filter) {
064        Objects.requireNonNull(filter, "Parameter must not be null.");
065        filters.add(filter);
066    }
067
068    @Override
069    public final String toString() {
070        StringBuilder sb = new StringBuilder();
071        sb.append(getClass().getSimpleName());
072        sb.append(": (");
073        for (Iterator<StanzaFilter> it = filters.iterator(); it.hasNext();) {
074            StanzaFilter filter = it.next();
075            sb.append(filter.toString());
076            if (it.hasNext()) {
077                sb.append(", ");
078            }
079        }
080        sb.append(")");
081        return sb.toString();
082    }
083}