arrow-refined-types / arrow.refinement.time / Millisecond

Millisecond

class Millisecond

Millisecond constrains an Int to be in range of 0..999

Safe nullable construction

import arrow.refinement.time.Millisecond

Millisecond.orNull(2)
// Millisecond(value=2)
Millisecond.orNull(-1)

Resolved constraints

Millisecond.constraints(2)
// [(true, 2 should be in the closed range of 0..999 to be a valid millisecond of second number)]
Millisecond.constraints(-1)
// [(false, -1 should be in the closed range of 0..999 to be a valid millisecond of second number)]
Millisecond.isValid(2)
// true
Millisecond.isValid(-1)
// false

Folding validation

Millisecond.fold(2, { "failed: $it" }, { "success: $it" })
// success: Millisecond(value=2)
Millisecond.fold(-1, { "failed: $it" }, { "success: $it" })
// failed: [(false, -1 should be in the closed range of 0..999 to be a valid millisecond of second number)]

Unsafe require

Millisecond.require(2)
// 2
try { Millisecond.require(-1) } catch (e: IllegalArgumentException) { e.message }
// -1 should be in the closed range of 0..999 to be a valid millisecond of second number

Types

Companion companion object Companion : Refined<Int, Millisecond>

Properties

value val value: Int

Do you like Arrow?

Arrow Org
<