001/** 002 * 003 * Copyright 2003-2007 Jive Software. 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 */ 017 018package org.jivesoftware.smack.filter; 019 020import org.jivesoftware.smack.packet.Message; 021import org.jivesoftware.smack.packet.Stanza; 022import org.jivesoftware.smack.packet.Presence; 023import org.jivesoftware.smack.packet.IQ; 024 025/** 026 * Filters for Stanzas of a particular type. The type is given as a Class object, so 027 * example types would: 028 * <ul> 029 * <li><tt>Message.class</tt> 030 * <li><tt>IQ.class</tt> 031 * <li><tt>Presence.class</tt> 032 * </ul> 033 * 034 * @author Matt Tucker 035 */ 036public final class StanzaTypeFilter implements StanzaFilter { 037 038 public static final StanzaTypeFilter PRESENCE = new StanzaTypeFilter(Presence.class); 039 public static final StanzaTypeFilter MESSAGE = new StanzaTypeFilter(Message.class); 040 public static final StanzaTypeFilter IQ = new StanzaTypeFilter(IQ.class); 041 042 private final Class<? extends Stanza> packetType; 043 044 /** 045 * Creates a new stanza(/packet) type filter that will filter for packets that are the 046 * same type as <tt>packetType</tt>. 047 * 048 * @param packetType the Class type. 049 */ 050 public StanzaTypeFilter(Class<? extends Stanza> packetType) { 051 this.packetType = packetType; 052 } 053 054 public boolean accept(Stanza packet) { 055 return packetType.isInstance(packet); 056 } 057 058 @Override 059 public String toString() { 060 return getClass().getSimpleName() + ": " + packetType.getName(); 061 } 062}