Reputation: 1620
I have below POC to use Java 8 feature.
I want to update DB after accept method. Is it good to go with andThen()
? When is this method called? Who calls it?
What is the basic use of andThen()
method? Looking at the docs was confusing.
public class StockTest {
public static void main(String[] args) {
List<Trader> traders = new ArrayList<>();
Random random = new Random();
// Initializing trading a/c's.
for (int i = 0; i < 10; i++) {
Trader trader = new Trader((random.nextInt(100) + 1) * 3);
traders.add(trader);
}
// Display Trade accounts.
System.out.println("Before Bonus, Units are:");
for (Trader trader : traders) {
System.out.print(trader.getUnits() + "\t");
}
// Add bonus to each trader.
traders.forEach(new Consumer<Trader>() {
@Override
public void accept(Trader trader) {
trader.updateBonus(2);
}
@Override
public Consumer<Trader> andThen(Consumer<? super Trader> after)
{
System.out.println("In andThen");
return Consumer.super.andThen(after);
}
});
// Display Trade accounts after bonus applied..
System.out.println("\nAfter bonus:");
for (Trader trader : traders) {
System.out.print(trader.getUnits() + "\t");
}
}
}
class Trader {
private int units;
public Trader(int initialUnits) {
this.units = initialUnits;
}
public int getUnits() {
return units;
}
public void setUnits(int units) {
this.units = units;
}
public void updateBonus(int bonusUnits) {
this.units = this.units * bonusUnits;
}
}
Please help with some example or use cases to utilize this method
Upvotes: 18
Views: 11311
Reputation: 11610
In short andThen is used to chain consumers, so the input will go to first and second consumer, lke below:
Consumer<Trader> consumer1 = new Consumer<Trader>() {
@Override
public void accept(Trader trader) {
trader.updateBonus(2);
}
};
Consumer<Trader> consumer2 = new Consumer<Trader>() {
@Override
public void accept(Trader trader) {
// do something
}
};
// Add bonus to each trader.
traders.forEach(consumer1.andThen(consumer2));
So here the Trader will be passed to consumer1
, then to consumer2
and so on.
You don't have to implement this method, or override it. When it comes to Consumers, implement only the accept
.
andThen
method is a helper tool to join consumers. Instead of passing the input to all of them in a loop.
Upvotes: 10
Reputation: 393831
You use andThen
when you want to chain the logic of two Consumer
s. consumer1.andThen(consumer2)
first calls the accept
method of consumer1
and then calls the accept
method of consumer2
.
Overriding the default implementation of andThen
makes little sense and prevents you from using lambda expressions/method references.
andThen
can be used to chain two Consumer
s:
traders.forEach(((Consumer<Trader>)(trader -> trader.updateBonus(2))).andThen(trader -> System.out.println("some more processing")));
Of course, in this example you can simply put the logic of the two Consumer
s in a single Consumer
:
traders.forEach(trader -> {trader.updateBonus(2);
System.out.println("some more processing");});
It makes more sense to use andThen
when you are chaining two existing Consumer
s:
Consumer<Trader> traderConsumer1 = trader -> trader.updateBonus(2);
Consumer<Trader> traderConsumer2 = trader -> System.out.println(trader);
traders.forEach(traderConsumer1.andThen(traderConsumer2));
Upvotes: 8