我正在尋找鏈接謂詞的能力並一起測試它們。基於不同類型實現謂詞鏈接
思考
我們有謂詞條件(或其他人):
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...
}
至於結果,我NEDD像 短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)
任何建議/改進的歡迎。
UPD 我已經發現了一些類似的解決方案
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"));
}
}
它看起來像所有的謂詞應該是類型的訂單.... –
這裏的問題是什麼? –
實現Chaining謂詞並一起測試它們。 –