Making Duration comparable

Solves #754
Jochen has a point, and I missed that in my review of Size.comparetTo().
I'd recommend going with the revised method both in Duration and in Size,
as it makes the method more readable and more maintainable; less code to
break.

Glenn

On Thu, Oct 23, 2014 at 4:55 AM, Jochen Schalanda <notifications@github.com>
wrote:

> In dropwizard-util/src/main/java/io/dropwizard/util/Duration.java:
>
> > @@ -149,4 +149,27 @@ public String toString() {
> >          }
> >          return Long.toString(count) + ' ' + units;
> >      }
> > +
> > +    @Override
> > +    public int compareTo(Duration other) {
> > +        if (count == 0 && other.count == 0) {
> > +            return 0;
> > +        }
> > +
> > +        // this is negative, other is non-negative
> > +        if (count < 0 && other.count >= 0) {
>
> Which conversion? As a matter of fact just using Long#compare is more
> efficient as it only does 2 comparisons in the worst case than 4 (3 if
> conditionals and finally Long#compare) in the worst case. [image: :wink:]
>
> The method could be reduced to the following implementation and still has
> the same behaviour:
>
> if (unit == other.unit) {
>   return Long.compare(count, other.count);}
> return Long.compare(toNanoseconds(), other.toNanoseconds());
>
> —
> Reply to this email directly or view it on GitHub
> <https://github.com/dropwizard/dropwizard/pull/757/files#r19265709>.
>