001/**
002 *
003 * Copyright 2003-2007 Jive Software.
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 org.jivesoftware.smack.packet.Stanza;
021import org.jivesoftware.smack.util.Predicate;
022
023/**
024 * Defines a way to filter stanzas for particular attributes. Stanza filters are used when
025 * constructing stanza listeners or collectors -- the filter defines what stanzas match the criteria
026 * of the collector or listener for further stanza processing.
027 * <p>
028 * Several simple filters are pre-defined. These filters can be logically combined for more complex
029 * stanza filtering by using the {@link org.jivesoftware.smack.filter.AndFilter AndFilter} and
030 * {@link org.jivesoftware.smack.filter.OrFilter OrFilter} filters. It's also possible to define
031 * your own filters by implementing this interface. The code example below creates a trivial filter
032 * for stanzas with a specific ID (real code should use {@link StanzaIdFilter} instead).
033 *
034 * <pre>
035 * // Use an anonymous inner class to define a stanza filter that returns
036 * // all stanzas that have a stanza ID of &quot;RS145&quot;.
037 * StanzaFilter myFilter = new StanzaFilter() {
038 *     public boolean accept(Stanza stanza) {
039 *         return &quot;RS145&quot;.equals(stanza.getStanzaId());
040 *     }
041 * };
042 * // Create a new stanza collector using the filter we created.
043 * StanzaCollector myCollector = connection.createStanzaCollector(myFilter);
044 * </pre>
045 * <p>
046 * As a rule of thumb: If you have a predicate method, that is, a method which takes a single Stanza as argument, is pure
047 * (side effect free) and returns only a boolean, then it is a good indicator that the logic should be put into a
048 * {@link StanzaFilter} (and be referenced in {@link org.jivesoftware.smack.StanzaListener}).
049 * </p>
050 *
051 * @see org.jivesoftware.smack.StanzaCollector
052 * @see org.jivesoftware.smack.StanzaListener
053 * @author Matt Tucker
054 */
055public interface StanzaFilter extends Predicate<Stanza> {
056
057    /**
058     * Tests whether or not the specified stanza should pass the filter.
059     *
060     * @param stanza the stanza to test.
061     * @return true if and only if <code>stanza</code> passes the filter.
062     */
063    boolean accept(Stanza stanza);
064
065    @Override
066    default boolean test(Stanza stanza) {
067        return accept(stanza);
068    }
069
070    default <S extends Stanza> Predicate<S> asPredicate(Class<?> stanzaClass) {
071        return s -> {
072            if (!stanzaClass.isAssignableFrom(s.getClass())) {
073                return false;
074            }
075            return accept(s);
076        };
077    }
078}