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 */
030@SuppressWarnings("BadImport")
031public final class IQTypeFilter extends FlexibleStanzaTypeFilter<IQ> {
032
033    public static final StanzaFilter GET = new IQTypeFilter(Type.get);
034    public static final StanzaFilter SET = new IQTypeFilter(Type.set);
035    public static final StanzaFilter RESULT = new IQTypeFilter(Type.result);
036    public static final StanzaFilter ERROR = new IQTypeFilter(Type.error);
037    public static final StanzaFilter GET_OR_SET = new OrFilter(GET, SET);
038
039    private final IQ.Type type;
040
041    private IQTypeFilter(IQ.Type type) {
042        super(IQ.class);
043        this.type = Objects.requireNonNull(type, "Type must not be null");
044    }
045
046    @Override
047    protected boolean acceptSpecific(IQ iq) {
048        return iq.getType() == type;
049    }
050
051    @Override
052    public String toString() {
053        return getClass().getSimpleName() + ": type=" + type;
054    }
055}