Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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 efa63dc
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
14 changes: 14 additions & 0 deletions src/java.base/share/classes/java/time/Duration.java
Expand Up @@ -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>
Expand Down
18 changes: 17 additions & 1 deletion test/jdk/java/time/tck/java/time/TCKDuration.java
@@ -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
Expand Down Expand Up @@ -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);
}

//-----------------------------------------------------------------------
Expand Down

1 comment on commit efa63dc

@openjdk-notifier
Copy link

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.