8171382: java.time.Duration missing isPositive method
Reviewed-by: rriggs, joehw, iris, bpb, scolebourne
This commit is contained in:
parent
ee5536183a
commit
efa63dc1c6
@ -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.
|
* Checks if this duration is zero length.
|
||||||
* <p>
|
* <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.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* 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);
|
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
|
@Test
|
||||||
public void test_isNegative() {
|
public void test_isNegative() {
|
||||||
assertEquals(Duration.ofNanos(0).isNegative(), false);
|
assertEquals(Duration.ofNanos(0).isNegative(), false);
|
||||||
@ -915,9 +929,11 @@ public class TCKDuration extends AbstractTCKTest {
|
|||||||
assertEquals(Duration.ofNanos(1).isNegative(), false);
|
assertEquals(Duration.ofNanos(1).isNegative(), false);
|
||||||
assertEquals(Duration.ofSeconds(1).isNegative(), false);
|
assertEquals(Duration.ofSeconds(1).isNegative(), false);
|
||||||
assertEquals(Duration.ofSeconds(1, 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.ofNanos(-1).isNegative(), true);
|
||||||
assertEquals(Duration.ofSeconds(-1).isNegative(), true);
|
assertEquals(Duration.ofSeconds(-1).isNegative(), true);
|
||||||
assertEquals(Duration.ofSeconds(-1, -1).isNegative(), true);
|
assertEquals(Duration.ofSeconds(-1, -1).isNegative(), true);
|
||||||
|
assertEquals(Duration.ofSeconds(Long.MIN_VALUE).isNegative(), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
//-----------------------------------------------------------------------
|
//-----------------------------------------------------------------------
|
||||||
|
Loading…
x
Reference in New Issue
Block a user