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

add methods to get finite duration from duration #8832

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 8 additions & 1 deletion core-tests/shared/src/test/scala/zio/DurationSpec.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package zio

import zio.test.Assertion.{equalTo, not}
import zio.test.Assertion.{equalTo, not, throwsA}
import zio.test.assert

import java.time.temporal.ChronoUnit
import java.time.{Duration => JavaDuration}
import java.util.concurrent.TimeUnit
Expand Down Expand Up @@ -156,6 +157,12 @@ object DurationSpec extends ZIOBaseSpec {
test("It converts into the infinite s.c.d.Duration") {
assert(Duration.Infinity.asScala)(equalTo(ScalaDuration.Inf: ScalaDuration))
},
test("It converts into None for asFiniteDuration") {
assert(Duration.Infinity.asFiniteDuration)(equalTo(None))
},
test("It throws an exception for asFiniteDurationUnsafe") {
assert(Duration.Infinity.asFiniteDurationUnsafe)(throwsA[IllegalArgumentException])
},
test("It converts into a Long.MaxValue second-long JDK Duration") {
assert(Duration.Infinity)(equalTo(JavaDuration.ofNanos(Long.MaxValue)))
},
Expand Down
13 changes: 13 additions & 0 deletions core/shared/src/main/scala/zio/Duration.scala
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,19 @@ final class DurationOps(private val duration: Duration) extends AnyVal {
case _ => ScalaFiniteDuration(duration.toNanos, TimeUnit.NANOSECONDS)
}

def asFiniteDuration: Option[ScalaFiniteDuration] = duration match {
case Duration.Infinity => None
case Duration.Zero => Some(ScalaDuration.Zero)
case _ => Some(ScalaFiniteDuration(duration.toNanos, TimeUnit.NANOSECONDS))
}

def asFiniteDurationUnsafe: ScalaFiniteDuration = duration match {
Copy link
Member

Choose a reason for hiding this comment

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

I don't think it's necessary to throw, because if you look at "Infinity", it's just a "really big" (biggest possible) finite duration. So I think this can and should be a total function.

Copy link
Member

Choose a reason for hiding this comment

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

Btw I would also consider simply changing def asScala: ScalaDuration to def asScala: ScalaFiniteDuration. This way no new methods are necessary and it should be backward compatible.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will do

case Duration.Infinity =>
throw new IllegalArgumentException("Duration is infinite but finite duration was expected")
case Duration.Zero => ScalaDuration.Zero
case _ => ScalaFiniteDuration(duration.toNanos, TimeUnit.NANOSECONDS)
}

def asJava: JavaDuration = duration

def max(other: Duration): Duration = if (duration > other) duration else other
Expand Down