001/**
002 *
003 * Copyright 2017-2018 Florian Schmaus.
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.Stanza;
020
021import org.jxmpp.jid.Jid;
022
023public abstract class AbstractJidTypeFilter implements StanzaFilter {
024
025    protected enum JidType {
026        entityFull,
027        entityBare,
028        domainFull,
029        domainBare,
030        any,
031        ;
032    }
033
034    private final JidType jidType;
035
036    protected AbstractJidTypeFilter(JidType jidType) {
037        this.jidType = jidType;
038    }
039
040    protected abstract Jid getJidToInspect(Stanza stanza);
041
042    @Override
043    public final boolean accept(Stanza stanza) {
044        final Jid jid = getJidToInspect(stanza);
045
046        if (jid == null) {
047            return false;
048        }
049
050        switch (jidType) {
051        case entityFull:
052            return jid.isEntityFullJid();
053        case entityBare:
054            return jid.isEntityBareJid();
055        case domainFull:
056            return jid.isDomainFullJid();
057        case domainBare:
058            return jid.isDomainBareJid();
059        case any:
060            return true;
061        default:
062            throw new AssertionError();
063        }
064    }
065
066}