001/**
002 *
003 * Copyright 2014-2021 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.Presence;
020import org.jivesoftware.smack.packet.Presence.Type;
021import org.jivesoftware.smack.util.Objects;
022
023/**
024 * A filter for Presence types. Returns true only if the stanza is an Presence stanza and it matches the type provided in the
025 * constructor.
026 */
027@SuppressWarnings("BadImport")
028public final class PresenceTypeFilter extends FlexibleStanzaTypeFilter<Presence> {
029
030    public static final PresenceTypeFilter AVAILABLE = new PresenceTypeFilter(Type.available);
031    public static final PresenceTypeFilter UNAVAILABLE = new PresenceTypeFilter(Type.unavailable);
032    public static final PresenceTypeFilter SUBSCRIBE = new PresenceTypeFilter(Type.subscribe);
033    public static final PresenceTypeFilter SUBSCRIBED = new PresenceTypeFilter(Type.subscribed);
034    public static final PresenceTypeFilter UNSUBSCRIBE = new PresenceTypeFilter(Type.unsubscribe);
035    public static final PresenceTypeFilter UNSUBSCRIBED = new PresenceTypeFilter(Type.unsubscribed);
036    public static final PresenceTypeFilter ERROR = new PresenceTypeFilter(Type.error);
037    public static final PresenceTypeFilter PROBE = new PresenceTypeFilter(Type.probe);
038
039    public static final StanzaFilter OUTGOING_PRESENCE_BROADCAST = new AndFilter(AVAILABLE, EmptyToMatcher.INSTANCE);
040
041    private final Presence.Type type;
042
043    private PresenceTypeFilter(Presence.Type type) {
044        super(Presence.class);
045        this.type = Objects.requireNonNull(type, "type must not be null");
046    }
047
048    @Override
049    protected boolean acceptSpecific(Presence presence) {
050        return presence.getType() == type;
051    }
052
053    @Override
054    public String toString() {
055        return getClass().getSimpleName() + ": type=" + type;
056    }
057}