Interface StanzaFilter

All Superinterfaces:
Predicate<Stanza>
All Known Implementing Classes:
AbstractExactJidTypeFilter, AbstractFromToMatchesFilter, AbstractJidTypeFilter, AbstractJidTypeFilter, AbstractListFilter, AbstractPossibleJidTypeFilter, AndFilter, EmptyToMatcher, ExtensionElementFilter, FlexibleStanzaTypeFilter, FromJidTypeFilter, FromMatchesFilter, FromTypeFilter, IQReplyFilter, IQResultReplyFilter, IQTypeFilter, MessageTypeFilter, MessageWithBodiesFilter, MessageWithSubjectFilter, MessageWithThreadFilter, NotFilter, OrFilter, PossibleFromTypeFilter, PossibleToTypeFilter, PresenceTypeFilter, StanzaExtensionFilter, StanzaIdFilter, StanzaTypeFilter, ThreadFilter, ToMatchesFilter, ToTypeFilter

public interface StanzaFilter extends Predicate<Stanza>
Defines a way to filter stanzas for particular attributes. Stanza filters are used when constructing stanza listeners or collectors -- the filter defines what stanzas match the criteria of the collector or listener for further stanza processing.

Several simple filters are pre-defined. These filters can be logically combined for more complex stanza filtering by using the AndFilter and OrFilter filters. It's also possible to define your own filters by implementing this interface. The code example below creates a trivial filter for stanzas with a specific ID (real code should use StanzaIdFilter instead).

 // Use an anonymous inner class to define a stanza filter that returns
 // all stanzas that have a stanza ID of "RS145".
 StanzaFilter myFilter = new StanzaFilter() {
     public boolean accept(Stanza stanza) {
         return "RS145".equals(stanza.getStanzaId());
     }
 };
 // Create a new stanza collector using the filter we created.
 StanzaCollector myCollector = connection.createStanzaCollector(myFilter);
 

As a rule of thumb: If you have a predicate method, that is, a method which takes a single Stanza as argument, is pure (side effect free) and returns only a boolean, then it is a good indicator that the logic should be put into a StanzaFilter (and be referenced in StanzaListener).

See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    accept(Stanza stanza)
    Tests whether or not the specified stanza should pass the filter.
    default <S extends Stanza>
    Predicate<S>
    asPredicate(Class<?> stanzaClass)
     
    default boolean
    test(Stanza stanza)
     

    Methods inherited from interface java.util.function.Predicate

    and, negate, or
  • Method Details

    • accept

      boolean accept(Stanza stanza)
      Tests whether or not the specified stanza should pass the filter.
      Parameters:
      stanza - the stanza to test.
      Returns:
      true if and only if stanza passes the filter.
    • test

      default boolean test(Stanza stanza)
      Specified by:
      test in interface Predicate<Stanza>
    • asPredicate

      default <S extends Stanza> Predicate<S> asPredicate(Class<?> stanzaClass)