001/**
002 *
003 * Copyright 2014 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 */
027public final class PresenceTypeFilter extends FlexibleStanzaTypeFilter<Presence> {
028
029    public static final PresenceTypeFilter AVAILABLE = new PresenceTypeFilter(Type.available);
030    public static final PresenceTypeFilter UNAVAILABLE = new PresenceTypeFilter(Type.unavailable);
031    public static final PresenceTypeFilter SUBSCRIBE = new PresenceTypeFilter(Type.subscribe);
032    public static final PresenceTypeFilter SUBSCRIBED = new PresenceTypeFilter(Type.subscribed);
033    public static final PresenceTypeFilter UNSUBSCRIBE = new PresenceTypeFilter(Type.unsubscribe);
034    public static final PresenceTypeFilter UNSUBSCRIBED = new PresenceTypeFilter(Type.unsubscribed);
035    public static final PresenceTypeFilter ERROR = new PresenceTypeFilter(Type.error);
036    public static final PresenceTypeFilter PROBE = new PresenceTypeFilter(Type.probe);
037
038    public static final StanzaFilter OUTGOING_PRESENCE_BROADCAST = new AndFilter(AVAILABLE, EmptyToMatcher.INSTANCE);
039
040    private final Presence.Type type;
041
042    private PresenceTypeFilter(Presence.Type type) {
043        super(Presence.class);
044        this.type = Objects.requireNonNull(type, "type must not be null");
045    }
046
047    @Override
048    protected boolean acceptSpecific(Presence presence) {
049        return presence.getType() == type;
050    }
051
052    @Override
053    public String toString() {
054        return getClass().getSimpleName() + ": type=" + type;
055    }
056}