December 25, 2018

Providing implementation for getAsBoolean in BooleanSupplier – a functional interface – in java

If you don’t know about Functional Inteface then you can read about it, in the provided link.

Now coming to BooleanSupplier, it is a functional interface. It has only one abstract method.

  1. boolean getAsBoolean() :- it returns a boolean value and has no input parameters.

It can be used to provide streams of boolean values.

Implementation 1 :- This implementation, always returns true. It is a pure function, with no side effects.

BooleanSupplier alwaysReturnTrue = () -> true;

Implementaion 2 :- This implementation, always returns false. It is a pure function, with no side effects.

BooleanSupplier alwaysReturnFalse = () -> false;

We are going to create a class, which creates a boolean supplier based on condition coming from outside.

This class will have a constructor, which takes one input and based on that it creates a BooleanSupplier.

For this article, I am just passing a simple value of true or false. In practical scenario, this outside condition can be provided by state changes occurring somewhere else.

This is our class.

static class BooleanSupplierCreator {                                                                                                                                                                   
    private final boolean BOOL;                                                                                                                                                                         
                                                                                                                                                                                                            
    BooleanSupplierCreator(final boolean BOOL) {                                                                                                                                                        
        this.BOOL = BOOL;                                                                                                                                                                               
    }                                                                                                                                                                                                   
                                                                                                                                                                                                            
    BooleanSupplier getBooleanSupplier() {                                                                                                                                                              
        return () -> BOOL ;                                                                                                                                                                             
    }                                                                                                                                                                                                   
}

It has a getBooleanSupplier method, which returns a BooleanSupplier implementation.

We can use it to create different BooleanSupplier implementations.

The idea of this example is to show a different usage of BooleanSupplier.

Implementaion 3 :- Get a boolean supplier, which always returns true.

BooleanSupplier trueBooleanSupplier = (new BooleanSupplierCreator( true ))
                                                                      .getBooleanSupplier();

Implementation 4 :- Get a boolean supplier, which always returns false.

BooleanSupplier falseBooleanSupplier = (new BooleanSupplierCreator( false ))
                                                                    .getBooleanSupplier();

The main usage of this Interface could be for creation of BooleanStreams, like we have IntStreams. I am saying it from my own coding experiences.

And, we can always be creative with the usage.

Like, we can create a true-false  SWITCH using BooleanSupplier.

Complete Code

import java.util.function.BooleanSupplier;                                                                                                                                                                  
import static java.lang.System.out;                                                                                                                                                                         
                                                                                                                                                                                                            
public class BooleanSupplierTest {                                                                                                                                                                          
    public static void main(String ... args) {                                                                                                                                                              
        BooleanSupplier alwaysReturnTrue = () -> true;                                                                                                                                                      
        BooleanSupplier alwaysReturnFalse = () -> false;                                                                                                                                                    
                                                                                                                                                                                                            
        out.println("GET Boolean from alwaysReturnTrue - " + alwaysReturnTrue.getAsBoolean());                                                                                                              
        out.println("GET Boolean from alwaysReturnFalse - " + alwaysReturnFalse.getAsBoolean());                                                                                                            
                                                                                                                                                                                                            
        BooleanSupplier trueBooleanSupplier = (new BooleanSupplierCreator( true )).getBooleanSupplier();                                                                                                    
        BooleanSupplier falseBooleanSupplier = (new BooleanSupplierCreator( false )).getBooleanSupplier();                                                                                                  
                                                                                                                                                                                                            
        out.println("GET Boolean from trueBooleanSupplier - " + trueBooleanSupplier.getAsBoolean());                                                                                                        
        out.println("GET Boolean from falseBooleanSupplier - " + falseBooleanSupplier.getAsBoolean());                                                                                                      
                                                                                                                                                                                                            
    }                                                                                                                                                                                                       
                                                                                                                                                                                                            
    static class BooleanSupplierCreator {                                                                                                                                                                   
        private final boolean BOOL;                                                                                                                                                                         
                                                                                                                                                                                                            
        BooleanSupplierCreator(final boolean BOOL) {                                                                                                                                                        
            this.BOOL = BOOL;                                                                                                                                                                               
        }                                                                                                                                                                                                   
                                                                                                                                                                                                            
        BooleanSupplier getBooleanSupplier() {                                                                                                                                                              
            return () -> BOOL ;                                                                                                                                                                             
        }                                                                                                                                                                                                   
    }                                                                                                                                                                                                       
}

Output 

GET Boolean from alwaysReturnTrue - true                                                                                                                                                                    
GET Boolean from alwaysReturnFalse - false                                                                                                                                                                  
GET Boolean from trueBooleanSupplier - true                                                                                                                                                                 
GET Boolean from falseBooleanSupplier - false