StanzaFilter.java

  1. /**
  2.  *
  3.  * Copyright 2003-2007 Jive Software.
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.jivesoftware.smack.filter;

  18. import org.jivesoftware.smack.packet.Stanza;
  19. import org.jivesoftware.smack.util.Predicate;

  20. /**
  21.  * Defines a way to filter stanzas for particular attributes. Stanza filters are used when
  22.  * constructing stanza listeners or collectors -- the filter defines what stanzas match the criteria
  23.  * of the collector or listener for further stanza processing.
  24.  * <p>
  25.  * Several simple filters are pre-defined. These filters can be logically combined for more complex
  26.  * stanza filtering by using the {@link org.jivesoftware.smack.filter.AndFilter AndFilter} and
  27.  * {@link org.jivesoftware.smack.filter.OrFilter OrFilter} filters. It's also possible to define
  28.  * your own filters by implementing this interface. The code example below creates a trivial filter
  29.  * for stanzas with a specific ID (real code should use {@link StanzaIdFilter} instead).
  30.  *
  31.  * <pre>
  32.  * // Use an anonymous inner class to define a stanza filter that returns
  33.  * // all stanzas that have a stanza ID of &quot;RS145&quot;.
  34.  * StanzaFilter myFilter = new StanzaFilter() {
  35.  *     public boolean accept(Stanza stanza) {
  36.  *         return &quot;RS145&quot;.equals(stanza.getStanzaId());
  37.  *     }
  38.  * };
  39.  * // Create a new stanza collector using the filter we created.
  40.  * StanzaCollector myCollector = connection.createStanzaCollector(myFilter);
  41.  * </pre>
  42.  * <p>
  43.  * As a rule of thumb: If you have a predicate method, that is, a method which takes a single Stanza as argument, is pure
  44.  * (side effect free) and returns only a boolean, then it is a good indicator that the logic should be put into a
  45.  * {@link StanzaFilter} (and be referenced in {@link org.jivesoftware.smack.StanzaListener}).
  46.  * </p>
  47.  *
  48.  * @see org.jivesoftware.smack.StanzaCollector
  49.  * @see org.jivesoftware.smack.StanzaListener
  50.  * @author Matt Tucker
  51.  */
  52. public interface StanzaFilter extends Predicate<Stanza> {

  53.     /**
  54.      * Tests whether or not the specified stanza should pass the filter.
  55.      *
  56.      * @param stanza the stanza to test.
  57.      * @return true if and only if <code>stanza</code> passes the filter.
  58.      */
  59.     boolean accept(Stanza stanza);

  60.     @Override
  61.     default boolean test(Stanza stanza) {
  62.         return accept(stanza);
  63.     }

  64.     default <S extends Stanza> Predicate<S> asPredicate(Class<?> stanzaClass) {
  65.         return s -> {
  66.             if (!stanzaClass.isAssignableFrom(s.getClass())) {
  67.                 return false;
  68.             }
  69.             return accept(s);
  70.         };
  71.     }
  72. }