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     * @return filter matching the "to" address.
039     */
040    public static ToMatchesFilter create(Jid address) {
041        return new ToMatchesFilter(address, address != null ? address.hasNoResource() : false) ;
042    }
043
044    /**
045     * Creates a filter matching on the "to" field. Compares the bare version of to and filter
046     * address.
047     *
048     * @param address The address to filter for. If <code>null</code> is given, the stanza must not
049     *        have a from address.
050     * @return filter matching the "to" address.
051     */
052    public static ToMatchesFilter createBare(Jid address) {
053        return new ToMatchesFilter(address, true);
054    }
055
056    /**
057     * Creates a filter matching on the "to" field. Compares the full version, if available, of to and filter
058     * address.
059     *
060     * @param address The address to filter for. If <code>null</code> is given, the stanza must not
061     *        have a from address.
062     * @return filter matching the "to" address.
063     */
064    public static ToMatchesFilter createFull(Jid address) {
065        return new ToMatchesFilter(address, false);
066    }
067
068    @Override
069    protected Jid getAddressToCompare(Stanza stanza) {
070        return stanza.getTo();
071    }
072
073}