001/**
002 *
003 * Copyright 2003-2014 Jive Software, 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;
020import org.jxmpp.jid.Jid;
021
022/**
023 * Filter for packets where the "from" field exactly matches a specified JID. If the specified
024 * address is a bare JID then the filter will match any address whose bare JID matches the
025 * specified JID. But if the specified address is a full JID then the filter will only match
026 * if the sender of the stanza(/packet) matches the specified resource.
027 *
028 * @author Gaston Dombiak
029 */
030public final class FromMatchesFilter extends AbstractFromToMatchesFilter {
031
032    public final static FromMatchesFilter MATCH_NO_FROM_SET = create(null);
033
034    /**
035     * Creates a filter matching on the "from" field. The from address must be the same as the
036     * filter address. The second parameter specifies whether the full or the bare addresses are
037     * compared.
038     *
039     * @param address The address to filter for. If <code>null</code> is given, the stanza(/packet) must not
040     *        have a from address.
041     * @param ignoreResourcepart
042     */
043    public FromMatchesFilter(Jid address, boolean ignoreResourcepart) {
044        super(address, ignoreResourcepart);
045    }
046
047    /**
048     * Creates a filter matching on the "from" field. If the filter address is bare, compares
049     * the filter address with the bare from address. Otherwise, compares the filter address
050     * with the full from address.
051     *
052     * @param address The address to filter for. If <code>null</code> is given, the stanza must not
053     *        have a from address.
054     */
055    public static FromMatchesFilter create(Jid address) {
056        return new FromMatchesFilter(address, address != null ? address.hasNoResource() : false) ;
057    }
058
059    /**
060     * Creates a filter matching on the "from" field. Compares the bare version of from and filter
061     * address.
062     *
063     * @param address The address to filter for. If <code>null</code> is given, the stanza must not
064     *        have a from address.
065     */
066    public static FromMatchesFilter createBare(Jid address) {
067        return new FromMatchesFilter(address, true);
068    }
069
070    /**
071     * Creates a filter matching on the "from" field. Compares the full version, if available, of from and filter
072     * address.
073     *
074     * @param address The address to filter for. If <code>null</code> is given, the stanza must not
075     *        have a from address.
076     */
077    public static FromMatchesFilter createFull(Jid address) {
078        return new FromMatchesFilter(address, false);
079    }
080
081    @Override
082    protected Jid getAddressToCompare(Stanza stanza) {
083        return stanza.getFrom();
084    }
085
086}