001/** 002 * 003 * Copyright 2015 Florian Schmaus 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018package org.jivesoftware.smack.filter; 019 020import java.util.ArrayList; 021import java.util.Arrays; 022import java.util.List; 023 024import org.jivesoftware.smack.util.Objects; 025import org.jivesoftware.smack.util.StringUtils; 026 027public abstract class AbstractListFilter implements StanzaFilter { 028 029 /** 030 * The list of filters. 031 */ 032 protected final List<StanzaFilter> filters; 033 034 /** 035 * Creates an empty filter. 036 */ 037 protected AbstractListFilter() { 038 filters = new ArrayList<StanzaFilter>(); 039 } 040 041 /** 042 * Creates an filter using the specified filters. 043 * 044 * @param filters the filters to add. 045 */ 046 protected AbstractListFilter(StanzaFilter... filters) { 047 Objects.requireNonNull(filters, "Parameter must not be null."); 048 for (StanzaFilter filter : filters) { 049 Objects.requireNonNull(filter, "Parameter must not be null."); 050 } 051 this.filters = new ArrayList<StanzaFilter>(Arrays.asList(filters)); 052 } 053 054 /** 055 * Adds a filter to the filter list. A stanza will pass the filter if all of the filters in the 056 * list accept it. 057 * 058 * @param filter a filter to add to the filter list. 059 */ 060 public void addFilter(StanzaFilter filter) { 061 Objects.requireNonNull(filter, "Parameter must not be null."); 062 filters.add(filter); 063 } 064 065 @Override 066 public final String toString() { 067 StringBuilder sb = new StringBuilder(); 068 sb.append(getClass().getSimpleName()); 069 sb.append(": ("); 070 sb.append(StringUtils.toStringBuilder(filters, ", ")); 071 sb.append(')'); 072 return sb.toString(); 073 } 074}