001/** 002 * 003 * Copyright 2015-2021 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 this(new ArrayList<StanzaFilter>(Arrays.asList(filters))); 048 } 049 050 /** 051 * Creates an filter using the specified filters. 052 * 053 * @param filters the filters to add. 054 */ 055 protected AbstractListFilter(List<StanzaFilter> filters) { 056 Objects.requireNonNull(filters, "Parameter must not be null."); 057 for (StanzaFilter filter : filters) { 058 Objects.requireNonNull(filter, "Parameter must not be null."); 059 } 060 this.filters = filters; 061 } 062 063 /** 064 * Adds a filter to the filter list. A stanza will pass the filter if all of the filters in the 065 * list accept it. 066 * 067 * @param filter a filter to add to the filter list. 068 */ 069 public void addFilter(StanzaFilter filter) { 070 Objects.requireNonNull(filter, "Parameter must not be null."); 071 filters.add(filter); 072 } 073 074 @Override 075 public final String toString() { 076 StringBuilder sb = new StringBuilder(); 077 sb.append(getClass().getSimpleName()); 078 sb.append(": ("); 079 sb.append(StringUtils.toStringBuilder(filters, ", ")); 080 sb.append(')'); 081 return sb.toString(); 082 } 083}