package gobblin.util;

import java.util.List;
import java.util.regex.Pattern;

import com.google.common.collect.Lists;


/**
 * A utility class for filtering datasets through blacklist and whitelist.
 */
public class DatasetFilterUtils {

  /**
   * Convert a list of Strings to a list of Patterns.
   */
  public static List<Pattern> getPatternsFromStrings(List<String> strings) {
    List<Pattern> patterns = Lists.newArrayList();
    for (String s : strings) {
      patterns.add(Pattern.compile(s, Pattern.CASE_INSENSITIVE));
    }
    return patterns;
  }

  /**
   * If whitelist is non-empty, a topic survives if it matches the whitelist.
   * Otherwise, a topic survives if it doesn't match the blacklist.
   * Whitelist and blacklist use regex patterns (NOT glob patterns).
   */
  public static boolean survived(String topic, List<Pattern> blacklist, List<Pattern> whitelist) {
    if (!whitelist.isEmpty()) {
      return stringInPatterns(topic, whitelist);
    } else {
      return !stringInPatterns(topic, blacklist);
    }
  }

  /**
   * Determines whether a string matches one of the regex patterns.
   */
  public static boolean stringInPatterns(String s, List<Pattern> patterns) {
    for (Pattern pattern : patterns) {
      if (pattern.matcher(s).matches()) {
        return true;
      }
    }
    return false;
  }
}

@@ -570,36 +570,9 @@
   * 
-If whitelist is non-empty, a
+A
  top
@@ -590,96 +590,107 @@
  if 
-i
+(1) it doesn'
 t match
-es
  the 
-whitelist.%0A   * Otherwise, a topic survives if it doesn'
+blacklist, and%0A   * (2) either whitelist is empty, or i
 t match
+es
  the 
-black
+white
 list
@@ -877,45 +877,8 @@
 if (
-!whitelist.isEmpty()) %7B%0A      return 
 stri
@@ -901,47 +901,83 @@
 ic, 
-white
+black
 list)
-;
+) %7B
 %0A    
-%7D else %7B%0A      return !
+  return false;%0A    %7D%0A    return (whitelist.isEmpty() %7C%7C 
 stri
@@ -1000,25 +1000,20 @@
 ic, 
-blacklist);%0A    %7D
+whitelist));
 %0A  %7D

change blacklist and whitelist logic