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 */
030@SuppressWarnings("BadImport")
031public final class MessageTypeFilter extends FlexibleStanzaTypeFilter<Message> {
032
033    public static final StanzaFilter NORMAL = new MessageTypeFilter(Type.normal);
034    public static final StanzaFilter CHAT = new MessageTypeFilter(Type.chat);
035    public static final StanzaFilter GROUPCHAT = new MessageTypeFilter(Type.groupchat);
036    public static final StanzaFilter HEADLINE = new MessageTypeFilter(Type.headline);
037    public static final StanzaFilter ERROR = new MessageTypeFilter(Type.error);
038    public static final StanzaFilter NORMAL_OR_CHAT = new OrFilter(NORMAL, CHAT);
039    public static final StanzaFilter NORMAL_OR_HEADLINE = new OrFilter(NORMAL, HEADLINE);
040    public static final StanzaFilter NORMAL_OR_CHAT_OR_HEADLINE = new OrFilter(NORMAL_OR_CHAT,
041                    HEADLINE);
042
043    private final Message.Type type;
044
045    /**
046     * Creates a new message type filter using the specified message type.
047     *
048     * @param type the message type.
049     */
050    private MessageTypeFilter(Message.Type type) {
051        super(Message.class);
052        this.type = type;
053    }
054
055    @Override
056    protected boolean acceptSpecific(Message message) {
057        return message.getType() == type;
058    }
059
060    @Override
061    public String toString() {
062        return getClass().getSimpleName() + ": type=" + type;
063    }
064}