Class to represent a Predicate that can be displayed for human consumption.
Often, if you model some predicates for a data set, it would be great to print a predicate so that a user
could understand it. You then get just a java useful string like "x.y.x.Predicate@0x1231abc".
Wouldn't it be better to get:
AssetClass in (Equity, Bond) and State=Active
This achieved easily, assume Asset has assetClass and State, you can write 2 flexible predicates:
public static Predicate<Asset> hasAssetClass(String... assetClass) {
return new PrintablePredicate<>("AssetClass", t -> StringUtil.equalsAnyIgnoreCase(t.getAssetClass(), assetClass), assetClass);
}
public static Predicate<Asset> hasState(String... state) {
return new PrintablePredicate><("State", t -> StringUtil.equalsAnyIgnoreCase(t.getState(), state), state);
}
// now Combine them:
Predicate<Asset> predicate = hasAssetClass("Equity","Bond").and(hasState("Active"));
System.out.println(predicate); // AssetClass in (Equity, Bond) and State=Active