<commit_before>package org.pm4j.core.pm.filter;

import java.util.HashMap;
import java.util.Map;

import org.pm4j.core.pm.PmBean;

/**
 * A filter that is a composition of multiple filters.<br>
 * All sub-filters are combined by an AND-logic. That means an item must pass
 * the checks of all registered filters to get visible.
 * <p>
 * Each sub-filter can be added with an identifier.<br>
 * This allows to replace and remove specific filters.
 *
 * @author <NAME>
 */
public class MultiFilter implements Filter {

  /** The 'normal' filter set that will be removed when {@link #clear()} gets called. */
  private Map<String, Filter> filterMap = new HashMap<String, Filter>();

  /** A set of filters that is not affected by a {@link #clear()} call. */
  private Map<String, Filter> fixFilterMap = new HashMap<String, Filter>();

  private Filter[] activeFilters = {};

  @Override
  public boolean doesItemMatch(Object item) {
    for (Filter f : activeFilters) {
      @SuppressWarnings("unchecked")
      boolean match = f.isBeanFilter()
          ? f.doesItemMatch(((PmBean<Object>)item).getPmBean())
          : f.doesItemMatch(item);

      if (! match) {
        return false;
      }
    }
    return true;
  }

  /**
   * The {@link MultiFilter} itself takes the items directly (usually PmBean's).
   */
  @Override
  public boolean isBeanFilter() {
    return false;
  }

  /**
   * Activates the given filter.<br>
   * Replaces the filter that was formerly defined for the given id.<br>
   * Removes the filter if <code>null</code> will be passed as
   * <code>filter</code> parameter.
   *
   * @param filterId
   *          The filter identifier.
   * @param filter
   *          The filter to apply.<br>
   *          <code>null</code> removes the filter definition.
   * @return <code>true</code>.
   */
  public void setFilter(String filterId, Filter filter) {
    if (filter != null) {
      filterMap.put(filterId, filter);
    }
    else {
      filterMap.remove(filterId);
    }

    updateActiveFilters();
  }

  /**
   * Retrieves the current filter is registered for the given identifier.
   *
   * @param filterId The filter identifier.
   * @return The found filter. Is <code>null</code> if no filter is defined for the given identifier.
   */
  public Filter getFilter(String filterId) {
    return filterMap.get(filterId);
  }

  public void setFixFilter(String filterId, Filter filter) {
    if (filter != null) {
      fixFilterMap.put(filterId, filter);
    }
    else {
      fixFilterMap.remove(filterId);
    }

    updateActiveFilters();
  }

  public Filter getFixFilter(String filterId) {
    return fixFilterMap.get(filterId);
  }

  /**
   * Removes all filter definitions.
   */
  public void clear() {
    filterMap.clear();
    activeFilters = new Filter[0];
  }

  /**
   * @return <code>true</code> if there is no filter defined.
   */<commit_msg>Filter bugfix: clear() does no longer touch fix filter definitions.<commit_after>package org.pm4j.core.pm.filter;

import java.util.HashMap;
import java.util.Map;

import org.pm4j.core.pm.PmBean;

/**
 * A filter that is a composition of multiple filters.<br>
 * All sub-filters are combined by an AND-logic. That means an item must pass
 * the checks of all registered filters to get visible.
 * <p>
 * Each sub-filter can be added with an identifier.<br>
 * This allows to replace and remove specific filters.
 *
 * @author <NAME>
 */
public class MultiFilter implements Filter {

  /** The 'normal' filter set that will be removed when {@link #clear()} gets called. */
  private Map<String, Filter> filterMap = new HashMap<String, Filter>();

  /** A set of filters that is not affected by a {@link #clear()} call. */
  private Map<String, Filter> fixFilterMap = new HashMap<String, Filter>();

  private Filter[] activeFilters = {};

  @Override
  public boolean doesItemMatch(Object item) {
    for (Filter f : activeFilters) {
      @SuppressWarnings("unchecked")
      boolean match = f.isBeanFilter()
          ? f.doesItemMatch(((PmBean<Object>)item).getPmBean())
          : f.doesItemMatch(item);

      if (! match) {
        return false;
      }
    }
    return true;
  }

  /**
   * The {@link MultiFilter} itself takes the items directly (usually PmBean's).
   */
  @Override
  public boolean isBeanFilter() {
    return false;
  }

  /**
   * Activates the given filter.<br>
   * Replaces the filter that was formerly defined for the given id.<br>
   * Removes the filter if <code>null</code> will be passed as
   * <code>filter</code> parameter.
   *
   * @param filterId
   *          The filter identifier.
   * @param filter
   *          The filter to apply.<br>
   *          <code>null</code> removes the filter definition.
   * @return <code>true</code>.
   */
  public void setFilter(String filterId, Filter filter) {
    if (filter != null) {
      filterMap.put(filterId, filter);
    }
    else {
      filterMap.remove(filterId);
    }

    updateActiveFilters();
  }

  /**
   * Retrieves the current filter is registered for the given identifier.
   *
   * @param filterId The filter identifier.
   * @return The found filter. Is <code>null</code> if no filter is defined for the given identifier.
   */
  public Filter getFilter(String filterId) {
    return filterMap.get(filterId);
  }

  public void setFixFilter(String filterId, Filter filter) {
    if (filter != null) {
      fixFilterMap.put(filterId, filter);
    }
    else {
      fixFilterMap.remove(filterId);
    }

    updateActiveFilters();
  }

  public Filter getFixFilter(String filterId) {
    return fixFilterMap.get(filterId);
  }

  /**
   * Removes all filter definitions added by calls to {@link #setFilter(String, Filter)}.
   * <p>
   * All fix filters stay untouched.
   */
  public void clear() {
    filterMap.clear();
    updateActiveFilters();
  }

  /**
   * @return <code>true</code> if there is no filter defined.
   */
{'fraction_non_alphanumeric': 0.11553381683251814, 'fraction_numerical': 0.0008433125316242199, 'mean_word_length': 2.891076115485564, 'pattern_counts': {'":': 0, '<': 52, '<?xml version=': 0, '>': 52, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6718652', 'n_tokens_mistral': 1770, 'n_tokens_neox': 1749, 'n_words': 572}