Over a million developers have joined DZone.

Fluent interfaces are finally ready for prime time in Java

· Java Zone

Check out this 8-step guide to see how you can increase your productivity by skipping slow application redeploys and by implementing application profiling, as you code! Brought to you in partnership with ZeroTurnaround.

One of the basic needs in fluent interface is connecting to user types. What field, method or operation does this fluent interface try to refer? This is not an easy question for the interface implementer! There are many techniques available to get this information, from mocking user types to providing a DSL for supported operations. But they have costs, require learning or plumbing code or are not refactoring friendly.

In C# this problem is solved by 2 language features: Lambda expressions and Expression Trees. For example, consider this:

var results = someCollection.where(c => c.SomeProperty < someValue * 2);

in C# library developer can get an expression tree of the user predicate above, and convert it to the proper behavior.

Java 8 provides the first feature and the second... is provided by JaQue project:

void method(Predicate<Customer> p) {
  LambdaExpression<Predicate<Customer>> parsed = LambdaExpression.parse(p);
  //Use parsed Expression Tree...
}

There is a hot discussion about an impact of Lambda expressions on JavaEE development. With JaQue Java becomes a semantic language, giving the libraries developers an ability to specify the meaning of user code at runtime.

The Java Zone is brought to you in partnership with ZeroTurnaround. Check out this 8-step guide to see how you can increase your productivity by skipping slow application redeploys and by implementing application profiling, as you code!

Topics:

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}