This afternoon I was working on implementing a user story with (JodaTime) LocalDate ranges. The problem? I needed to represent a lot of periods, in days. The thing I needed to check: Is a certain LocalDate contained in this period.
First I started looking at JodaTime itself, it has support for Interval(s). But there is a ‘problem’, Interval is based around ReadableInstants, and LocalDate is a ReadablePartial, not a ReadableInstant! So to make my code work I had to do the following:
It works, sure, but the conversion to ReadableInstant with toDateTimeAtStartOfDay kind of bugged me. But luckely Google Guava has something to make the code much cleaner: Ranges
Using Range made the code much smaller and easier to read:
I really love using Google Guava, it provides solutions for a lot of common programming challenges. Not only that, their API is very very clean and skillfully designed. The use of open/closed in Range.java is very handy and makes it easy to think about corner cases.