Java 8 Function class addThen default method

vicky :

I am learning Java-8 Lambda, I am trying to understand addThen default method in java.util.function.Function interface.As per my understanding addthen will first execute the First function and then it will execute the second method. So I created a program like below:

//Pojo class 
  class Bike {

    public Bike(String bikeName, int price, String bikeType) {
        this.bikeName = bikeName;
        this.price = price;
        this.bikeType = bikeType;
    }

    private String bikeType;
    private String bikeName;
    private int price;

    public String getBikeType() {
        return bikeType;
    }

    @Override
    public String toString() {
        return "Bike [bikeType=" + bikeType + ", bikeName=" + bikeName + ", price=" + price + "]";
    }

    public void setBikeType(String bikeType) {
        this.bikeType = bikeType;
    }

    public String getBikeName() {
        return bikeName;
    }

    public void setBikeName(String bikeName) {
        this.bikeName = bikeName;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

}

//Main class
public class FunctionInbuildDefaultMethodsExample {

    public static void main(String[] args) {
        learningAndThen();
    }

    static void learningAndThen() {

        Function<Bike, String> updateBikefunction = (Bike bike) -> {
            System.out.println("OldBike Name is::" + bike.getBikeName());
            bike.setBikeName("PULSOR-200CC");
            return bike.getBikeName();
        };

        Function<Bike, String> updateBikePriceFunction = (Bike bike) ->  {

            System.out.println("OldBike Price is::" + bike.getPrice());
            bike.setPrice(95000);
            return bike.getBikeName();

        };

        /*
         * First update Bike and then price
         * */
         /*Compilation error here*/
        Function<Bike,String> bikeFunction = updateBikefunction.andThen(updateBikePriceFunction);
        bikeFunction.apply( new Bike("PULSOR-125CC", 65000, "BAJAJ"));

    }
 }

I am getting an compilation error at the line

 Function<Bike,String> bikeFunction =
updateBikefunction.andThen(updateBikePriceFunction);

as

"The method andThen(Function) in the type Function is not applicable for the arguments (Function)"

, After looking into the source code of Function interface , I understand that addThen default method is looking for instance of type Function<String,Book>. My questions is, If addThen default method is supposed to execute the first function and then the next fuction which is passed as parameter , why the addThen default method of function interface is written in such a manner excepting instance of type Function<String,Book>.

Andrew Tobilko :
updateBikefunction = Bike -> String
updateBikePriceFunction = Bike -> String

updateBikefunction -> updateBikePriceFunction = Bike -> String -> Bike -> String
                                                        ^^^^^^^^^^^^^^

To form a chain of functions, the previous result should be an output for the next function. Here is this condition is broken.

The updateBikefunction could be changed to a Function<Bike, Bike>

    Function<Bike, Bike> updateBikefunction = (Bike bike) -> {
        ...
        return bike;
    };

to compile the line:

Function<Bike, String> bikeFunction = updateBikefunction.andThen(updateBikePriceFunction);

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=475106&siteId=1