2014-05-29 16:05:20 +02:00
|
|
|
/**
|
|
|
|
*
|
2015-02-26 18:41:17 +01:00
|
|
|
* Copyright 2014-2015 Florian Schmaus
|
2014-05-29 16:05:20 +02:00
|
|
|
*
|
|
|
|
* 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 java.lang.reflect.ParameterizedType;
|
|
|
|
|
2015-02-05 11:17:27 +01:00
|
|
|
import org.jivesoftware.smack.packet.Stanza;
|
2015-02-19 12:11:19 +01:00
|
|
|
import org.jivesoftware.smack.util.Objects;
|
2014-05-29 16:05:20 +02:00
|
|
|
|
|
|
|
/**
|
2015-02-26 18:41:17 +01:00
|
|
|
* Filters for stanzas of a particular type and allows a custom method to further filter the packets.
|
2014-05-29 16:05:20 +02:00
|
|
|
*
|
|
|
|
* @author Florian Schmaus
|
|
|
|
*/
|
2015-02-26 18:41:17 +01:00
|
|
|
public abstract class FlexibleStanzaTypeFilter<S extends Stanza> implements StanzaFilter {
|
2014-05-29 16:05:20 +02:00
|
|
|
|
2015-02-26 18:41:17 +01:00
|
|
|
protected final Class<S> stanzaType;
|
2014-05-29 16:05:20 +02:00
|
|
|
|
2015-02-26 18:41:17 +01:00
|
|
|
public FlexibleStanzaTypeFilter(Class<S> packetType) {
|
|
|
|
this.stanzaType = Objects.requireNonNull(packetType, "Type must not be null");
|
2014-05-29 16:05:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
2015-02-26 18:41:17 +01:00
|
|
|
public FlexibleStanzaTypeFilter() {
|
|
|
|
stanzaType = (Class<S>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
|
2014-05-29 16:05:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@SuppressWarnings("unchecked")
|
2015-02-26 18:41:17 +01:00
|
|
|
public final boolean accept(Stanza packet) {
|
|
|
|
if (stanzaType.isInstance(packet)) {
|
|
|
|
return acceptSpecific((S) packet);
|
2014-05-29 16:05:20 +02:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-02-26 18:41:17 +01:00
|
|
|
protected abstract boolean acceptSpecific(S packet);
|
2015-02-19 12:11:19 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
2015-02-26 18:41:17 +01:00
|
|
|
return getClass().getSimpleName() + ": " + stanzaType.toString();
|
2015-02-19 12:11:19 +01:00
|
|
|
}
|
2014-05-29 16:05:20 +02:00
|
|
|
}
|