posted on Thu, Apr 23 '20 under tag: code

AutoClosable

You’ve probably seen the try-with-resources statement which allows you to open resources and try doing something without having to explicit close the resource in a finally block.

AutoClosable is the interface that makes this possible. If you implement AutoClosable (by just having a close() method) you can have this for your own resources too.

I saw this in GBIF pipeline’s elasticsearch client wrapper class.

Lombok

In the same file, there is a @SneakyThrows which is actually a Lombok annotation to automatically catch errors that are too rare to be thrown. I had mentioned Lombok and code generation in my previous post about builder pattern. But, it maybe worth checking out in the context of not having to write getters and setters and such easily guessable simple things. (Of course, the better thing to do is to switch to kotlin)

Decorator pattern

While writing the previous paragraph, I wasn’t really sure what they called @something in java. I first tried searching “decorator” because in python decorators start with ‘@’. Turns out it is not called decorator in java. But landed on this nice article about decorator pattern on a website that seems to have many other patterns. Talking about patterns, there is also java-design-patterns.com which lists many cool patterns.

Annotations for type-checking

What’s the deal about annotations then? This quick lesson on oracle’s tutorial gives a very friendly introduction on annotations. Annotations are seen all over java code in modern projects and therefore it is worth the three minute that you spend on this lesson to know how they actually work. It also introduces the idea of type annotations and pluggable type systems which is really intresting because that allows things like @NonNull to exist freeing your mind from NullPointerExceptions. On implementation side, lombok has support for @NonNull, IntelliJ automatically warns, and The Checker Framework has a whole lot of custom types.

Apache

Apache has a million projects. Including avro, beam, and spark. It is definitely worth knowing about the Apache software foundation and the projects it supports.


There probably will be a part two for this post.

Like what you are reading? Subscribe (by RSS, email, twitter, or telegram)!