A genuinely type safe Map for Java

Steve Neal Java Programming Leave a Comment

The problem: As much as I love generics in Java, it is still necessary to employ evil casts when retrieving different data types from a Map; see lines 13, 14 and 15: Did you spot the bug in this example? The compiler would have missed it. Ideally, I’d much rather work with code like this: In addition to the casts …

Adding a thread timeout to methods in Java

Steve Neal Development, Java Programming 1 Comment

When calling a method that could potentially take longer that you’d like to complete, it is possible to write code that will back out after a given time period. This can happen in numerous circumstances where you’d like an application to maintain a degree of liveness. I’ve used it recently in an application that needed to run a number of …

Using Assertions in Java

Steve Neal Development, Java Programming 1 Comment

I’ve been working on a project this week and have been delighted to see that the original team of programmers who developed it have made the effort to use assertions in their code. The assert keyword was introduced in Java 1.4 and has since been a sadly under-used language feature. Assertions offer developers a neat way to implement error detecting …