001/** 002 * 003 * Copyright 2015 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.jidtype; 018 019import org.jivesoftware.smack.filter.StanzaFilter; 020import org.jivesoftware.smack.packet.Stanza; 021import org.jivesoftware.smack.util.Objects; 022import org.jxmpp.jid.Jid; 023 024/** 025 * Base class for XMPP address type filters. 026 * 027 * @author Florian Schmaus 028 * 029 */ 030public abstract class AbstractJidTypeFilter implements StanzaFilter { 031 032 private final JidType jidType; 033 034 protected AbstractJidTypeFilter(JidType jidType) { 035 this.jidType = Objects.requireNonNull(jidType, "jidType must not be null"); 036 } 037 038 @Override 039 public boolean accept(Stanza stanza) { 040 Jid toMatch = getJidToMatchFrom(stanza); 041 if (toMatch == null) { 042 return false; 043 } 044 return jidType.isTypeOf(toMatch); 045 } 046 047 protected abstract Jid getJidToMatchFrom(Stanza stanza); 048 049 @Override 050 public final String toString() { 051 return getClass().getSimpleName() + ": " + jidType; 052 } 053 054 public enum JidType { 055 BareJid, 056 DomainBareJid, 057 DomainFullJid, 058 DomainJid, 059 EntityBareJid, 060 EntityFullJid, 061 EntityJid, 062 FullJid, 063 ; 064 065 public boolean isTypeOf(Jid jid) { 066 if (jid == null) { 067 return false; 068 } 069 switch (this) { 070 case BareJid: 071 return jid.hasNoResource(); 072 case DomainBareJid: 073 return jid.isDomainBareJid(); 074 case DomainFullJid: 075 return jid.isDomainFullJid(); 076 case EntityBareJid: 077 return jid.isEntityBareJid(); 078 case EntityFullJid: 079 return jid.isEntityFullJid(); 080 case EntityJid: 081 return jid.isEntityJid(); 082 case FullJid: 083 return jid.hasResource(); 084 default: 085 throw new IllegalStateException(); 086 } 087 } 088 } 089}