mirror of
https://codeberg.org/Mercury-IM/Smack
synced 2024-11-15 12:02:05 +01:00
AndFilter and OrFilter now work over more than two packets (SMACK-96).
git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@2136 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
parent
8e83a092db
commit
357ec5c13f
4 changed files with 131 additions and 17 deletions
|
@ -55,18 +55,34 @@ package org.jivesoftware.smack.filter;
|
||||||
import org.jivesoftware.smack.packet.Packet;
|
import org.jivesoftware.smack.packet.Packet;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implements the logical AND operation over two packet filters. In other words, packets
|
* Implements the logical AND operation over two or more packet filters.
|
||||||
* pass this filter if they pass <b>both</b> of the filters.
|
* In other words, packets pass this filter if they pass <b>all</b> of the filters.
|
||||||
*
|
*
|
||||||
* @author Matt Tucker
|
* @author Matt Tucker
|
||||||
*/
|
*/
|
||||||
public class AndFilter implements PacketFilter {
|
public class AndFilter implements PacketFilter {
|
||||||
|
|
||||||
private PacketFilter filter1;
|
/**
|
||||||
private PacketFilter filter2;
|
* The current number of elements in the filter.
|
||||||
|
*/
|
||||||
|
private int size;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates an AND filter using the specified filters.
|
* The list of filters.
|
||||||
|
*/
|
||||||
|
private PacketFilter [] filters;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates an empty AND filter. Filters should be added using the
|
||||||
|
* {@link #addFilter(PacketFilter) method.
|
||||||
|
*/
|
||||||
|
public AndFilter() {
|
||||||
|
size = 0;
|
||||||
|
filters = new PacketFilter[3];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates an AND filter using the two specified filters.
|
||||||
*
|
*
|
||||||
* @param filter1 the first packet filter.
|
* @param filter1 the first packet filter.
|
||||||
* @param filter2 the second packet filter.
|
* @param filter2 the second packet filter.
|
||||||
|
@ -75,11 +91,45 @@ public class AndFilter implements PacketFilter {
|
||||||
if (filter1 == null || filter2 == null) {
|
if (filter1 == null || filter2 == null) {
|
||||||
throw new IllegalArgumentException("Parameters cannot be null.");
|
throw new IllegalArgumentException("Parameters cannot be null.");
|
||||||
}
|
}
|
||||||
this.filter1 = filter1;
|
size = 2;
|
||||||
this.filter2 = filter2;
|
filters = new PacketFilter[2];
|
||||||
|
filters[0] = filter1;
|
||||||
|
filters[1] = filter2;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a filter to the filter list for the AND operation. A packet
|
||||||
|
* will pass the filter if all of the filters in the list accept it.
|
||||||
|
*
|
||||||
|
* @param filter a filter to add to the filter list.
|
||||||
|
*/
|
||||||
|
public void addFilter(PacketFilter filter) {
|
||||||
|
if (filter == null) {
|
||||||
|
throw new IllegalArgumentException("Parameter cannot be null.");
|
||||||
|
}
|
||||||
|
// If there is no more room left in the filters array, expand it.
|
||||||
|
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) {
|
||||||
return filter1.accept(packet) && filter2.accept(packet);
|
for (int i=0; i<size; i++) {
|
||||||
|
if (!filters[i].accept(packet)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String toString() {
|
||||||
|
return filters.toString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -55,18 +55,34 @@ package org.jivesoftware.smack.filter;
|
||||||
import org.jivesoftware.smack.packet.Packet;
|
import org.jivesoftware.smack.packet.Packet;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implements the logical OR operation over two packet filters. In other words, packets
|
* Implements the logical OR operation over two or more packet filters. In
|
||||||
* pass this filter if they pass <b>either</b> of the filters.
|
* other words, packets pass this filter if they pass <b>any</b> of the filters.
|
||||||
*
|
*
|
||||||
* @author Matt Tucker
|
* @author Matt Tucker
|
||||||
*/
|
*/
|
||||||
public class OrFilter implements PacketFilter {
|
public class OrFilter implements PacketFilter {
|
||||||
|
|
||||||
private PacketFilter filter1;
|
/**
|
||||||
private PacketFilter filter2;
|
* The current number of elements in the filter.
|
||||||
|
*/
|
||||||
|
private int size;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates an OR filter using the specified filters.
|
* The list of filters.
|
||||||
|
*/
|
||||||
|
private PacketFilter [] filters;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates an empty OR filter. Filters should be added using the
|
||||||
|
* {@link #addFilter(PacketFilter) method.
|
||||||
|
*/
|
||||||
|
public OrFilter() {
|
||||||
|
size = 0;
|
||||||
|
filters = new PacketFilter[3];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates an OR filter using the two specified filters.
|
||||||
*
|
*
|
||||||
* @param filter1 the first packet filter.
|
* @param filter1 the first packet filter.
|
||||||
* @param filter2 the second packet filter.
|
* @param filter2 the second packet filter.
|
||||||
|
@ -75,11 +91,45 @@ public class OrFilter implements PacketFilter {
|
||||||
if (filter1 == null || filter2 == null) {
|
if (filter1 == null || filter2 == null) {
|
||||||
throw new IllegalArgumentException("Parameters cannot be null.");
|
throw new IllegalArgumentException("Parameters cannot be null.");
|
||||||
}
|
}
|
||||||
this.filter1 = filter1;
|
size = 2;
|
||||||
this.filter2 = filter2;
|
filters = new PacketFilter[2];
|
||||||
|
filters[0] = filter1;
|
||||||
|
filters[1] = filter2;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a filter to the filter list for the OR operation. A packet
|
||||||
|
* will pass the filter if any filter in the list accepts it.
|
||||||
|
*
|
||||||
|
* @param filter a filter to add to the filter list.
|
||||||
|
*/
|
||||||
|
public void addFilter(PacketFilter filter) {
|
||||||
|
if (filter == null) {
|
||||||
|
throw new IllegalArgumentException("Parameter cannot be null.");
|
||||||
|
}
|
||||||
|
// If there is no more room left in the filters array, expand it.
|
||||||
|
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) {
|
||||||
return filter1.accept(packet) || filter2.accept(packet);
|
for (int i=0; i<size; i++) {
|
||||||
|
if (filters[i].accept(packet)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
public String toString() {
|
||||||
|
return filters.toString();
|
||||||
|
}
|
||||||
|
}
|
|
@ -88,5 +88,12 @@ public class AndFilterTest extends TestCase {
|
||||||
|
|
||||||
andFilter = new AndFilter(falseFilter, falseFilter);
|
andFilter = new AndFilter(falseFilter, falseFilter);
|
||||||
assertFalse(andFilter.accept(packet));
|
assertFalse(andFilter.accept(packet));
|
||||||
|
|
||||||
|
andFilter = new AndFilter();
|
||||||
|
andFilter.addFilter(trueFilter);
|
||||||
|
andFilter.addFilter(trueFilter);
|
||||||
|
andFilter.addFilter(falseFilter);
|
||||||
|
andFilter.addFilter(trueFilter);
|
||||||
|
assertFalse(andFilter.accept(packet));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -122,5 +122,12 @@ public class OrFilterTest extends TestCase {
|
||||||
new OrFilter(falseFilter, falseFilter), new OrFilter(falseFilter, falseFilter)
|
new OrFilter(falseFilter, falseFilter), new OrFilter(falseFilter, falseFilter)
|
||||||
);
|
);
|
||||||
assertFalse(orFilter.accept(packet));
|
assertFalse(orFilter.accept(packet));
|
||||||
|
|
||||||
|
orFilter = new OrFilter();
|
||||||
|
orFilter.addFilter(trueFilter);
|
||||||
|
orFilter.addFilter(trueFilter);
|
||||||
|
orFilter.addFilter(falseFilter);
|
||||||
|
orFilter.addFilter(trueFilter);
|
||||||
|
assertTrue(orFilter.accept(packet));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue