Combining two or more Consumer Functional Interface

In this post I will explain how to combine two or more Consumer Functional interface implementations.

Consumer Functional interface has a default method “andThen”, which combines two Consumer Functional interface implementations.

The method signature is as shown below


default Consumer andThen(Consumer after)

The way two Consumer implementations are combined using “andThen” method is as shown below


Consumer consumer = consumer1.andThen(consumer2)

The result of above code execution is a composed Consumer implementation named “consumer” which contain both consumer1 and consumer2.

When the accept method on the Consumer implementation is called along with item to be consumed as shown below, first consumer1 will process the item and then consumer2.


consumer.accept(item);

If consumer1 throws exception consumer2 will not get executed.

If either one of them throws exception, the exception is propogated to the caller.

Below is an example. In the example I have created two Consumer implementations named “AppleRating” and “ApplePricing” as shown below

AppleRating


package function;

import java.util.Random;
import java.util.function.Consumer;

public class AppleRating implements Consumer {
    private Random random;
    
    public AppleRating() {
        random = new Random();
    }
    
    @Override
    public void accept(Apple t) {
        int rate = random.nextInt(10);
        t.setRate(rate);
    }
}

ApplePricing


package function;

import java.util.function.Consumer;

public class ApplePricing implements Consumer {
    @Override
    public void accept(Apple t) {
        if(t.getRate() < 5) {
            t.setPrice(2);
        } else {
            t.setPrice(10);
        }
    }
}

AppleRating will be combined with ApplePricing, so that first AppleRating rate the apple and next ApplePricing will decide the apple price based on its rating.

Below is the main code

Main Code


1  package function;
2  
3  import java.util.function.Consumer;
4  
5  public class ConsumerDemo1 {
6   public static void main(String[] args) {
7       ApplePricing applePricing = new ApplePricing();
8       AppleRating appleRating = new AppleRating();
9       Consumer consumer = appleRating.andThen(applePricing);
10      
11      for(int i = 0; i < 10; i++) {
12          Apple apple = new Apple();
13          consumer.accept(apple);
14          System.out.println("Apple " + i + ": " + apple.getRate() + " : " + apple.getPrice());
15      }
16  }
17 }

At line 9, I am combining the two consumers to create a composite consumer.

At line 13, I am calling accept method and passing each apple as argument.

Output

Apple 0: 4 : 2
Apple 1: 4 : 2
Apple 2: 3 : 2
Apple 3: 8 : 10
Apple 4: 9 : 10
Apple 5: 6 : 10
Apple 6: 2 : 2
Apple 7: 8 : 10
Apple 8: 0 : 2
Apple 9: 0 : 2

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s