December 11, 2018

How to use or implement java.util.function.BiConsumer interface in java.

BiConsumer is a Functional Interface . It has two methods, one abstract and other one is default method.

  1. void accept(T parameter1, U parameter2) :- It accepts two parameters and does not return anything.
  2. default BiConsumer<T, U> andThen(BiConsumer< ? super T, ? super U> applyThisAfterProcessing) :-  This function takes one BiConsumer and returns another BiConsumer .The BiConsumer , which is passed in as an argument is applied after the caller BiConsumer function.

What are we going to implement :- 

  1. Log users’ action in a log file for accessing illegal sites
  2. Send an e-mail to an administrator, informing of illegal access by the user.
  3. Make the above two steps sequential, rather making two different calls, make only one call.

Local variables used in this appliation.

String firstName = "anurag";
String lastName = "anand";

Utility Member methods used in this application

static void logUserAction(String fullName) {
    System.out.println(fullName + " has accessd restricted site.") ;
}

static void sendMailToAdministrator(String userEmailId) {
    System.out.println("Mail : Restircted Access By user -> " + userEmailId);
}

Step 1 :- Implementing a BiConsumer to log actions in a file.

BiConsumer<String, String>  logToFile = (fName, lName) -> logUserAction(fName + " " + lName);
logToFile.accept(firstName, lastName);

Output :- 

anurag anand has accessed restricted site.

Step 2 :- Implement a BiConsumer to send an e-mail to administrator

BiConsumer<String, String> informAdministrator = (fName, lName) -> sendMailToAdministrator(fName + "." + lName + "@abc.com");
informAdministrator.accept(firstName, lastName);

Output :- 

Mail : Restircted Access By user -> anurag.anand@abc.com

Step 3 :- Now, we will combine step 1 and step 2 and make these two step sequential by using andThen method.

//Step 1
BiConsumer<String, String>  logToFile = (fName, lName) -> logUserAction(fName + " " + lName);
//Step 2
BiConsumer<String, String> informAdministrator = (fName, lName) -> sendMailToAdministrator(fName + "." + lName + "@abc.com");

//Step 3 :- Combine the above two steps to make it sequential step
BiConsumer<String, String>  userLogger = logToFile.andThen(informAdministrator);
userLogger .accept(firstName, lastName);

Output

anurag anand has accessed restricted site.
Mail : Restircted Access By user -> anurag.anand@abc.com

We passed the informAdministrator function to the andThen method of logToFile function.
This will execute logToFile first and then informAdministrator function.
We can pass logToFile to informAdministrator’s andThen function, this will reverese the sequence of execution.

Complete Code

package com.functionalinterface;

import java.util.function.BiConsumer;

public class BiConsumerTest {
    public static void main(String[] args) {
        String firstName = "anurag";
        String lastName = "anand";

        //Step 1
        BiConsumer<String, String>  logToFile = (fName, lName) -> logUserAction(fName + " " + lName);
//        logToFile.accept(firstName, lastName);

        //Step 2
        BiConsumer<String, String> informAdministrator = (fName, lName) -> sendMailToAdministrator(fName + "." + lName + "@abc.com");
//        informAdministrator.accept(firstName, lastName);

        //Step 3 :- combine above two steps to create one sequential step
        BiConsumer<String, String>  userLogger = logToFile.andThen(informAdministrator);
        userLogger .accept(firstName, lastName);
    }

    static void logUserAction(String fullName) {
        System.out.println(fullName + " has accessed restricted site.") ;
    }

    static void sendMailToAdministrator(String userEmailId) {
        System.out.println("Mail : Restircted Access By user -> " + userEmailId);
    }
}