tempus-fugit

Java micro-library for writing & testing concurrent code

Durations

Frequently, some length of time is represented as a long in Java. The trouble here is that you don't always know what time unit the long represents and as Effective Java recommends, you'll often end up with something like the following

1
connection.setReadTimeout(TIMEOUT_IN_MILLIS);

or you might come across unhelpful code like this;

1
connection.setReadTimeout(1000 * 2 * 60);

This is all very error prone, ugly and in the second case, fails to convey the intent. The Duration class captures a length of time and forces you to express the time unit. So, for example,

1
connection.setReadTimeout(seconds(2);

Using Duration in your own method forces your clients to create use an explicit time unit and you can convert back explicitly. for example,

1
2
3
4
5
public void setReadTimeout(Duration timeout) {
   ...
   readTimeout = timeout.inMillis();
   ...
}

Next, Testing Time Sensitive Code: Conditions »