Combining two functions in Java 8

In the isreadytodeliver method, if all products in the order are available (productstate. Available) and the order state is ready to send (orderstate. Ready_to_send), the method must return true

I wrote return orderstate Andthen (productstate) but got this error:

public class OrderFunctions  {

    public Function<Order,Boolean> isReadyToDeliver() {            
        Function<Order,Boolean> orderState = o -> o.getState() == OrderState.READY_TO_SEND;            
        Function<Order,Boolean>  productState = 
                o -> o.getProducts()
                    .stream()
                    .map(Product -> Product.getState())
                    .allMatch(Product -> Product == ProductState.AVAILABLE);

        return ????? ; 
       //return  orderState.andThen(productState);
       //error: The method andThen(Function<? super Boolean,? extends V>) in the type Function<Order,Boolean> is not applicable for the arguments (Function<Order,Boolean>)      
    }
}

If other classes are required:

enum OrderState {CONFIRMED,PAID,WAREHOUSE_PROCESSED,READY_TO_SEND,DELIVERED }

enum ProductType { NORMAL,BREAKABLE,PERISHABLE }

public class Order {

    private OrderState state;
    private List<Product> products = new ArrayList<>();

    public OrderState getState() {
        return state;
    }

    public void setState(OrderState state) {
        this.state = state;
    }

    public Order state(OrderState state) {
        this.state = state;
        return this;
    }

    public List<Product> getProducts() {
        return products;
    }

    public void setProducts(List<Product> products) {
        this.products = products;
    }

    public Order product(Product product) {
        if (products == null) {
            products = new ArrayList<>();
        }
        products.add(product);
        return this;
    }
}

public class Product {

    private String code;
    private String title;
    private ProductState state;

    public ProductState getState() {
        return state;
    }

    public void setState(ProductState state) {
        this.state = state;
    }

    public Product state(ProductState state) {
        this.state = state;
        return this;
    }
}

Solution

If you change isreadytodeliver() to return predicate < order >, you will be able to combine the two predicates with And (predict another) function:

public Predicate<Order> isReadyToDeliver() {
    Predicate<Order> orderState = o -> o.getState() == OrderState.READY_TO_SEND;

    Predicate<Order> productState =
                o -> o.getProducts()
                   .stream()
                   .map(Product -> Product.getState())
                   .allMatch(Product -> Product == ProductState.AVAILABLE);

    return orderState.and(productState);
}

Your function combination example doesn't work because when you combine functions f and G, G is the parameter value returned by function F In your case, it is broken because orderstate expects order and returns Boolean. In this case, orderstate Andthen () expects a function to take Boolean as an argument and return something else This requirement is not satisfied because productstate expects order and returns Boolean This is exactly what the following error says:

However, if for some reason you want to continue using function < order, Boolean >, then you will return a lambda, such as:

public Function<Order,Boolean> isReadyToDeliver() {
    Function<Order,Boolean> orderState = o -> o.getState() == OrderState.READY_TO_SEND;

    Function<Order,Boolean> productState =
            o -> o.getProducts()
                    .stream()
                    .map(Product -> Product.getState())
                    .allMatch(Product -> Product == ProductState.AVAILABLE);


    return (order) -> orderState.apply(order) && productState.apply(order);
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>