diff --git a/smack-core/src/main/java/org/jivesoftware/smack/filter/AbstractExactJidTypeFilter.java b/smack-core/src/main/java/org/jivesoftware/smack/filter/AbstractExactJidTypeFilter.java new file mode 100644 index 000000000..79b4099b0 --- /dev/null +++ b/smack-core/src/main/java/org/jivesoftware/smack/filter/AbstractExactJidTypeFilter.java @@ -0,0 +1,53 @@ +/** + * + * Copyright 2017-2018 Florian Schmaus. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jivesoftware.smack.filter; + +import org.jivesoftware.smack.packet.Stanza; + +import org.jxmpp.jid.Jid; + +public abstract class AbstractExactJidTypeFilter extends AbstractJidTypeFilter { + + protected AbstractExactJidTypeFilter(JidType jidType) { + super(jidType); + } + + @Override + public final boolean accept(Stanza stanza) { + final Jid jid = getJidToInspect(stanza); + + if (jid == null) { + return false; + } + + switch (jidType) { + case entityFull: + return jid.isEntityFullJid(); + case entityBare: + return jid.isEntityBareJid(); + case domainFull: + return jid.isDomainFullJid(); + case domainBare: + return jid.isDomainBareJid(); + case any: + return true; + default: + throw new AssertionError(); + } + } + +} diff --git a/smack-core/src/main/java/org/jivesoftware/smack/filter/AbstractJidTypeFilter.java b/smack-core/src/main/java/org/jivesoftware/smack/filter/AbstractJidTypeFilter.java index 93628b5e9..d0bf7d67b 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/filter/AbstractJidTypeFilter.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/filter/AbstractJidTypeFilter.java @@ -31,7 +31,7 @@ public abstract class AbstractJidTypeFilter implements StanzaFilter { ; } - private final JidType jidType; + protected final JidType jidType; protected AbstractJidTypeFilter(JidType jidType) { this.jidType = jidType; @@ -39,28 +39,4 @@ public abstract class AbstractJidTypeFilter implements StanzaFilter { protected abstract Jid getJidToInspect(Stanza stanza); - @Override - public final boolean accept(Stanza stanza) { - final Jid jid = getJidToInspect(stanza); - - if (jid == null) { - return false; - } - - switch (jidType) { - case entityFull: - return jid.isEntityFullJid(); - case entityBare: - return jid.isEntityBareJid(); - case domainFull: - return jid.isDomainFullJid(); - case domainBare: - return jid.isDomainBareJid(); - case any: - return true; - default: - throw new AssertionError(); - } - } - } diff --git a/smack-core/src/main/java/org/jivesoftware/smack/filter/AbstractPossibleJidTypeFilter.java b/smack-core/src/main/java/org/jivesoftware/smack/filter/AbstractPossibleJidTypeFilter.java new file mode 100644 index 000000000..9a88c3152 --- /dev/null +++ b/smack-core/src/main/java/org/jivesoftware/smack/filter/AbstractPossibleJidTypeFilter.java @@ -0,0 +1,52 @@ +/** + * + * Copyright 2018 Florian Schmaus. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jivesoftware.smack.filter; + +import org.jivesoftware.smack.packet.Stanza; + +import org.jxmpp.jid.Jid; + +public abstract class AbstractPossibleJidTypeFilter extends AbstractJidTypeFilter { + + protected AbstractPossibleJidTypeFilter(JidType jidType) { + super(jidType); + } + + @Override + public final boolean accept(Stanza stanza) { + final Jid jid = getJidToInspect(stanza); + + if (jid == null) { + return false; + } + + switch (jidType) { + case entityFull: + return null != jid.asEntityFullJidIfPossible(); + case entityBare: + return null != jid.asEntityBareJidIfPossible(); + case domainFull: + return null != jid.asDomainFullJidIfPossible(); + case domainBare: + case any: + return true; + default: + throw new AssertionError(); + } + } + +} diff --git a/smack-core/src/main/java/org/jivesoftware/smack/filter/FromTypeFilter.java b/smack-core/src/main/java/org/jivesoftware/smack/filter/FromTypeFilter.java index a571cd791..3a85da28a 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/filter/FromTypeFilter.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/filter/FromTypeFilter.java @@ -20,7 +20,7 @@ import org.jivesoftware.smack.packet.Stanza; import org.jxmpp.jid.Jid; -public final class FromTypeFilter extends AbstractJidTypeFilter { +public final class FromTypeFilter extends AbstractExactJidTypeFilter { public static final FromTypeFilter ENTITY_FULL_JID = new FromTypeFilter(JidType.entityFull); public static final FromTypeFilter ENTITY_BARE_JID = new FromTypeFilter(JidType.entityBare); diff --git a/smack-core/src/main/java/org/jivesoftware/smack/filter/PossibleFromTypeFilter.java b/smack-core/src/main/java/org/jivesoftware/smack/filter/PossibleFromTypeFilter.java new file mode 100644 index 000000000..f78148b76 --- /dev/null +++ b/smack-core/src/main/java/org/jivesoftware/smack/filter/PossibleFromTypeFilter.java @@ -0,0 +1,40 @@ +/** + * + * Copyright 2018 Florian Schmaus. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jivesoftware.smack.filter; + +import org.jivesoftware.smack.packet.Stanza; + +import org.jxmpp.jid.Jid; + +public final class PossibleFromTypeFilter extends AbstractPossibleJidTypeFilter { + + public static final PossibleFromTypeFilter ENTITY_FULL_JID = new PossibleFromTypeFilter(JidType.entityFull); + public static final PossibleFromTypeFilter ENTITY_BARE_JID = new PossibleFromTypeFilter(JidType.entityBare); + public static final PossibleFromTypeFilter DOMAIN_FULL_JID = new PossibleFromTypeFilter(JidType.domainFull); + public static final PossibleFromTypeFilter DOMAIN_BARE_JID = new PossibleFromTypeFilter(JidType.domainBare); + public static final PossibleFromTypeFilter FROM_ANY_JID = new PossibleFromTypeFilter(JidType.any); + + private PossibleFromTypeFilter(JidType jidType) { + super(jidType); + } + + @Override + protected Jid getJidToInspect(Stanza stanza) { + return stanza.getFrom(); + } + +} diff --git a/smack-core/src/main/java/org/jivesoftware/smack/filter/PossibleToTypeFilter.java b/smack-core/src/main/java/org/jivesoftware/smack/filter/PossibleToTypeFilter.java new file mode 100644 index 000000000..8cbc3dd8a --- /dev/null +++ b/smack-core/src/main/java/org/jivesoftware/smack/filter/PossibleToTypeFilter.java @@ -0,0 +1,42 @@ +/** + * + * Copyright 2018 Florian Schmaus. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jivesoftware.smack.filter; + +import org.jivesoftware.smack.packet.Stanza; + +import org.jxmpp.jid.Jid; + +public final class PossibleToTypeFilter extends AbstractPossibleJidTypeFilter { + + public static final PossibleToTypeFilter ENTITY_FULL_JID = new PossibleToTypeFilter(JidType.entityFull); + public static final PossibleToTypeFilter ENTITY_BARE_JID = new PossibleToTypeFilter(JidType.entityBare); + public static final PossibleToTypeFilter DOMAIN_FULL_JID = new PossibleToTypeFilter(JidType.domainFull); + public static final PossibleToTypeFilter DOMAIN_BARE_JID = new PossibleToTypeFilter(JidType.domainBare); + public static final PossibleToTypeFilter TO_ANY_JID = new PossibleToTypeFilter(JidType.any); + + public static final StanzaFilter ENTITY_FULL_OR_BARE_JID = new OrFilter(ENTITY_FULL_JID, ENTITY_BARE_JID); + + private PossibleToTypeFilter(JidType jidType) { + super(jidType); + } + + @Override + protected Jid getJidToInspect(Stanza stanza) { + return stanza.getTo(); + } + +} diff --git a/smack-core/src/main/java/org/jivesoftware/smack/filter/ToTypeFilter.java b/smack-core/src/main/java/org/jivesoftware/smack/filter/ToTypeFilter.java index 68b25d4ee..7513e9674 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/filter/ToTypeFilter.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/filter/ToTypeFilter.java @@ -20,7 +20,7 @@ import org.jivesoftware.smack.packet.Stanza; import org.jxmpp.jid.Jid; -public final class ToTypeFilter extends AbstractJidTypeFilter { +public final class ToTypeFilter extends AbstractExactJidTypeFilter { public static final ToTypeFilter ENTITY_FULL_JID = new ToTypeFilter(JidType.entityFull); public static final ToTypeFilter ENTITY_BARE_JID = new ToTypeFilter(JidType.entityBare);