FromMatchesFilter.java

  1. /**
  2.  *
  3.  * Copyright 2003-2014 Jive Software, 2017 Florian Schmaus.
  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.jxmpp.jid.Jid;

  20. /**
  21.  * Filter for packets where the "from" field exactly matches a specified JID. If the specified
  22.  * address is a bare JID then the filter will match any address whose bare JID matches the
  23.  * specified JID. But if the specified address is a full JID then the filter will only match
  24.  * if the sender of the stanza matches the specified resource.
  25.  *
  26.  * @author Gaston Dombiak
  27.  */
  28. public final class FromMatchesFilter extends AbstractFromToMatchesFilter {

  29.     public static final FromMatchesFilter MATCH_NO_FROM_SET = create(null);

  30.     /**
  31.      * Creates a filter matching on the "from" field. The from address must be the same as the
  32.      * filter address. The second parameter specifies whether the full or the bare addresses are
  33.      * compared.
  34.      *
  35.      * @param address The address to filter for. If <code>null</code> is given, the stanza must not
  36.      *        have a from address.
  37.      * @param ignoreResourcepart TODO javadoc me please
  38.      */
  39.     public FromMatchesFilter(Jid address, boolean ignoreResourcepart) {
  40.         super(address, ignoreResourcepart);
  41.     }

  42.     /**
  43.      * Creates a filter matching on the "from" field. If the filter address is bare, compares
  44.      * the filter address with the bare from address. Otherwise, compares the filter address
  45.      * with the full from address.
  46.      *
  47.      * @param address The address to filter for. If <code>null</code> is given, the stanza must not
  48.      *        have a from address.
  49.      * @return filter for the "from" address.
  50.      */
  51.     public static FromMatchesFilter create(Jid address) {
  52.         return new FromMatchesFilter(address, address != null ? address.hasNoResource() : false) ;
  53.     }

  54.     /**
  55.      * Creates a filter matching on the "from" field. Compares the bare version of from and filter
  56.      * address.
  57.      *
  58.      * @param address The address to filter for. If <code>null</code> is given, the stanza must not
  59.      *        have a from address.
  60.      * @return filter matching the "from" address.
  61.      */
  62.     public static FromMatchesFilter createBare(Jid address) {
  63.         return new FromMatchesFilter(address, true);
  64.     }

  65.     /**
  66.      * Creates a filter matching on the "from" field. Compares the full version, if available, of from and filter
  67.      * address.
  68.      *
  69.      * @param address The address to filter for. If <code>null</code> is given, the stanza must not
  70.      *        have a from address.
  71.      * @return filter matching the "from" address.
  72.      */
  73.     public static FromMatchesFilter createFull(Jid address) {
  74.         return new FromMatchesFilter(address, false);
  75.     }

  76.     @Override
  77.     protected Jid getAddressToCompare(Stanza stanza) {
  78.         return stanza.getFrom();
  79.     }

  80. }