gpt4 book ai didi

java - 实现基于不同类型的谓词链接

转载 作者:行者123 更新时间:2023-11-30 06:05:47 26 4
gpt4 key购买 nike

我正在寻找链接谓词并将它们一起测试的能力。

想法

我们有谓词(或其他)的条件:

 1. P<T>, P<R>, P<S>
2. P<T>.and(P<T>) => P<T> as result P<T>.test(t1, t2)
3. P<T>.and(P<R>) => P<T,R> as result P<T,R>.test(t,r)
4. P<T>.and(P<R>.or(P<S>)) => P<T,R,S> as result P<T,R,S>.test(t,r,s)

我有模型

class User {
private String name;
private boolean isActive;
public User(String name, boolean isActive) {
this.name = name;
this.isActive = isActive;
}
//getters only...
}

顺序

public class Order {
private long createdAt;
private User user;
private Manager manager;
private boolean isApproved;

public Order(User user, Manager manager, long createdAt, boolean isApproved) {
this.user = user;
this.manager = manager;
this.createdAt = createdAt;
this.isApproved = isApproved;
}
// getters only ...
}

经理

class Manager {
private ManagerCompetency competency;
private String name;
public Manager(String name, ManagerCompetency competency) {
this.name = name;
this.competency = competency;
}
//getters only...
}

因此,我需要类似简称 OrderAcceptanceSpecification OAS

Boolean result = OAS.isUserCanCreateOrder()
.and(OAS.isOrderApproved())
.and(OAS.isOrderExpiredAfter(3600)).negate()
.and(
OAS.isOrderApproverCompetentAs(Competencies.HIGH)
.or(OAS.isOrderApproverCompetentAs(Competencies.MIDDLE)
).test(Order, Manager, User)

欢迎提出任何建议/改进。

我找到了类似的解决方案

public class ChainedPredicate<T> {

private T t;

private Predicate<T> predicate;

public ChainedPredicate(T t, Predicate<T> predicate) {
Objects.requireNonNull(t);
Objects.requireNonNull(predicate);
this.t = t;
this.predicate = predicate;
}

private ChainedPredicate(Predicate<T> predicate) {
Objects.requireNonNull(predicate);
this.predicate = predicate;
}

public Predicate<T> toPredicate() {
return t -> test();
}

public boolean test() {
return predicate.test(t);
}

public ChainedPredicate<T> and(ChainedPredicate<?> other) {
Objects.requireNonNull(other);
return new ChainedPredicate<T>(t -> test() && other.test());
}

public ChainedPredicate<T> or(ChainedPredicate<?> other) {
Objects.requireNonNull(other);
return new ChainedPredicate<T>(t -> test() || other.test());
}

public ChainedPredicate<T> negate() {
return new ChainedPredicate<T>(toPredicate().negate());
}
}

执行测试

public class TestChainedPredicate {

public static void main(String[] args) {

Predicate<Boolean> tp1 = x -> 1 >0;
Predicate<String> fp1 = x -> 1 <0;
Predicate<Integer> fp2 = x -> 1 <0;

ChainedPredicate<Boolean> p = new ChainedPredicate<Boolean>(true, tp1); //true
ChainedPredicate<String> p1 = new ChainedPredicate<String>("123", fp1); // false
ChainedPredicate<Integer> p2 = new ChainedPredicate<Integer>(100, fp2); // false

boolean result = p.and(p1.or(p2)).test(); // false
System.out.println(result + " expected : " + false + " : " + ((result==false) ? "OK" : "ERROR") );

result = p.or(p1.or(p2)).test();
System.out.println(result + " expected : " + true + " : " + ((result==true) ? "OK" : "ERROR"));

result = p1.or(p.and(p2)).test();
System.out.println(result + " expected : " + false + " : " + ((result==false) ? "OK" : "ERROR"));

result = p1.or(p.or(p2)).test(); // false
System.out.println(result + " expected : " + true + " : " + ((result==true) ? "OK" : "ERROR"));

result = p1.or(p.or(p2)).negate().test(); // false
System.out.println(result + " expected : " + false + " : " + ((result==false) ? "OK" : "ERROR"));

}

}

最佳答案

正如方法签名所暗示的,您只能链接相同类型的谓词。没有解决方法,除非您的类共享父类(super class)。

Predicate<T> and(Predicate<? super T> other)

Predicate<T> or(Predicate<? super T> other)

关于java - 实现基于不同类型的谓词链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45711123/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com