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