Can I use for building java.util.function Interface Predicate<T> ? #401
hrstoyanov
started this conversation in
General
Replies: 1 comment 2 replies
-
|
Hi, I'm not sure what you are looking for. Something like this? import java.util.List;
import java.util.stream.Stream;
import am.ik.yavi.builder.ValidatorBuilder;
import am.ik.yavi.core.Validator;
public record Car(String manufacturer, String licensePlate, Integer seatCount) {
public static final Validator<Car> validator = ValidatorBuilder.<Car>of()
.constraint(Car::manufacturer, "manufacturer", c -> c.notNull())
.constraint(Car::licensePlate, "licensePlate", c -> c.notNull().greaterThanOrEqual(2).lessThanOrEqual(14))
.constraint(Car::seatCount, "seatCount", c -> c.greaterThanOrEqual(2))
.failFast(true)
.build();
public static void main(String[] args) {
Stream<Car> stream = Stream.of(
new Car(null, "DD-AB-123", 4),
new Car("Morris", "D", 4),
new Car("Morris", "DD-AB-123", 1),
new Car("Morris", "DD-AB-123", 2));
List<Car> filtered = stream.filter(car -> Car.validator.validate(car).isValid()).toList();
System.out.println(filtered); // [Car[manufacturer=Morris, licensePlate=DD-AB-123, seatCount=2]]
}
} |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Just came across YAVI, thank you for this wonderful, no-refletion, no-annotations, no-dependency java library!
Can I use this one for building complex Predicate<> for stream filtering? I think "fail fast" mode is good here, I also do not need to store info about exactly which property fails the test, just the validation/test result - true/false.
Any code example would be appreciated!
Beta Was this translation helpful? Give feedback.
All reactions