AbstractListFilter.java

  1. /**
  2.  *
  3.  * Copyright 2015 Florian Schmaus
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.jivesoftware.smack.filter;

  18. import java.util.ArrayList;
  19. import java.util.Arrays;
  20. import java.util.Iterator;
  21. import java.util.List;

  22. import org.jivesoftware.smack.util.Objects;

  23. /**
  24.  *
  25.  */
  26. public abstract class AbstractListFilter implements StanzaFilter {

  27.     /**
  28.      * The list of filters.
  29.      */
  30.     protected final List<StanzaFilter> filters;

  31.     /**
  32.      * Creates an empty filter.
  33.      */
  34.     protected AbstractListFilter() {
  35.         filters = new ArrayList<StanzaFilter>();
  36.     }

  37.     /**
  38.      * Creates an filter using the specified filters.
  39.      *
  40.      * @param filters the filters to add.
  41.      */
  42.     protected AbstractListFilter(StanzaFilter... filters) {
  43.         Objects.requireNonNull(filters, "Parameter must not be null.");
  44.         for(StanzaFilter filter : filters) {
  45.             Objects.requireNonNull(filter, "Parameter must not be null.");
  46.         }
  47.         this.filters = new ArrayList<StanzaFilter>(Arrays.asList(filters));
  48.     }

  49.     /**
  50.      * Adds a filter to the filter list. A stanza will pass the filter if all of the filters in the
  51.      * list accept it.
  52.      *
  53.      * @param filter a filter to add to the filter list.
  54.      */
  55.     public void addFilter(StanzaFilter filter) {
  56.         Objects.requireNonNull(filter, "Parameter must not be null.");
  57.         filters.add(filter);
  58.     }

  59.     @Override
  60.     public final String toString() {
  61.         StringBuilder sb = new StringBuilder();
  62.         sb.append(getClass().getSimpleName());
  63.         sb.append(": (");
  64.         for (Iterator<StanzaFilter> it = filters.iterator(); it.hasNext();) {
  65.             StanzaFilter filter = it.next();
  66.             sb.append(filter.toString());
  67.             if (it.hasNext()) {
  68.                 sb.append(", ");
  69.             }
  70.         }
  71.         sb.append(")");
  72.         return sb.toString();
  73.     }
  74. }