8171382: java.time.Duration missing isPositive method

Reviewed-by: rriggs, joehw, iris, bpb, scolebourne
This commit is contained in:
Naoto Sato 2021-07-26 16:33:16 +00:00
parent ee5536183a
commit efa63dc1c6
2 changed files with 31 additions and 1 deletions
src/java.base/share/classes/java/time
test/jdk/java/time/tck/java/time

@ -582,6 +582,20 @@ public final class Duration
}
//-----------------------------------------------------------------------
/**
* 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,6 +908,20 @@ public class TCKDuration extends AbstractTCKTest {
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);
@ -915,9 +929,11 @@ public class TCKDuration extends AbstractTCKTest {
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);
}
//-----------------------------------------------------------------------