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.IQ;
021import org.jivesoftware.smack.packet.Message;
022import org.jivesoftware.smack.packet.Presence;
023import org.jivesoftware.smack.packet.Stanza;
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><code>Message.class</code>
030 *      <li><code>IQ.class</code>
031 *      <li><code>Presence.class</code>
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 type filter that will filter for packets that are the
046     * same type as <code>packetType</code>.
047     *
048     * @param packetType the Class type.
049     */
050    public StanzaTypeFilter(Class<? extends Stanza> packetType) {
051        this.packetType = packetType;
052    }
053
054    @Override
055    public boolean accept(Stanza packet) {
056        return packetType.isInstance(packet);
057    }
058
059    @Override
060    public String toString() {
061        return getClass().getSimpleName() + ": " + packetType.getSimpleName();
062    }
063}