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(/packet) and it matches the type provided in the 025 * constructor. 026 */ 027public 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 private final Presence.Type type; 039 040 private PresenceTypeFilter(Presence.Type type) { 041 super(Presence.class); 042 this.type = Objects.requireNonNull(type, "type must not be null"); 043 } 044 045 @Override 046 protected boolean acceptSpecific(Presence presence) { 047 return presence.getType() == type; 048 } 049 050 @Override 051 public String toString() { 052 return getClass().getSimpleName() + ": type=" + type; 053 } 054}