diff --git a/core/src/main/java/org/jivesoftware/smack/filter/AndFilter.java b/core/src/main/java/org/jivesoftware/smack/filter/AndFilter.java index 6b00fcd4d..ac5bb259f 100644 --- a/core/src/main/java/org/jivesoftware/smack/filter/AndFilter.java +++ b/core/src/main/java/org/jivesoftware/smack/filter/AndFilter.java @@ -17,10 +17,11 @@ package org.jivesoftware.smack.filter; -import org.jivesoftware.smack.packet.Packet; - -import java.util.List; 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. @@ -33,14 +34,14 @@ public class AndFilter implements PacketFilter { /** * The list of filters. */ - private List filters = new ArrayList(); + private final List filters; /** * Creates an empty AND filter. Filters should be added using the * {@link #addFilter(PacketFilter)} method. */ public AndFilter() { - + filters = new ArrayList(); } /** @@ -56,8 +57,8 @@ public class AndFilter implements PacketFilter { if(filter == null) { throw new IllegalArgumentException("Parameter cannot be null."); } - this.filters.add(filter); } + this.filters = new ArrayList(Arrays.asList(filters)); } /** diff --git a/core/src/main/java/org/jivesoftware/smack/filter/OrFilter.java b/core/src/main/java/org/jivesoftware/smack/filter/OrFilter.java index 141fcc9ac..e67977b50 100644 --- a/core/src/main/java/org/jivesoftware/smack/filter/OrFilter.java +++ b/core/src/main/java/org/jivesoftware/smack/filter/OrFilter.java @@ -17,6 +17,10 @@ package org.jivesoftware.smack.filter; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + import org.jivesoftware.smack.packet.Packet; /** @@ -27,39 +31,34 @@ import org.jivesoftware.smack.packet.Packet; */ public class OrFilter implements PacketFilter { - /** - * The current number of elements in the filter. - */ - private int size; - /** * The list of filters. */ - private PacketFilter [] filters; + private final List filters; /** * Creates an empty OR filter. Filters should be added using the * {@link #addFilter(PacketFilter)} method. */ public OrFilter() { - size = 0; - filters = new PacketFilter[3]; + filters = new ArrayList(); } /** - * Creates an OR filter using the two specified filters. + * Creates an OR filter using the specified filters. * - * @param filter1 the first packet filter. - * @param filter2 the second packet filter. + * @param filters the filters to add. */ - public OrFilter(PacketFilter filter1, PacketFilter filter2) { - if (filter1 == null || filter2 == null) { - throw new IllegalArgumentException("Parameters cannot be null."); + public OrFilter(PacketFilter... filters) { + if (filters == null) { + throw new IllegalArgumentException("Parameter cannot be null."); } - size = 2; - filters = new PacketFilter[2]; - filters[0] = filter1; - filters[1] = filter2; + for(PacketFilter filter : filters) { + if(filter == null) { + throw new IllegalArgumentException("Parameter cannot be null."); + } + } + this.filters = new ArrayList(Arrays.asList(filters)); } /** @@ -72,22 +71,12 @@ public class OrFilter implements PacketFilter { 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(); // Create a filter to listen for packets we're interested in. - OrFilter filter = new OrFilter(); - filter.addFilter(new PacketTypeFilter(OfferRequestProvider.OfferRequestPacket.class)); - filter.addFilter(new PacketTypeFilter(OfferRevokeProvider.OfferRevokePacket.class)); - filter.addFilter(new PacketTypeFilter(Presence.class)); - filter.addFilter(new PacketTypeFilter(Message.class)); + OrFilter filter = new OrFilter( + new PacketTypeFilter(OfferRequestProvider.OfferRequestPacket.class), + new PacketTypeFilter(OfferRevokeProvider.OfferRevokePacket.class), + new PacketTypeFilter(Presence.class), + new PacketTypeFilter(Message.class)); packetListener = new PacketListener() { public void processPacket(Packet packet) {