合并 Java8 中的两个函数
Combine two Functions in Java8
在isReadyToDeliver
方法中,如果订单中的所有产品都可用(ProductState.AVAILABLE
)并且订单状态已准备好发送(OrderState.READY_TO_SEND
),方法必须returntrue
。
我写了两个部分,但我无法将它们组合成 return 短语,
我写了 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()
更改为 return 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
函数 return秒。在你的情况下它被打破了,因为 orderState
期望 Order
和 return Boolean
而在这种情况下 orderState.andThen()
期望一个函数将 Boolean
作为一个参数和 returns 其他东西。此要求未得到满足,因为 productState
预期 Order
并且 return 已 Boolean
。这正是以下错误所说的:
error: The method andThen(Function) in the type Function is not applicable for the arguments (Function)
但是如果出于某种原因你想留在 Function<Order, Boolean>
那么你将有 return 像这样的 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);
}
来自orderState
和productState
这两个函数
您可以使用 &&
(逻辑 和 )创建一个新的 lambda 表达式
return 它:
public Function<Order, Boolean> isReadyToDeliver() {
Function<Order, Boolean> orderState = ...;
Function<Order, Boolean> productState = ...;
return o -> orderState.apply(o) && productState.apply(o);
}
在isReadyToDeliver
方法中,如果订单中的所有产品都可用(ProductState.AVAILABLE
)并且订单状态已准备好发送(OrderState.READY_TO_SEND
),方法必须returntrue
。
我写了两个部分,但我无法将它们组合成 return 短语,
我写了 return orderState.andThen(productState)
但收到此错误:
The method
andThen(Function<? super Boolean,? extends V>)
in the typeFunction<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()
更改为 return 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
函数 return秒。在你的情况下它被打破了,因为 orderState
期望 Order
和 return Boolean
而在这种情况下 orderState.andThen()
期望一个函数将 Boolean
作为一个参数和 returns 其他东西。此要求未得到满足,因为 productState
预期 Order
并且 return 已 Boolean
。这正是以下错误所说的:
error: The method andThen(Function) in the type Function is not applicable for the arguments (Function)
但是如果出于某种原因你想留在 Function<Order, Boolean>
那么你将有 return 像这样的 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);
}
来自orderState
和productState
这两个函数
您可以使用 &&
(逻辑 和 )创建一个新的 lambda 表达式
return 它:
public Function<Order, Boolean> isReadyToDeliver() {
Function<Order, Boolean> orderState = ...;
Function<Order, Boolean> productState = ...;
return o -> orderState.apply(o) && productState.apply(o);
}