December 11, 2017
Perhaps you’re a fan of Java 8 and it’s no features and syntactical sugar, or you’re a seasoned veteran who’s open for new ideas in Java Land - here are some projects written in (or for) Java that are definitely worth checking out.
This one is not exactly a new player on the block, but it is a project that supports Java 8. The reason why Lombok
is such a great library to build projects on is because it clears up some of the boilerplate that comes with creating POJOs (Plain Old Java Objects).
For those who are new to Java, these are getters and setters for private fields, a default toString()
method and so much more, like a logger field that instantiates a logger via a factory that’s automagically added to your class, for a simple @Log
annotation.
Here’s a simple example of why Lombok is so powerful:
@Data
public class User {
private Long id;
private String username;
private String email;
private Boolean active;
}
With the above alone, I know have access to user.getUsername()
and user.setActive(bool)
automatically. This works, because at compile time, Lombok searches the AST (Abstract Syntax Tree) generated from your Java code and finds such annotations that are relevant for it. It then hooks into the bytecode generation part of the compiler and injects these properties on there. Of course if you’re using an IDE for Java development, you do need a plugin that understands this, otherwise you’ll see some errors showing up in your files.
Vavr is a functional library for Java 8, which makes use of lambda functions and other Java 8+ features to provide an excellent, easy to use API for developing with functional patterns in Java. It introduces concepts, like Try
, which is like an Optional
for methods that can throw an error. Some of it’s core features are those of functional languages, like tuples
, functions
, pattern matching
, currying
and memoization
, which are the building blocks of highly optimized applications.
The library itself provides a massive set of features, which you can read more about in the official docs. Here is a tiny taster as to what you can achieve with Vavr in a simplistic manner:
Lazy<Double> lazy = Lazy.of(Math::random);
lazy.isEvaluated(); // = false
lazy.get(); // = 0.123 (random generated)
lazy.isEvaluated(); // = true
lazy.get(); // = 0.123 (memoized)
this example shows a value, which gets lazily evaluated, which means that only upon calling get()
will it actually call to its constructor (Math.random()
) to receive a value. If no such call gets made, the footprint of this value becomes almost neglegable. The second time get()
is called, it will no longer access the constructor, but will have the value cached (memoized) as it’s return value, which is neat.
You can also sum a list of integers, with the following tiny code:
List.of(1, 2, 3).sum();
Finally, here is an example of currying from their docs:
Function3<Integer, Integer, Integer, Integer> sum = (a, b, c) -> a + b + c;
final Function1<Integer, Function1<Integer, Integer>> add2 = sum.curried().apply(2);
then(add2.apply(4).apply(3)).isEqualTo(9);
Some of the hard-core Java users may not be too familiar with functional patterns as they’ve gone relatively unused in OOP environments as of now, but there is a trend emerging of combining the two paradigms for great benefits.
Reactive Extensions or Rx for short is a very popular reactive programming library for multiple languages. At the moment one of the most popular implementations is actually the one for Java. Reactive programming has it’s place in the Java ecosystem via core libraries, like java.util.stream
, but libraries like RxJava provide refined APIs to deal with changes in Java applications.
Here is a nice example of RxJava at work:
class Example {
public static void hello(String... names) {
Observable.from(names)
.map(name -> "Hello, " + name + "!")
.subscribe(System.out::println);
}
}
// ...
Example.hello("Daniel", "David")
/* Prints:
Hello, Daniel!
Hello, David!
*/
RxJava is a large library and there are tons of cool features to check out. Of course it is also compatible with previous versions of Java, but Java 8 features make it much more worthwile to use in my opinion.
Do check out the official docs!
Guava is one of the most popular utility libraries, provided to the community by Google. It builds upon some of the base features of Java and provides nice, clean utilities on top to expand the capabilities of your application.
It’s got stuff from String utilities to behaviours, math, and even concurrency. Here is one of many example use cases of Guava:
Joiner.on(",").join(Arrays.asList(1, 5, 7)); // returns "1,5,7"
And here’s another example of creating a multimap, which is a challenge in itself to implement properly, but luckily Guava has it down for us:
ListMultimap<String, Integer> treeListMultimap =
MultimapBuilder.treeKeys().arrayListValues().build();
Here is a link to the docs to read more about it.
This one is not really a library, but more of a framework for creating HTTP-based microservices, favouring REST architecture. I’ve included it here, because it’s one of the less opinionated frameworks that do this, plus it’s been developed by Spotify, which is a massive company with highly reliable services with great uptime and massive demands, so if it works for them, it will work for my pet project too.
Below is an example taken straight from their website, which shows the minimal setup needed to create an Apollo Application:
public final class App {
public static void main(String[] args) throws LoadingException {
HttpService.boot(App::init, "my-app", args);
}
static void init(Environment environment) {
environment.routingEngine()
.registerAutoRoute(Route.sync("GET", "/", rc -> "hello world"));
}
}
If you want to build scalable microservices with Java, I believe Apollo is a great tool. Here is a link to their website.
This one is a very neat plugin, which browses your dependencies and checks if there are some that you can replace with built-in Java 8 features. Check it out on GitHub.
Having entered the Java World quite recently, I was drawn to developing with this language due to the combination of stable and well-though-of syntax and highly performant features as well as a large arsenal of libraries that are out there. I would not have chosen to pick up Java pre-version 8, but now I recommend it to everyone who wants to learn a mature and stable programming language.