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_HEADLINE = new OrFilter(NORMAL, HEADLINE);
039    public static final StanzaFilter NORMAL_OR_CHAT_OR_HEADLINE = new OrFilter(NORMAL_OR_CHAT,
040                    HEADLINE);
041
042    private final Message.Type type;
043
044    /**
045     * Creates a new message type filter using the specified message type.
046     *
047     * @param type the message type.
048     */
049    private MessageTypeFilter(Message.Type type) {
050        super(Message.class);
051        this.type = type;
052    }
053
054    @Override
055    protected boolean acceptSpecific(Message message) {
056        return message.getType() == type;
057    }
058
059    @Override
060    public String toString() {
061        return getClass().getSimpleName() + ": type=" + type;
062    }
063}