Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8171382: java.time.Duration missing isPositive method #4892

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;
Copy link
Contributor

Choose a reason for hiding this comment

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

I had to think whether this logic is correct, but I believe it is because nanos is 32 bits and positive so won't impact the negative bit of seconds.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks, Stephen. Yes, nanos is even smaller, less than 1,000,000,000.

}

/**
* 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