All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sf.javagimmicks.util.IncludeExcludePatternPredicate Maven / Gradle / Ivy

There is a newer version: 0.99-alpha1
Show newest version
package net.sf.javagimmicks.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;

/**
 * A {@link Predicate} for {@link CharSequence}s that internally uses any number of
 * include and exclude regular expression {@link Pattern}s for filtering.
 */
public class IncludeExcludePatternPredicate implements Predicate
{
   /**
    * Creates a new instance for given {@link Collection}s of include and
    * exclude {@link Pattern}s.
    * 
    * @param includePatterns
    *           the {@link Collection} of include {@link Pattern}s
    * @param excludePatterns
    *           the {@link Collection} of exclude {@link Pattern}s
    * @return the new instance
    */
   public static IncludeExcludePatternPredicate fromPatterns(final Collection includePatterns,
         final Collection excludePatterns)
   {
      return new IncludeExcludePatternPredicate(
            new ArrayList(includePatterns),
            new ArrayList(excludePatterns));
   }

   /**
    * Creates a new instance for a given {@link Collection} of include
    * {@link Pattern}s.
    * 
    * @param includePatterns
    *           the {@link Collection} of include {@link Pattern}s
    * @return the new instance
    */
   public static IncludeExcludePatternPredicate fromPatterns(final Collection includePatterns)
   {
      final Collection excludePatterns = Collections.emptySet();

      return fromPatterns(includePatterns, excludePatterns);
   }

   /**
    * Creates a new instance for given {@link Collection}s of include and
    * exclude patterns provided as regular expression {@link String}s.
    * 
    * @param includePatterns
    *           the {@link Collection} of include patterns
    * @param excludePatterns
    *           the {@link Collection} of exclude patterns
    * @return the new instance
    */
   public static IncludeExcludePatternPredicate fromStringPatterns(final Collection includePatterns,
         final Collection excludePatterns)
   {
      return fromPatterns(bulkCompile(includePatterns), bulkCompile(excludePatterns));
   }

   /**
    * Creates a new instance for a given {@link Collection} of include patterns
    * provided as regular expression {@link String}s.
    * 
    * @param includePatterns
    *           the {@link Collection} of include patterns
    * @return the new instance
    */
   public static IncludeExcludePatternPredicate fromStringPatterns(final Collection includePatterns)
   {
      final Collection excludePatterns = Collections.emptySet();

      return fromStringPatterns(includePatterns, excludePatterns);
   }

   private final List _includePatterns;
   private final List _excludePatterns;

   /**
    * Creates a new instance without any include or exclude {@link Pattern}s.
    */
   public IncludeExcludePatternPredicate()
   {
      this(new ArrayList(), new ArrayList());
   }

   private IncludeExcludePatternPredicate(final List includePatterns, final List excludePatterns)
   {
      _includePatterns = includePatterns;
      _excludePatterns = excludePatterns;
   }

   /**
    * Adds a number of include {@link Pattern}s to this instance.
    * 
    * @param patterns
    *           the {@link Pattern}s to add
    */
   public void addIncludePatterns(final Collection patterns)
   {
      _includePatterns.addAll(patterns);
   }

   /**
    * Adds a number of include {@link Pattern}s to this instance.
    * 
    * @param patterns
    *           the {@link Pattern}s to add
    */
   public void addIncludePatterns(final Pattern... patterns)
   {
      addIncludePatterns(Arrays.asList(patterns));
   }

   /**
    * Adds a number of include pattern {@link String}s to this instance.
    * 
    * @param patterns
    *           the pattern {@link String}s to add
    */
   public void addIncludePatternStrings(final Collection patterns)
   {
      _includePatterns.addAll(bulkCompile(patterns));
   }

   /**
    * Adds a number of include pattern {@link String}s to this instance.
    * 
    * @param patterns
    *           the pattern {@link String}s to add
    */
   public void addIncludePatterns(final String... patterns)
   {
      addIncludePatternStrings(Arrays.asList(patterns));
   }

   /**
    * Adds a number of exclude {@link Pattern}s to this instance.
    * 
    * @param patterns
    *           the {@link Pattern}s to add
    */
   public void addExcludePatterns(final Collection patterns)
   {
      _excludePatterns.addAll(patterns);
   }

   /**
    * Adds a number of exclude {@link Pattern}s to this instance.
    * 
    * @param patterns
    *           the {@link Pattern}s to add
    */
   public void addExcludePatterns(final Pattern... patterns)
   {
      addExcludePatterns(Arrays.asList(patterns));
   }

   /**
    * Adds a number of exclude pattern {@link String}s to this instance.
    * 
    * @param patterns
    *           the pattern {@link String}s to add
    */
   public void addExcludePatternStrings(final Collection patterns)
   {
      _excludePatterns.addAll(bulkCompile(patterns));
   }

   /**
    * Adds a number of exclude pattern {@link String}s to this instance.
    * 
    * @param patterns
    *           the pattern {@link String}s to add
    */
   public void addExcludePatterns(final String... patterns)
   {
      addExcludePatternStrings(Arrays.asList(patterns));
   }

   /**
    * Returns a read-only view of the contained include {@link Pattern}s.
    * 
    * @return the include {@link Pattern}s
    */
   public List getIncludePatterns()
   {
      return Collections.unmodifiableList(_includePatterns);
   }

   /**
    * Returns a read-only view of the contained exclude {@link Pattern}s.
    * 
    * @return the exclude {@link Pattern}s
    */
   public List getExcludePatterns()
   {
      return Collections.unmodifiableList(_excludePatterns);
   }

   @Override
   public boolean test(final CharSequence charSequence)
   {
      return (_includePatterns.isEmpty() || matchesAny(_includePatterns, charSequence))
            && !matchesAny(_excludePatterns, charSequence);
   }

   private static boolean matchesAny(final List patterns, final CharSequence charSequence)
   {
      for (final Pattern pattern : patterns)
      {
         if (pattern.matcher(charSequence).matches())
         {
            return true;
         }
      }

      return false;
   }

   private static Set bulkCompile(final Collection patterns)
   {
      final Set result = new HashSet();
      for (final String pattern : patterns)
      {
         result.add(Pattern.compile(pattern));
      }

      return result;
   }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy