mirror of
https://codeberg.org/Mercury-IM/Smack
synced 2024-11-22 22:32:06 +01:00
Unify AndFilter and OrFilter
AndFilter and OrFilter are dual; there is no reason why they should have differing interfaces.
This commit is contained in:
parent
9ac882241a
commit
e654eac9d5
3 changed files with 32 additions and 42 deletions
|
@ -17,10 +17,11 @@
|
||||||
|
|
||||||
package org.jivesoftware.smack.filter;
|
package org.jivesoftware.smack.filter;
|
||||||
|
|
||||||
import org.jivesoftware.smack.packet.Packet;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.jivesoftware.smack.packet.Packet;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implements the logical AND operation over two or more packet filters.
|
* Implements the logical AND operation over two or more packet filters.
|
||||||
|
@ -33,14 +34,14 @@ public class AndFilter implements PacketFilter {
|
||||||
/**
|
/**
|
||||||
* The list of filters.
|
* The list of filters.
|
||||||
*/
|
*/
|
||||||
private List<PacketFilter> filters = new ArrayList<PacketFilter>();
|
private final List<PacketFilter> filters;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates an empty AND filter. Filters should be added using the
|
* Creates an empty AND filter. Filters should be added using the
|
||||||
* {@link #addFilter(PacketFilter)} method.
|
* {@link #addFilter(PacketFilter)} method.
|
||||||
*/
|
*/
|
||||||
public AndFilter() {
|
public AndFilter() {
|
||||||
|
filters = new ArrayList<PacketFilter>();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -56,8 +57,8 @@ public class AndFilter implements PacketFilter {
|
||||||
if(filter == null) {
|
if(filter == null) {
|
||||||
throw new IllegalArgumentException("Parameter cannot be null.");
|
throw new IllegalArgumentException("Parameter cannot be null.");
|
||||||
}
|
}
|
||||||
this.filters.add(filter);
|
|
||||||
}
|
}
|
||||||
|
this.filters = new ArrayList<PacketFilter>(Arrays.asList(filters));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -17,6 +17,10 @@
|
||||||
|
|
||||||
package org.jivesoftware.smack.filter;
|
package org.jivesoftware.smack.filter;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import org.jivesoftware.smack.packet.Packet;
|
import org.jivesoftware.smack.packet.Packet;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -27,39 +31,34 @@ import org.jivesoftware.smack.packet.Packet;
|
||||||
*/
|
*/
|
||||||
public class OrFilter implements PacketFilter {
|
public class OrFilter implements PacketFilter {
|
||||||
|
|
||||||
/**
|
|
||||||
* The current number of elements in the filter.
|
|
||||||
*/
|
|
||||||
private int size;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The list of filters.
|
* The list of filters.
|
||||||
*/
|
*/
|
||||||
private PacketFilter [] filters;
|
private final List<PacketFilter> filters;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates an empty OR filter. Filters should be added using the
|
* Creates an empty OR filter. Filters should be added using the
|
||||||
* {@link #addFilter(PacketFilter)} method.
|
* {@link #addFilter(PacketFilter)} method.
|
||||||
*/
|
*/
|
||||||
public OrFilter() {
|
public OrFilter() {
|
||||||
size = 0;
|
filters = new ArrayList<PacketFilter>();
|
||||||
filters = new PacketFilter[3];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates an OR filter using the two specified filters.
|
* Creates an OR filter using the specified filters.
|
||||||
*
|
*
|
||||||
* @param filter1 the first packet filter.
|
* @param filters the filters to add.
|
||||||
* @param filter2 the second packet filter.
|
|
||||||
*/
|
*/
|
||||||
public OrFilter(PacketFilter filter1, PacketFilter filter2) {
|
public OrFilter(PacketFilter... filters) {
|
||||||
if (filter1 == null || filter2 == null) {
|
if (filters == null) {
|
||||||
throw new IllegalArgumentException("Parameters cannot be null.");
|
throw new IllegalArgumentException("Parameter cannot be null.");
|
||||||
}
|
}
|
||||||
size = 2;
|
for(PacketFilter filter : filters) {
|
||||||
filters = new PacketFilter[2];
|
if(filter == null) {
|
||||||
filters[0] = filter1;
|
throw new IllegalArgumentException("Parameter cannot be null.");
|
||||||
filters[1] = filter2;
|
}
|
||||||
|
}
|
||||||
|
this.filters = new ArrayList<PacketFilter>(Arrays.asList(filters));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -72,22 +71,12 @@ public class OrFilter implements PacketFilter {
|
||||||
if (filter == null) {
|
if (filter == null) {
|
||||||
throw new IllegalArgumentException("Parameter cannot be null.");
|
throw new IllegalArgumentException("Parameter cannot be null.");
|
||||||
}
|
}
|
||||||
// If there is no more room left in the filters array, expand it.
|
filters.add(filter);
|
||||||
if (size == filters.length) {
|
|
||||||
PacketFilter [] newFilters = new PacketFilter[filters.length+2];
|
|
||||||
for (int i=0; i<filters.length; i++) {
|
|
||||||
newFilters[i] = filters[i];
|
|
||||||
}
|
|
||||||
filters = newFilters;
|
|
||||||
}
|
|
||||||
// Add the new filter to the array.
|
|
||||||
filters[size] = filter;
|
|
||||||
size++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean accept(Packet packet) {
|
public boolean accept(Packet packet) {
|
||||||
for (int i=0; i<size; i++) {
|
for (PacketFilter filter : filters) {
|
||||||
if (filters[i].accept(packet)) {
|
if (filter.accept(packet)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -107,11 +107,11 @@ public class AgentSession {
|
||||||
queueUsersListeners = new ArrayList<QueueUsersListener>();
|
queueUsersListeners = new ArrayList<QueueUsersListener>();
|
||||||
|
|
||||||
// Create a filter to listen for packets we're interested in.
|
// Create a filter to listen for packets we're interested in.
|
||||||
OrFilter filter = new OrFilter();
|
OrFilter filter = new OrFilter(
|
||||||
filter.addFilter(new PacketTypeFilter(OfferRequestProvider.OfferRequestPacket.class));
|
new PacketTypeFilter(OfferRequestProvider.OfferRequestPacket.class),
|
||||||
filter.addFilter(new PacketTypeFilter(OfferRevokeProvider.OfferRevokePacket.class));
|
new PacketTypeFilter(OfferRevokeProvider.OfferRevokePacket.class),
|
||||||
filter.addFilter(new PacketTypeFilter(Presence.class));
|
new PacketTypeFilter(Presence.class),
|
||||||
filter.addFilter(new PacketTypeFilter(Message.class));
|
new PacketTypeFilter(Message.class));
|
||||||
|
|
||||||
packetListener = new PacketListener() {
|
packetListener = new PacketListener() {
|
||||||
public void processPacket(Packet packet) {
|
public void processPacket(Packet packet) {
|
||||||
|
|
Loading…
Reference in a new issue