ToMatchesFilter.java

  1. /**
  2.  *
  3.  * Copyright 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. public final class ToMatchesFilter extends AbstractFromToMatchesFilter {

  21.     public static final ToMatchesFilter MATCH_NO_TO_SET = create(null);

  22.     public ToMatchesFilter(Jid address, boolean ignoreResourcepart) {
  23.         super(address, ignoreResourcepart);
  24.     }

  25.     /**
  26.      * Creates a filter matching on the "to" field. If the filter address is bare, compares
  27.      * the filter address with the bare from address. Otherwise, compares the filter address
  28.      * with the full from address.
  29.      *
  30.      * @param address The address to filter for. If <code>null</code> is given, the stanza must not
  31.      *        have a from address.
  32.      * @return filter matching the "to" address.
  33.      */
  34.     public static ToMatchesFilter create(Jid address) {
  35.         return new ToMatchesFilter(address, address != null ? address.hasNoResource() : false) ;
  36.     }

  37.     /**
  38.      * Creates a filter matching on the "to" field. Compares the bare version of to and filter
  39.      * address.
  40.      *
  41.      * @param address The address to filter for. If <code>null</code> is given, the stanza must not
  42.      *        have a from address.
  43.      * @return filter matching the "to" address.
  44.      */
  45.     public static ToMatchesFilter createBare(Jid address) {
  46.         return new ToMatchesFilter(address, true);
  47.     }

  48.     /**
  49.      * Creates a filter matching on the "to" field. Compares the full version, if available, of to and filter
  50.      * address.
  51.      *
  52.      * @param address The address to filter for. If <code>null</code> is given, the stanza must not
  53.      *        have a from address.
  54.      * @return filter matching the "to" address.
  55.      */
  56.     public static ToMatchesFilter createFull(Jid address) {
  57.         return new ToMatchesFilter(address, false);
  58.     }

  59.     @Override
  60.     protected Jid getAddressToCompare(Stanza stanza) {
  61.         return stanza.getTo();
  62.     }

  63. }