在Java8中组合两个函数

时间:2018-01-20 15:47:39

标签: java lambda java-8 functional-programming functional-interface

isReadyToDeliver方法中,如果订单中的所有产品都可用(ProductState.AVAILABLE)且订单状态已准备好发送(OrderState.READY_TO_SEND),则方法必须返回true。 我写了两部分,但是我无法将它们组合成回句,

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

  

andThen(Function<? super Boolean,? extends V>)类型中的方法Function<Order,Boolean>不适用于参数(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;
    }
}

2 个答案:

答案 0 :(得分:6)

如果您更改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);
}

您的函数组合示例不起作用,因为当您编写函数fg时,gf函数作为参数值返回。在您的情况下,它已被破坏,因为orderState期望Order并返回Boolean,本案例orderState.andThen()期望一个函数将Boolean作为参数并返回别的。此要求未得到满足,因为productState期望Order并返回Boolean。这正是以下错误所说的:

  

错误:函数类型中的方法andThen(Function)不适用于参数(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);
}

答案 1 :(得分:2)

来自两个函数orderStateproductState 您可以使用&&(逻辑)创建新的lambda表达式 并将其归还:

public Function<Order, Boolean> isReadyToDeliver() {

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

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


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