001/**
002 *
003 * Copyright 2003-2006 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 */
017package org.jivesoftware.smack.filter;
018
019import org.jivesoftware.smack.packet.IQ;
020import org.jivesoftware.smack.packet.IQ.Type;
021import org.jivesoftware.smack.util.Objects;
022
023/**
024 * A filter for IQ stanza types. Returns true only if the stanza is an IQ packet
025 * and it matches the type provided in the constructor.
026 *
027 * @author Alexander Wenckus
028 *
029 */
030public final class IQTypeFilter extends FlexibleStanzaTypeFilter<IQ> {
031
032    public static final StanzaFilter GET = new IQTypeFilter(Type.get);
033    public static final StanzaFilter SET = new IQTypeFilter(Type.set);
034    public static final StanzaFilter RESULT = new IQTypeFilter(Type.result);
035    public static final StanzaFilter ERROR = new IQTypeFilter(Type.error);
036    public static final StanzaFilter GET_OR_SET = new OrFilter(GET, SET);
037
038    private final IQ.Type type;
039
040    private IQTypeFilter(IQ.Type type) {
041        super(IQ.class);
042        this.type = Objects.requireNonNull(type, "Type must not be null");
043    }
044
045    @Override
046    protected boolean acceptSpecific(IQ iq) {
047        return iq.getType() == type;
048    }
049
050    @Override
051    public String toString() {
052        return getClass().getSimpleName() + ": type=" + type;
053    }
054}