package org.apache.logging.log4j.core.filter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.Marker;
import org.apache.logging.log4j.core.AbstractLifeCycle;
import org.apache.logging.log4j.core.Filter;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.Logger;
import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.config.plugins.PluginElement;
import org.apache.logging.log4j.core.config.plugins.PluginFactory;
import org.apache.logging.log4j.message.Message;

@Plugin(
   name = "filters",
   category = "Core",
   printObject = true
)
public final class CompositeFilter extends AbstractLifeCycle implements Iterable<Filter>, Filter {
   private final List<Filter> filters;

   private CompositeFilter() {
      this.filters = new ArrayList();
   }

   private CompositeFilter(List<Filter> filters) {
      if (filters == null) {
         this.filters = Collections.unmodifiableList(new ArrayList());
      } else {
         this.filters = Collections.unmodifiableList(filters);
      }
   }

   public CompositeFilter addFilter(Filter filter) {
      if (filter == null) {
         return this;
      } else {
         List<Filter> filterList = new ArrayList(this.filters);
         filterList.add(filter);
         return new CompositeFilter(Collections.unmodifiableList(filterList));
      }
   }

   public CompositeFilter removeFilter(Filter filter) {
      if (filter == null) {
         return this;
      } else {
         List<Filter> filterList = new ArrayList(this.filters);
         filterList.remove(filter);
         return new CompositeFilter(Collections.unmodifiableList(filterList));
      }
   }

   public Iterator<Filter> iterator() {
      return this.filters.iterator();
   }

   public List<Filter> getFilters() {
      return this.filters;
   }

   public boolean isEmpty() {
      return this.filters.isEmpty();
   }

   public int size() {
      return this.filters.size();
   }

   public void start() {
      this.setStarting();
      Iterator i$ = this.filters.iterator();

      while(i$.hasNext()) {
         Filter filter = (Filter)i$.next();
         filter.start();
      }

      this.setStarted();
   }

   public void stop() {
      this.setStopping();
      Iterator i$ = this.filters.iterator();

      while(i$.hasNext()) {
         Filter filter = (Filter)i$.next();
         filter.stop();
      }

      this.setStopped();
   }

   public Filter.Result getOnMismatch() {
      return Filter.Result.NEUTRAL;
   }

   public Filter.Result getOnMatch() {
      return Filter.Result.NEUTRAL;
   }

   public Filter.Result filter(Logger logger, Level level, Marker marker, String msg, Object... params) {
      Filter.Result result = Filter.Result.NEUTRAL;
      Iterator i$ = this.filters.iterator();

      do {
         if (!i$.hasNext()) {
            return result;
         }

         Filter filter = (Filter)i$.next();
         result = filter.filter(logger, level, marker, msg, params);
      } while(result != Filter.Result.ACCEPT && result != Filter.Result.DENY);

      return result;
   }

   public Filter.Result filter(Logger logger, Level level, Marker marker, Object msg, Throwable t) {
      Filter.Result result = Filter.Result.NEUTRAL;
      Iterator i$ = this.filters.iterator();

      do {
         if (!i$.hasNext()) {
            return result;
         }

         Filter filter = (Filter)i$.next();
         result = filter.filter(logger, level, marker, msg, t);
      } while(result != Filter.Result.ACCEPT && result != Filter.Result.DENY);

      return result;
   }

   public Filter.Result filter(Logger logger, Level level, Marker marker, Message msg, Throwable t) {
      Filter.Result result = Filter.Result.NEUTRAL;
      Iterator i$ = this.filters.iterator();

      do {
         if (!i$.hasNext()) {
            return result;
         }

         Filter filter = (Filter)i$.next();
         result = filter.filter(logger, level, marker, msg, t);
      } while(result != Filter.Result.ACCEPT && result != Filter.Result.DENY);

      return result;
   }

   public Filter.Result filter(LogEvent event) {
      Filter.Result result = Filter.Result.NEUTRAL;
      Iterator i$ = this.filters.iterator();

      do {
         if (!i$.hasNext()) {
            return result;
         }

         Filter filter = (Filter)i$.next();
         result = filter.filter(event);
      } while(result != Filter.Result.ACCEPT && result != Filter.Result.DENY);

      return result;
   }

   public String toString() {
      StringBuilder sb = new StringBuilder();

      Filter filter;
      for(Iterator i$ = this.filters.iterator(); i$.hasNext(); sb.append(filter.toString())) {
         filter = (Filter)i$.next();
         if (sb.length() == 0) {
            sb.append('{');
         } else {
            sb.append(", ");
         }
      }

      if (sb.length() > 0) {
         sb.append('}');
      }

      return sb.toString();
   }

   @PluginFactory
   public static CompositeFilter createFilters(@PluginElement("Filters") Filter[] filters) {
      List<Filter> filterList = filters != null && filters.length != 0 ? Arrays.asList(filters) : new ArrayList();
      return new CompositeFilter((List)filterList);
   }
}
