My gut tells me this is poor practice, so, I thought I would ask for some feedback.
The goal here is to fake a fluent API design for any class without using reflection while keeping it as pretty as possible. If I were to use reflection, I would just use jOOR.
No need to comment on null-checks. I have omitted them in this post for the sake of simplicity.
Class:
public final class Fluent<T> {
private final T object;
public Fluent(final T object) {
this.object = object;
}
public Fluent<T> with(final Consumer<T> consumer) {
consumer.accept(object);
return this;
}
public T get() {
return object;
}
}
Example:
final MenuItem item = new Fluent<>(new MenuItem("Foo"))
.with(o -> o.setAccelerator(accelerator))
.with(o -> o.setOnAction(this::callback)).get();
SupplierbeConsumerinwith? \$\endgroup\$