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.Message.Type;
022
023
024/**
025 * Filters for packets of a specific type of Message (e.g. CHAT).
026 *
027 * @see org.jivesoftware.smack.packet.Message.Type
028 * @author Ward Harold
029 */
030public final class MessageTypeFilter extends FlexibleStanzaTypeFilter<Message> {
031
032    public static final StanzaFilter NORMAL = new MessageTypeFilter(Type.normal);
033    public static final StanzaFilter CHAT = new MessageTypeFilter(Type.chat);
034    public static final StanzaFilter GROUPCHAT = new MessageTypeFilter(Type.groupchat);
035    public static final StanzaFilter HEADLINE = new MessageTypeFilter(Type.headline);
036    public static final StanzaFilter ERROR = new MessageTypeFilter(Type.error);
037    public static final StanzaFilter NORMAL_OR_CHAT = new OrFilter(NORMAL, CHAT);
038    public static final StanzaFilter NORMAL_OR_CHAT_OR_HEADLINE = new OrFilter(NORMAL_OR_CHAT,
039                    HEADLINE);
040
041    private final Message.Type type;
042
043    /**
044     * Creates a new message type filter using the specified message type.
045     *
046     * @param type the message type.
047     */
048    private MessageTypeFilter(Message.Type type) {
049        super(Message.class);
050        this.type = type;
051    }
052
053    @Override
054    protected boolean acceptSpecific(Message message) {
055        return message.getType() == type;
056    }
057
058    @Override
059    public String toString() {
060        return getClass().getSimpleName() + ": type=" + type;
061    }
062}