All Downloads are FREE. Search and download functionalities are using the official Maven repository.

dev.forkhandles.values.extensions.kt Maven / Gradle / Ivy

The newest version!
package dev.forkhandles.values

import java.io.File
import java.math.BigDecimal
import java.math.BigInteger
import java.time.Clock
import java.time.Clock.systemUTC
import java.time.Duration
import java.time.Instant
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.LocalTime
import java.time.OffsetDateTime
import java.time.OffsetTime
import java.time.ZoneId
import java.time.ZoneOffset.UTC
import java.time.ZonedDateTime
import java.time.temporal.Temporal
import java.util.UUID
import kotlin.random.Random

val > IntValueFactory.ZERO get() = of(0)
fun > IntValueFactory.random(random: Random = Random) = of(random.nextInt())
fun > IntValueFactory.plus(v1: DOMAIN, v2: DOMAIN): DOMAIN = of(v1.value + v2.value)
fun > IntValueFactory.minus(v1: DOMAIN, v2: DOMAIN): DOMAIN = of(v1.value - v2.value)
fun > IntValueFactory.times(v1: DOMAIN, v2: DOMAIN): DOMAIN = of(v1.value * v2.value)

val > LongValueFactory.ZERO get() = of(0)
fun > LongValueFactory.random(random: Random = Random) = of(random.nextLong())
fun > LongValueFactory.plus(v1: DOMAIN, v2: DOMAIN): DOMAIN = of(v1.value + v2.value)
fun > LongValueFactory.minus(v1: DOMAIN, v2: DOMAIN): DOMAIN = of(v1.value - v2.value)
fun > LongValueFactory.times(v1: DOMAIN, v2: DOMAIN): DOMAIN = of(v1.value * v2.value)

val > DoubleValueFactory.ZERO get() = of(0.0)
fun > DoubleValueFactory.random(random: Random = Random) = of(random.nextDouble())
fun > DoubleValueFactory.plus(v1: DOMAIN, v2: DOMAIN): DOMAIN = of(v1.value + v2.value)
fun > DoubleValueFactory.minus(v1: DOMAIN, v2: DOMAIN): DOMAIN = of(v1.value - v2.value)
fun > DoubleValueFactory.times(v1: DOMAIN, v2: DOMAIN): DOMAIN = of(v1.value * v2.value)

val > FloatValueFactory.ZERO get() = of(0.0f)
fun > FloatValueFactory.random(random: Random = Random) = of(random.nextFloat())
fun > FloatValueFactory.plus(v1: DOMAIN, v2: DOMAIN): DOMAIN = of(v1.value + v2.value)
fun > FloatValueFactory.minus(v1: DOMAIN, v2: DOMAIN): DOMAIN = of(v1.value - v2.value)
fun > FloatValueFactory.times(v1: DOMAIN, v2: DOMAIN): DOMAIN = of(v1.value * v2.value)

val > BigIntegerValueFactory.ZERO get() = of(BigInteger.ZERO)
fun > BigIntegerValueFactory.random(random: Random = Random) = of(BigInteger(random.nextLong().toString()))

fun > BigIntegerValueFactory.plus(v1: DOMAIN, v2: DOMAIN) = of(v1.value + v2.value)
fun > BigIntegerValueFactory.minus(v1: DOMAIN, v2: DOMAIN) = of(v1.value - v2.value)
fun > BigIntegerValueFactory.times(v1: DOMAIN, v2: DOMAIN) = of(v1.value * v2.value)

val > BigDecimalValueFactory.ZERO get() = of(BigDecimal.ZERO)
fun > BigDecimalValueFactory.random(random: Random = Random) =
    of(BigDecimal(random.nextDouble()))

fun > BigDecimalValueFactory.plus(v1: DOMAIN, v2: DOMAIN) = of(v1.value + v2.value)
fun > BigDecimalValueFactory.minus(v1: DOMAIN, v2: DOMAIN) = of(v1.value - v2.value)
fun > BigDecimalValueFactory.times(v1: DOMAIN, v2: DOMAIN) = of(v1.value * v2.value)

fun > BooleanValueFactory.random(random: Random = Random) = of(random.nextBoolean())

val > UUIDValueFactory.ZERO get() = of(0, 0)
fun > UUIDValueFactory.of(first: Long = 0, second: Long = 0) = of(UUID(first, second))
fun > UUIDValueFactory.random(random: Random = Random) = of(random.nextLong(), random.nextLong())

val > LocalDateValueFactory.EPOCH get() = of(LocalDate.EPOCH)
val > LocalDateValueFactory.MAX get() = of(LocalDate.MAX)
fun > LocalDateValueFactory.now(clock: Clock = systemUTC()) =
    of(LocalDate.now(clock))

@JvmName("isBeforeLocalDate")
fun > DOMAIN.isBefore(second: DOMAIN): Boolean = value < second.value
@JvmName("isAfterLocalDate")
fun > DOMAIN.isAfter(second: DOMAIN): Boolean = value > second.value

val > LocalDateTimeValueFactory.EPOCH
    get() = of(LocalDateTime.ofEpochSecond(0, 0, UTC))
val > LocalDateTimeValueFactory.MAX
    get() = of(LocalDateTime.ofEpochSecond(0, 0, UTC))

fun > LocalDateTimeValueFactory.now(clock: Clock = systemUTC()) =
    of(LocalDateTime.now(clock))

@JvmName("isBeforeLocalDateTime")
fun > DOMAIN.isBefore(second: DOMAIN): Boolean = value < second.value
@JvmName("isAfterLocalDateTime")
fun > DOMAIN.isAfter(second: DOMAIN): Boolean = value > second.value

val > ZonedDateTimeValueFactory.EPOCH
    get() = of(ZonedDateTime.ofInstant(Instant.EPOCH, ZoneId.of("UTC")))
val > ZonedDateTimeValueFactory.MAX
    get() = of(ZonedDateTime.ofInstant(Instant.MAX, ZoneId.of("UTC")))

@JvmName("isBeforeZonedDateTime")
fun > ZonedDateTimeValueFactory.now(clock: Clock = systemUTC()) =
    of(ZonedDateTime.now(clock))

@JvmName("isAfterZonedDateTime")
fun > DOMAIN.isBefore(second: DOMAIN) = value < second.value
fun > DOMAIN.isAfter(second: DOMAIN) = value > second.value

val > OffsetDateTimeValueFactory.EPOCH
    get() = of(OffsetDateTime.ofInstant(Instant.EPOCH, ZoneId.of("UTC")))

val > OffsetDateTimeValueFactory.MAX
    get() = of(OffsetDateTime.ofInstant(Instant.MAX, ZoneId.of("UTC")))

fun > OffsetDateTimeValueFactory.now(clock: Clock = systemUTC()) =
    of(OffsetDateTime.now(clock))

@JvmName("isBeforeOffsetDateTime")
fun > DOMAIN.isBefore(second: DOMAIN): Boolean = value < second.value
@JvmName("isAfterOffsetDateTime")
fun > DOMAIN.isAfter(second: DOMAIN): Boolean = value > second.value

val > InstantValueFactory.EPOCH get() = of(Instant.EPOCH)
val > InstantValueFactory.MAX get() = of(Instant.MAX)
fun > InstantValueFactory.now(clock: Clock = systemUTC()) = of(Instant.now(clock))
@JvmName("isBeforeInstant")
fun > DOMAIN.isBefore(second: DOMAIN): Boolean = value < second.value
@JvmName("isAfterInstant")
fun > DOMAIN.isAfter(second: DOMAIN): Boolean = value > second.value

fun  Value.between(that: Value) = Duration.between(value, that.value)

fun > LocalTimeValueFactory.now(clock: Clock = systemUTC()) = of(LocalTime.now(clock))

val > LocalTimeValueFactory.MIDNIGHT get() = of(LocalTime.MIDNIGHT)
@JvmName("isBeforeLocalTime")
fun > DOMAIN.isBefore(second: DOMAIN) = value < second.value
@JvmName("isAfterLocalTime")
fun > DOMAIN.isAfter(second: DOMAIN) = value > second.value

fun > OffsetTimeValueFactory.now(clock: Clock = systemUTC()) =
    of(OffsetTime.now(clock))

@JvmName("isBeforeOffsetTime")
fun > DOMAIN.isBefore(second: DOMAIN) = value < second.value
@JvmName("isAfterOffsetTime")
fun > DOMAIN.isAfter(second: DOMAIN) = value > second.value

val > FileValueFactory.USER_HOME get() = of(File(System.getProperty("user.home")))
val > FileValueFactory.ROOT get() = of(File("/"))
val > FileValueFactory.WORKING_DIR get() = of(File("."))





© 2015 - 2024 Weber Informatics LLC | Privacy Policy