Skip to content
Permalink
Browse files
8171382: java.time.Duration missing isPositive method
Reviewed-by: rriggs, joehw, iris, bpb, scolebourne
  • Loading branch information
naotoj committed Jul 26, 2021
1 parent ee55361 commit efa63dc1c64db357eeb497d2e1fefd170ca22d98
Showing 2 changed files with 31 additions and 1 deletion.
@@ -582,6 +582,20 @@ private static class DurationUnits {
}

//-----------------------------------------------------------------------
/**
* Checks if this duration is positive, excluding zero.
* <p>
* A {@code Duration} represents a directed distance between two points on
* the time-line and can therefore be positive, zero or negative.
* This method checks whether the length is greater than zero.
*
* @return true if this duration has a total length greater than zero
* @since 18
*/
public boolean isPositive() {
return (seconds | nanos) > 0;
}

/**
* Checks if this duration is zero length.
* <p>
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -908,16 +908,32 @@ public void test_isZero() {
assertEquals(Duration.ofSeconds(-1, -1).isZero(), false);
}

@Test
public void test_isPositive() {
assertEquals(Duration.ofNanos(0).isPositive(), false);
assertEquals(Duration.ofSeconds(0).isPositive(), false);
assertEquals(Duration.ofNanos(1).isPositive(), true);
assertEquals(Duration.ofSeconds(1).isPositive(), true);
assertEquals(Duration.ofSeconds(1, 1).isPositive(), true);
assertEquals(Duration.ofSeconds(Long.MAX_VALUE, 999_999_999).isPositive(), true);
assertEquals(Duration.ofNanos(-1).isPositive(), false);
assertEquals(Duration.ofSeconds(-1).isPositive(), false);
assertEquals(Duration.ofSeconds(-1, -1).isPositive(), false);
assertEquals(Duration.ofSeconds(Long.MIN_VALUE).isPositive(), false);
}

@Test
public void test_isNegative() {
assertEquals(Duration.ofNanos(0).isNegative(), false);
assertEquals(Duration.ofSeconds(0).isNegative(), false);
assertEquals(Duration.ofNanos(1).isNegative(), false);
assertEquals(Duration.ofSeconds(1).isNegative(), false);
assertEquals(Duration.ofSeconds(1, 1).isNegative(), false);
assertEquals(Duration.ofSeconds(Long.MAX_VALUE, 999_999_999).isNegative(), false);
assertEquals(Duration.ofNanos(-1).isNegative(), true);
assertEquals(Duration.ofSeconds(-1).isNegative(), true);
assertEquals(Duration.ofSeconds(-1, -1).isNegative(), true);
assertEquals(Duration.ofSeconds(Long.MIN_VALUE).isNegative(), true);
}

//-----------------------------------------------------------------------

1 comment on commit efa63dc

@openjdk-notifier
Copy link

@openjdk-notifier openjdk-notifier bot commented on efa63dc Jul 26, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.