From 357ec5c13f1332f277fb247fd2ce2bea52689e3e Mon Sep 17 00:00:00 2001 From: Matt Tucker Date: Mon, 13 Oct 2003 00:40:11 +0000 Subject: [PATCH] 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 --- .../jivesoftware/smack/filter/AndFilter.java | 66 +++++++++++++++--- .../jivesoftware/smack/filter/OrFilter.java | 68 ++++++++++++++++--- .../smack/filter/AndFilterTest.java | 7 ++ .../smack/filter/OrFilterTest.java | 7 ++ 4 files changed, 131 insertions(+), 17 deletions(-) diff --git a/source/org/jivesoftware/smack/filter/AndFilter.java b/source/org/jivesoftware/smack/filter/AndFilter.java index 75f250392..e860431e6 100644 --- a/source/org/jivesoftware/smack/filter/AndFilter.java +++ b/source/org/jivesoftware/smack/filter/AndFilter.java @@ -55,18 +55,34 @@ package org.jivesoftware.smack.filter; import org.jivesoftware.smack.packet.Packet; /** - * Implements the logical AND operation over two packet filters. In other words, packets - * pass this filter if they pass both of the filters. + * Implements the logical AND operation over two or more packet filters. + * In other words, packets pass this filter if they pass all of the filters. * * @author Matt Tucker */ 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 filter2 the second packet filter. @@ -75,11 +91,45 @@ public class AndFilter implements PacketFilter { if (filter1 == null || filter2 == null) { throw new IllegalArgumentException("Parameters cannot be null."); } - this.filter1 = filter1; - this.filter2 = filter2; + size = 2; + 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; ieither of the filters. + * Implements the logical OR operation over two or more packet filters. In + * other words, packets pass this filter if they pass any of the filters. * * @author Matt Tucker */ 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 filter2 the second packet filter. @@ -75,11 +91,45 @@ public class OrFilter implements PacketFilter { if (filter1 == null || filter2 == null) { throw new IllegalArgumentException("Parameters cannot be null."); } - this.filter1 = filter1; - this.filter2 = filter2; + size = 2; + 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