Re: Some possible enhancements for java.time.Duration?

2021-06-15 Thread Robert Marcano

On 6/15/21 10:15 AM, dfranken@gmail.com wrote:

Dear readers,

I think java.time.Duration is a pretty useful class and after having to
work a lot with it, I recognized there might be some enhancements we
could make (where 'enhancement' is a subjective term of course).

For instance:

Comparison
--
   boolean isGreaterThan(Duration duration) / isLongerThan(..)
   boolean isSmallerThan(Duration duration) / isShorterThan(..)

English is not my primary language so I don't know which of these
aliases would be better. Given that classes such as Instant and
OffsetDateTime also have comparison methods (isAfter, isBefore), I
think it would be useful if Duration had easy comparison methods. Of
course we have compareTo(..) but I always get confused what it actually
means when I get a positive or negative number. :)


A small tip when using compareTo, donĀ“t think about the numbers but the 
operation. Always compare to 0


  object1.compareTo(object2) > 0

==>

  object1 > object2


You only change the comparison operator, and it always means object1 
 object2




More comparison
---
   static Duration max(Duration d1, Duration d2)
   static Duration min(Duration d1, Duration d2)

Returns the longest resp. shortest of the two durations, where negative
durations are shorter than positive durations.

Side note: I have not found an easy method to obtain the max resp. min
values of elements which implement Comparable, I could only come up
with something like:

   Stream.of(Duration.ZERO, Duration.ofSeconds(1L))
 .max/min(Comparator.naturalOrder())
 .orElse(null);

It could be worthwile to add generic methods such as
   public static > T max(T... elements)
   public static > T min(T... elements)
in the Comparator class.

Disallowing negative value
--
Okay, this one is a bit more farfetched, but a method which would be
useful for my use case is to have an alternative of between() which is
never negative and just truncates to zero if it happens to be negative.

I'm measuring a duration between timestamps which come from different
systems which should be in sync with their NTP servers (and each
other), but there may still be some time dilation which could lead to
weird results making it look like the effect happened before the cause.

So I could see some use for a method like:

   Duration positiveOrZero()


Kind regards,

Dave Franken





Re: Some possible enhancements for java.time.Duration?

2021-06-15 Thread Stephen Colebourne
On Tue, 15 Jun 2021 at 15:16,  wrote:
> Comparison
> --
>   boolean isGreaterThan(Duration duration) / isLongerThan(..)
>   boolean isSmallerThan(Duration duration) / isShorterThan(..)
>
> English is not my primary language so I don't know which of these
> aliases would be better. Given that classes such as Instant and
> OffsetDateTime also have comparison methods (isAfter, isBefore), I
> think it would be useful if Duration had easy comparison methods. Of
> course we have compareTo(..) but I always get confused what it actually
> means when I get a positive or negative number. :)

Maybe. Classes like OffsetDateTime have such methods because equals()
and compareTo() have different definitions. My gut feeling is that
this should not be done until Valhalla project has decided what if
anything it will do with the need for operator overloading on value
types.


> More comparison
> ---
>   static Duration max(Duration d1, Duration d2)
>   static Duration min(Duration d1, Duration d2)

I don't see the point of this. If such methods are to be added, they
should be generic, taking any Comparable


> Disallowing negative value
> --
> Okay, this one is a bit more farfetched, but a method which would be
> useful for my use case is to have an alternative of between() which is
> never negative and just truncates to zero if it happens to be negative.
>
> I'm measuring a duration between timestamps which come from different
> systems which should be in sync with their NTP servers (and each
> other), but there may still be some time dilation which could lead to
> weird results making it look like the effect happened before the cause.

I understand the use case, but it is too marginal for Duration IMO.

thanks
Stephen


Some possible enhancements for java.time.Duration?

2021-06-15 Thread dfranken . jdk
Dear readers,

I think java.time.Duration is a pretty useful class and after having to
work a lot with it, I recognized there might be some enhancements we
could make (where 'enhancement' is a subjective term of course).

For instance:

Comparison
--
  boolean isGreaterThan(Duration duration) / isLongerThan(..)
  boolean isSmallerThan(Duration duration) / isShorterThan(..)

English is not my primary language so I don't know which of these
aliases would be better. Given that classes such as Instant and
OffsetDateTime also have comparison methods (isAfter, isBefore), I
think it would be useful if Duration had easy comparison methods. Of
course we have compareTo(..) but I always get confused what it actually
means when I get a positive or negative number. :)

More comparison
---
  static Duration max(Duration d1, Duration d2)
  static Duration min(Duration d1, Duration d2)

Returns the longest resp. shortest of the two durations, where negative
durations are shorter than positive durations.

Side note: I have not found an easy method to obtain the max resp. min
values of elements which implement Comparable, I could only come up
with something like:

  Stream.of(Duration.ZERO, Duration.ofSeconds(1L))
.max/min(Comparator.naturalOrder())
.orElse(null);

It could be worthwile to add generic methods such as
  public static > T max(T... elements)
  public static > T min(T... elements)
in the Comparator class.

Disallowing negative value
--
Okay, this one is a bit more farfetched, but a method which would be
useful for my use case is to have an alternative of between() which is
never negative and just truncates to zero if it happens to be negative.

I'm measuring a duration between timestamps which come from different
systems which should be in sync with their NTP servers (and each
other), but there may still be some time dilation which could lead to
weird results making it look like the effect happened before the cause.

So I could see some use for a method like:

  Duration positiveOrZero()


Kind regards,

Dave Franken