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