Adam Heath wrote:
Additionally, becase fromLong creates an empty TimeDuration, then sets
the fields one by one, the vm is free to re-order those values, and
even delay then for a *long* time, well after fromLong returns to it's
caller. There is no synchronization on this; if these fields were
only set from the constructor, then this problem wouldn't occur.
Unfortunately, those are JVM details I wasn't aware of at the time.
Also, it might be nice to make use of TimeUnit for conversion to other
values.
Or maybe use the class as inspiration. A convert method might be handy.
If the internal representation was an array of ints, then we could have
int constants that are indexes into the array. Then we would need only
one accessor method:
int days = duration.get(TimeDuration.DAYS);