001/**
002 *
003 * Copyright 2003-2007 Jive Software, 2021 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.List;
021
022import org.jivesoftware.smack.packet.Stanza;
023
024/**
025 * Implements the logical AND operation over two or more stanza filters.
026 * In other words, packets pass this filter if they pass <b>all</b> of the filters.
027 *
028 * @author Matt Tucker
029 */
030public class AndFilter extends AbstractListFilter implements StanzaFilter {
031
032    /**
033     * Creates an empty AND filter. Filters should be added using the
034     * {@link #addFilter(StanzaFilter)} method.
035     */
036    public AndFilter() {
037        super();
038    }
039
040    /**
041     * Creates an AND filter using the specified filters.
042     *
043     * @param filters the filters to add.
044     */
045    public AndFilter(StanzaFilter... filters) {
046        super(filters);
047    }
048
049    /**
050     * Creates an AND filter using the specified filters.
051     *
052     * @param filters the filters to add.
053     */
054    public AndFilter(List<StanzaFilter> filters) {
055        super(filters);
056    }
057
058    @Override
059    public boolean accept(Stanza packet) {
060        for (StanzaFilter filter : filters) {
061            if (!filter.accept(packet)) {
062                return false;
063            }
064        }
065        return true;
066    }
067
068}