White and Blacklist Logic using Java 8

I was creating a new maven application for my employer’s release process automation. In this maven plugin I need to able to detect a white and black list keyword that could be executed or not. In this example, the snippet code below is my working solution.

Please note, the code itself only works on the Java 8 and above due to usage of lambda functions. Further down, you need to use JDK version 8 and above to run the logic, other parts are omitted due to confidential information.

import java.util.ArrayList;
import java.util.List;

public class Utility {

   private static List<String> whitelisted, blacklisted;
   /**
     * Lookup function to determine the keyword is white or blacklisted.
     * @param dependencyKey key input from a variable to determine if this keyword is whitelisted or blacklisted.
     * @return a status of dependencyKey based on whitelisted/blacklisted entities.
     */
    public static Boolean componentKeyLookup(String dependencyKey) {
        boolean status = true;

        //check if any key is on blacklist.
        for(String blacklist : blacklisted){
            if(dependencyKey.contains(blacklist)){
               status = false; break;
            }
        }

        if (status) { //check if any key in still on whitelist.
            status = whitelisted.stream().anyMatch( s -> dependencyKey.contains(s));
        }

        return status;

    }

    // to make it works - try to create a lookup function to initialize the white-listed and blacklisted variable.
public static void listLookup(){
        String whiteListLine = "orange|banana|grape";
        String blackListLine = "pear|strawberries|durians";

        String[] whiteListArray = whiteListLine.split("\\|");
        String[] blackListArray = blackListLine.split("\\|");

        if(whiteListArray.length == 0 || blackListArray.length == 0){
            throw new IllegalArgumentException("List is not found.");
        }

        whitelisted = new ArrayList<>();
        for (String _whiteList : whiteListArray){
            whitelisted.add(_whiteList);
        }

        blacklisted = new ArrayList<>();
        for (String _blackList : blackListArray){
            blacklisted.add(_blackList);
        }
    }

}

Save the file and run it from another class / other method by initialize the listLookup() function. Then run the componentKeyLookup(String input) by inputting the word you want to check if it is on the white or blacklist. A sample snippet code below

//some imports package here (including the Utility class)

public class Main {

  public static void main(String[] args){
        Utility.listLookup(); //initialize component.
        boolean status1 = Utility.componentKeyLookup("banana");
        System.out.println(status1);

        boolean status2 = Utility.componentKeyLookup("orange");
        System.out.println(status1);

        boolean status3 = Utility.componentKeyLookup("grape");
        System.out.println(status1);

  }

}

and voila, that’s it. Simple and easy to use. You can modify and enhance the code above.

Leave a Reply

Your email address will not be published. Required fields are marked *