Joins in pure-java database queries

Posted by & filed under Java.

I’ve been thinking about how to express joins in my pure Java query library. Hibernate and other ORMs allow you to map collection fields via join tables. They will take care of cascading inserts and deletes from multiple tables for you. While this is nice and easy, it can to cause problems with bigger object… Read more »

Checked Exceptions and Streams

Posted by & filed under Java.

There are different viewpoints on how to use exceptions effectively in Java. Some people like checked exceptions, some argue they are a failed experiment and prefer exclusive use of unchecked exceptions. Others eschew exceptions entirely in favour of passing and returning types like Optional or Maybe. Whatever your view, you are likely to need to… Read more »

C# style properties in Java

Posted by & filed under Java.

One of the complaints that often comes up in any discussion on Java is getters/setters. If there are c# developers present they sometimes suggest that Java would be better off with c# style properties. c# properties, replace separate getters and setter methods that often clutter Java code. They allow the getters/setters to be combined together,… Read more »

Typesafe database interaction with Java 8

Posted by & filed under Java.

Method references in Java 8 will allow us to build much nicer APIs for interacting with databases. For example when you combine method references with features we already had in Java it’s possible to create clean, typesafe queries without needing code generation. Full examples and implementation available on github. Here’s an example of what’s possible… Read more »

Null Coalescing in Java 8

Posted by & filed under Java.

SQL gives us a “coalesce” function, which returns the first non-null argument. This seems to be a common operation in Java too, since Java unfortunately burdens us with the concept of nulls. We have been able to do something similar with Java for some time using a varargs method like this: public static <T> T… Read more »

StrictMocks in Mockito

Posted by & filed under Java.

Today someone asked how to verify that only your stubbed interactions occur, and no others (when using Mockito). I have heard this asked this quite often, especially by people used to JMock where mocks are strict by default. I’d be interested in knowing if there’s an out of the box way of doing this. The… Read more »

InstanceOf “Smart Casts” with Java 8 Lambdas

Posted by & filed under Java.

As with try-as-expression, there are many other language features we can simulate with Lambdas. Another example is removing the cast commonly needed with instanceof. Kotlin has a nice feature called smart casts, which allows you to do if (x instanceof Duck) { x.quack(); } if (x instanceof Duck) { x.quack(); } Where x is “casted”… Read more »

Try as Expression in Java 8

Posted by & filed under Java.

I’ve been familiarising myself with the new Java 8 language features. It’s great how much easier it is to work around language limitations now that we have lambdas. One annoyance with Java is that try blocks cannot be used as expressions. We can do it with “if” conditionals using the ternary operator. String result =… Read more »

JavaScript shell scripting with Nashorn

Posted by & filed under Java.

One of the nice features of Nashorn is that you can write shell scripts in JavaScript. It supports #!s, #comments, reading arguments, and everything there’s a Java library for (Including executing external processes obviously) Here’s an example #!/home/benji/nashorn7/bin/nashorn #this is a comment   print(’I am running from ‘ + __FILE__);   var name = arguments.join(’… Read more »

JavaScript tests with JUnit

Posted by & filed under Java.

This weekend I have been playing with Nashorn, the new JavaScript engine coming in Java 8. As an exercise I implemented a JUnit runner for JavaScript unit tests using Nashorn. Others have implemented similar wrappers, we even have one at work. None of the ones I have found do everything I want, and it was… Read more »