Combine two functions in Java8

To the isReadyToDelivermethod, if all products in order ( ProductState.AVAILABLE) are available, and if the order status is ready for dispatch ( OrderState.READY_TO_SEND), the method should return true. I wrote both two parts, but I could not combine them in the inverse phrase,

I wrote return orderState.andThen(productState), but get this error:

andThen(Function<? super Boolean,? extends V>)Type method Function<Order,Boolean>not applicable to arguments(Function<Order,Boolean>)

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 necessary, other classes:

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;
    }
}
+6
source share
2 answers

If you change isReadyToDeliver()to return Predicate<Order>, then you can combine two predicates with a function .and(Predicate another):

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);
}

, f g, g , f. , orderState Order Boolean, orderState.andThen() , Boolean - . , productState Order Boolean. :

error: andThen (Function) Function (Function)

- Function<Order, Boolean>, :

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);
}
+6

orderState productState -, && ( ) :

public Function<Order, Boolean> isReadyToDeliver() {

    Function<Order, Boolean> orderState = ...;

    Function<Order, Boolean>  productState = ...;


    return o -> orderState.apply(o) && productState.apply(o);
} 
+2

All Articles