001/**
002 *
003 * Copyright 2017 Florian Schmaus.
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 */
017package org.jivesoftware.smack.filter;
018
019import org.jivesoftware.smack.packet.Stanza;
020
021import org.jxmpp.jid.Jid;
022
023public final class ToMatchesFilter extends AbstractFromToMatchesFilter {
024
025    public static final ToMatchesFilter MATCH_NO_TO_SET = create(null);
026
027    public ToMatchesFilter(Jid address, boolean ignoreResourcepart) {
028        super(address, ignoreResourcepart);
029    }
030
031    /**
032     * Creates a filter matching on the "to" field. If the filter address is bare, compares
033     * the filter address with the bare from address. Otherwise, compares the filter address
034     * with the full from address.
035     *
036     * @param address The address to filter for. If <code>null</code> is given, the stanza must not
037     *        have a from address.
038     */
039    public static ToMatchesFilter create(Jid address) {
040        return new ToMatchesFilter(address, address != null ? address.hasNoResource() : false) ;
041    }
042
043    /**
044     * Creates a filter matching on the "to" field. Compares the bare version of to and filter
045     * address.
046     *
047     * @param address The address to filter for. If <code>null</code> is given, the stanza must not
048     *        have a from address.
049     */
050    public static ToMatchesFilter createBare(Jid address) {
051        return new ToMatchesFilter(address, true);
052    }
053
054    /**
055     * Creates a filter matching on the "to" field. Compares the full version, if available, of to and filter
056     * address.
057     *
058     * @param address The address to filter for. If <code>null</code> is given, the stanza must not
059     *        have a from address.
060     */
061    public static ToMatchesFilter createFull(Jid address) {
062        return new ToMatchesFilter(address, false);
063    }
064
065    @Override
066    protected Jid getAddressToCompare(Stanza stanza) {
067        return stanza.getTo();
068    }
069
070}