blob: 847b618a6921956212e141792767bcf09cc2062d [file] [log] [blame]
Shuyi Chend7955ce2013-05-22 14:51:55 -07001/**
2 * $RCSfile$
3 * $Revision$
4 * $Date$
5 *
6 * Copyright 2003-2007 Jive Software.
7 *
8 * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20
21package org.jivesoftware.smack.filter;
22
23import org.jivesoftware.smack.packet.Packet;
24
25import java.util.List;
26import java.util.ArrayList;
27
28/**
29 * Implements the logical AND operation over two or more packet filters.
30 * In other words, packets pass this filter if they pass <b>all</b> of the filters.
31 *
32 * @author Matt Tucker
33 */
34public class AndFilter implements PacketFilter {
35
36 /**
37 * The list of filters.
38 */
39 private List<PacketFilter> filters = new ArrayList<PacketFilter>();
40
41 /**
42 * Creates an empty AND filter. Filters should be added using the
43 * {@link #addFilter(PacketFilter)} method.
44 */
45 public AndFilter() {
46
47 }
48
49 /**
50 * Creates an AND filter using the specified filters.
51 *
52 * @param filters the filters to add.
53 */
54 public AndFilter(PacketFilter... filters) {
55 if (filters == null) {
56 throw new IllegalArgumentException("Parameter cannot be null.");
57 }
58 for(PacketFilter filter : filters) {
59 if(filter == null) {
60 throw new IllegalArgumentException("Parameter cannot be null.");
61 }
62 this.filters.add(filter);
63 }
64 }
65
66 /**
67 * Adds a filter to the filter list for the AND operation. A packet
68 * will pass the filter if all of the filters in the list accept it.
69 *
70 * @param filter a filter to add to the filter list.
71 */
72 public void addFilter(PacketFilter filter) {
73 if (filter == null) {
74 throw new IllegalArgumentException("Parameter cannot be null.");
75 }
76 filters.add(filter);
77 }
78
79 public boolean accept(Packet packet) {
80 for (PacketFilter filter : filters) {
81 if (!filter.accept(packet)) {
82 return false;
83 }
84 }
85 return true;
86 }
87
88 public String toString() {
89 return filters.toString();
90 }
91}