在Java8中组合两个函数

前端之家收集整理的这篇文章主要介绍了在Java8中组合两个函数前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在isReadyToDeliver方法中,如果订单中的所有产品都可用(ProductState.AVAILABLE),并且订单状态已准备好发送(OrderState.READY_TO_SEND),则方法必须返回true.
我写了两部分,但我无法将它们组合在一起,

我写了return orderState.andThen(productState)但是得到了这个错误

The method andThen(Function<? super Boolean,? extends V>) in the type Function<Order,Boolean> is not applicable for the 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>)      
    }
}

如果需要其他类:

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

解决方法

如果更改isReadyToDeliver()以返回Predicate< Order>那么你将能够将两个谓词与.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: The method andThen(Function) in the type Function is not applicable for the arguments (Function)

但是如果由于某种原因你想继续使用Function< Order,Boolean>然后你将返回一个lambda,如:

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);
}
原文链接:https://www.f2er.com/java/239987.html

猜你在找的Java相关文章