In this article, we will create a BooleanSupplier implementation, which returns true or false value on the alternate basis.

If it returns true on current call, then it will return a false on the next call and a true on the next to next call, and this will go on.

This is a very simple implementation. And it is very easy to understand.

We will create a class, which returns a boolean value on each call to it’s switchIt method.

In the switchIt method, we update the boolean member variable with the oppostie value of it’s last value, and then return the updated value.

No magic is happening here, we all have written code like this.

This is our class BooleanSwitch .

static class BooleanSwitch {                                                                                                                                                                            
    private static boolean switchBooleanValue;                                                                                                                                                          
                                                                                                                                                                                                            
    static boolean switchIt() {                                                                                                                                                                         
       return switchBooleanValue = !switchBooleanValue;                                                                                                                                                
    }                                                                                                                                                                                                   
}

Now, we create a BooleanSupplier using the above class and it’s switchIt method.

BooleanSupplier booleanSwitch = BooleanSwitch :: switchIt;

We are using java method reference to provide it’s implementation.

Now, I will wrap above codes in a class and call it in the main method, to get some output.

And, you will see the return value from the boolean supplier changes on each call.

import java.util.function.BooleanSupplier;                                                                                                                                                                  
import static java.lang.System.out;                                                                                                                                                                         
                                                                                                                                                                                                            
public class BooleanSupplierSwitch {                                                                                                                                                                        
    public static void main (String... args) {                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              

        BooleanSupplier booleanSwitch = BooleanSwitch :: switchIt;                                                                                                                                          
                                                                                                                                                                                                            
        for(int i=0; i<5; ++i) {                                                                                                                                                                            
            out.println("Current switch value is :- " + booleanSwitch.getAsBoolean());                                                                                                                      
                                                                                                                                                                                                            
        }                                                                                                                                                                                                   
    }                                                                                                                                                                                                       
                                                                                                                                                                                                            
    static class BooleanSwitch {                                                                                                                                                                            
        private static boolean switchBooleanValue;                                                                                                                                                          
                                                                                                                                                                                                            
        static boolean switchIt() {                                                                                                                                                                         
            return switchBooleanValue = !switchBooleanValue;                                                                                                                                                
        }                                                                                                                                                                                                   
                                                                                                                                                                                                            
    }                                                                                                                                                                                                       
}

Output

Current switch value is :- true                                                                                                                                                                             
Current switch value is :- false                                                                                                                                                                            
Current switch value is :- true                                                                                                                                                                             
Current switch value is :- false                                                                                                                                                                            
Current switch value is :- true