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

commonMain.io.islandtime.measures._Decades.kt Maven / Gradle / Ivy

The newest version!
//
// This file is auto-generated by 'tools:code-generator'
//
@file:JvmMultifileClass
@file:JvmName("DecadesKt")

package io.islandtime.measures

import dev.erikchristensen.javamath2kmp.minusExact
import dev.erikchristensen.javamath2kmp.negateExact
import dev.erikchristensen.javamath2kmp.plusExact
import dev.erikchristensen.javamath2kmp.timesExact
import dev.erikchristensen.javamath2kmp.toIntExact
import io.islandtime.internal.DECADES_PER_CENTURY
import io.islandtime.internal.MONTHS_PER_DECADE
import io.islandtime.internal.YEARS_PER_DECADE
import kotlin.Boolean
import kotlin.Comparable
import kotlin.Int
import kotlin.Long
import kotlin.PublishedApi
import kotlin.String
import kotlin.jvm.JvmMultifileClass
import kotlin.jvm.JvmName
import kotlin.math.absoluteValue

/**
 * A number of decades.
 */
inline class IntDecades(
  /**
   * The underlying value.
   */
  val value: Int
) : Comparable {
  /**
   * The absolute value of this duration.
   * @throws ArithmeticException if overflow occurs
   */
  val absoluteValue: IntDecades
    get() = if (value < 0) -this else this
  /**
   * Converts this duration to months.
   * @throws ArithmeticException if overflow occurs
   */
  val inMonths: IntMonths
    get() = (value timesExact MONTHS_PER_DECADE).months

  /**
   * Converts this duration to months without checking for overflow.
   */
  internal val inMonthsUnchecked: IntMonths
    get() = (value * MONTHS_PER_DECADE).months

  /**
   * Converts this duration to years.
   * @throws ArithmeticException if overflow occurs
   */
  val inYears: IntYears
    get() = (value timesExact YEARS_PER_DECADE).years

  /**
   * Converts this duration to years without checking for overflow.
   */
  internal val inYearsUnchecked: IntYears
    get() = (value * YEARS_PER_DECADE).years

  /**
   * Converts this duration to the number of whole centuries.
   */
  val inCenturies: IntCenturies
    get() = (value / DECADES_PER_CENTURY).centuries

  /**
   * Checks if this duration is zero.
   */
  fun isZero(): Boolean = value == 0

  /**
   * Checks if this duration is negative.
   */
  fun isNegative(): Boolean = value < 0

  /**
   * Checks if this duration is positive.
   */
  fun isPositive(): Boolean = value > 0

  override fun compareTo(other: IntDecades): Int = value.compareTo(other.value)

  /**
   * Converts this duration to an ISO-8601 time interval representation.
   */
  override fun toString(): String {
     return when (value) {
       0 -> "P0Y"
       Int.MIN_VALUE -> "-P2147483648Y"
       else -> buildString {
         if (value < 0) { append('-') }
         append("P")
         append(value.absoluteValue timesExact 10)
         append('Y')
       }
     }
  }

  /**
   * Negates this duration.
   * @throws ArithmeticException if overflow occurs
   */
  operator fun unaryMinus() = IntDecades(value.negateExact())

  /**
   * Negates this duration without checking for overflow.
   */
  internal fun negateUnchecked() = IntDecades(-value)

  /**
   * Multiplies this duration by a scalar value.
   * @throws ArithmeticException if overflow occurs
   */
  operator fun times(scalar: Int) = IntDecades(value timesExact scalar)

  /**
   * Multiplies this duration by a scalar value.
   * @throws ArithmeticException if overflow occurs
   */
  operator fun times(scalar: Long) = this.toLongDecades() * scalar

  /**
   * Divides this duration by a scalar value.
   * @throws ArithmeticException if overflow occurs or the scalar is zero
   */
  operator fun div(scalar: Int): IntDecades {
     return if (scalar == -1) {
       -this
     } else {
       IntDecades(value / scalar)
     }
  }

  /**
   * Divides this duration by a scalar value.
   * @throws ArithmeticException if the scalar is zero
   */
  operator fun div(scalar: Long): LongDecades = this.toLongDecades() / scalar
  operator fun rem(scalar: Int) = IntDecades(value % scalar)

  operator fun rem(scalar: Long) = this.toLongDecades() % scalar

  operator fun plus(months: IntMonths) = this.inMonths + months

  operator fun minus(months: IntMonths) = this.inMonths - months

  operator fun plus(months: LongMonths) = this.toLongDecades().inMonths + months

  operator fun minus(months: LongMonths) = this.toLongDecades().inMonths - months

  operator fun plus(years: IntYears) = this.inYears + years

  operator fun minus(years: IntYears) = this.inYears - years

  operator fun plus(years: LongYears) = this.toLongDecades().inYears + years

  operator fun minus(years: LongYears) = this.toLongDecades().inYears - years

  operator fun plus(decades: IntDecades) = IntDecades(value plusExact decades.value)

  operator fun minus(decades: IntDecades) = IntDecades(value minusExact decades.value)

  operator fun plus(decades: LongDecades) = LongDecades(value.toLong() plusExact decades.value)

  operator fun minus(decades: LongDecades) = LongDecades(value.toLong() minusExact decades.value)

  operator fun plus(centuries: IntCenturies) = this + centuries.inDecades

  operator fun minus(centuries: IntCenturies) = this - centuries.inDecades

  operator fun plus(centuries: LongCenturies) = this.toLongDecades() + centuries.inDecades

  operator fun minus(centuries: LongCenturies) = this.toLongDecades() - centuries.inDecades

  inline fun  toComponents(action: (centuries: IntCenturies, decades: IntDecades) -> T): T {
    val centuries = (value / DECADES_PER_CENTURY).centuries
    val decades = (value % DECADES_PER_CENTURY).decades
    return action(centuries, decades)
  }

  /**
   * Converts this duration to [LongDecades].
   */
  fun toLongDecades() = LongDecades(value.toLong())

  /**
   * Converts this duration to a `Long` value.
   */
  fun toLong() = value.toLong()

  companion object {
    /**
     * The smallest supported value.
     */
    val MIN: IntDecades = IntDecades(Int.MIN_VALUE)

    /**
     * The largest supported value.
     */
    val MAX: IntDecades = IntDecades(Int.MAX_VALUE)
  }
}

/**
 * Converts this value to a duration of decades.
 */
val Int.decades: IntDecades
  get() = IntDecades(this)

/**
 * Multiplies this value by a duration of decades.
 * @throws ArithmeticException if overflow occurs
 */
operator fun Int.times(decades: IntDecades) = decades * this

/**
 * Multiplies this value by a duration of decades.
 * @throws ArithmeticException if overflow occurs
 */
operator fun Long.times(decades: IntDecades) = decades * this

/**
 * A number of decades.
 */
inline class LongDecades(
  /**
   * The underlying value.
   */
  val value: Long
) : Comparable {
  /**
   * The absolute value of this duration.
   * @throws ArithmeticException if overflow occurs
   */
  val absoluteValue: LongDecades
    get() = if (value < 0) -this else this
  /**
   * Converts this duration to months.
   * @throws ArithmeticException if overflow occurs
   */
  val inMonths: LongMonths
    get() = (value timesExact MONTHS_PER_DECADE).months

  /**
   * Converts this duration to months without checking for overflow.
   */
  internal val inMonthsUnchecked: LongMonths
    get() = (value * MONTHS_PER_DECADE).months

  /**
   * Converts this duration to years.
   * @throws ArithmeticException if overflow occurs
   */
  val inYears: LongYears
    get() = (value timesExact YEARS_PER_DECADE).years

  /**
   * Converts this duration to years without checking for overflow.
   */
  internal val inYearsUnchecked: LongYears
    get() = (value * YEARS_PER_DECADE).years

  /**
   * Converts this duration to the number of whole centuries.
   */
  val inCenturies: LongCenturies
    get() = (value / DECADES_PER_CENTURY).centuries

  /**
   * Checks if this duration is zero.
   */
  fun isZero(): Boolean = value == 0L

  /**
   * Checks if this duration is negative.
   */
  fun isNegative(): Boolean = value < 0L

  /**
   * Checks if this duration is positive.
   */
  fun isPositive(): Boolean = value > 0L

  override fun compareTo(other: LongDecades): Int = value.compareTo(other.value)

  /**
   * Converts this duration to an ISO-8601 time interval representation.
   */
  override fun toString(): String {
     return when (value) {
       0L -> "P0Y"
       Long.MIN_VALUE -> "-P9223372036854775808Y"
       else -> buildString {
         if (value < 0) { append('-') }
         append("P")
         append(value.absoluteValue timesExact 10)
         append('Y')
       }
     }
  }

  /**
   * Negates this duration.
   * @throws ArithmeticException if overflow occurs
   */
  operator fun unaryMinus() = LongDecades(value.negateExact())

  /**
   * Negates this duration without checking for overflow.
   */
  internal fun negateUnchecked() = LongDecades(-value)

  /**
   * Multiplies this duration by a scalar value.
   * @throws ArithmeticException if overflow occurs
   */
  operator fun times(scalar: Int) = LongDecades(value timesExact scalar)

  /**
   * Multiplies this duration by a scalar value.
   * @throws ArithmeticException if overflow occurs
   */
  operator fun times(scalar: Long) = LongDecades(value timesExact scalar)

  /**
   * Divides this duration by a scalar value.
   * @throws ArithmeticException if overflow occurs or the scalar is zero
   */
  operator fun div(scalar: Int): LongDecades {
     return if (scalar == -1) {
       -this
     } else {
       LongDecades(value / scalar)
     }
  }

  /**
   * Divides this duration by a scalar value.
   * @throws ArithmeticException if overflow occurs or the scalar is zero
   */
  operator fun div(scalar: Long): LongDecades {
     return if (scalar == -1L) {
       -this
     } else {
       LongDecades(value / scalar)
     }
  }

  operator fun rem(scalar: Int) = LongDecades(value % scalar)

  operator fun rem(scalar: Long) = LongDecades(value % scalar)

  operator fun plus(months: IntMonths) = this.inMonths + months

  operator fun minus(months: IntMonths) = this.inMonths - months

  operator fun plus(months: LongMonths) = this.inMonths + months

  operator fun minus(months: LongMonths) = this.inMonths - months

  operator fun plus(years: IntYears) = this.inYears + years

  operator fun minus(years: IntYears) = this.inYears - years

  operator fun plus(years: LongYears) = this.inYears + years

  operator fun minus(years: LongYears) = this.inYears - years

  operator fun plus(decades: IntDecades) = LongDecades(value plusExact decades.value)

  operator fun minus(decades: IntDecades) = LongDecades(value minusExact decades.value)

  operator fun plus(decades: LongDecades) = LongDecades(value plusExact decades.value)

  operator fun minus(decades: LongDecades) = LongDecades(value minusExact decades.value)

  operator fun plus(centuries: IntCenturies) = this + centuries.inDecades

  operator fun minus(centuries: IntCenturies) = this - centuries.inDecades

  operator fun plus(centuries: LongCenturies) = this + centuries.inDecades

  operator fun minus(centuries: LongCenturies) = this - centuries.inDecades

  inline fun  toComponents(action: (centuries: LongCenturies, decades: IntDecades) -> T): T {
    val centuries = (value / DECADES_PER_CENTURY).centuries
    val decades = (value % DECADES_PER_CENTURY).toInt().decades
    return action(centuries, decades)
  }

  /**
   * Converts this duration to [IntDecades].
   * @throws ArithmeticException if overflow occurs
   */
  fun toIntDecades() = IntDecades(value.toIntExact())

  /**
   * Converts this duration to [IntDecades] without checking for overflow.
   */
  @PublishedApi
  internal fun toIntDecadesUnchecked() = IntDecades(value.toInt())

  /**
   * Converts this duration to an `Int` value.
   * @throws ArithmeticException if overflow occurs
   */
  fun toInt() = value.toIntExact()

  /**
   * Converts this duration to an `Int` value without checking for overflow.
   */
  internal fun toIntUnchecked() = value.toInt()

  companion object {
    /**
     * The smallest supported value.
     */
    val MIN: LongDecades = LongDecades(Long.MIN_VALUE)

    /**
     * The largest supported value.
     */
    val MAX: LongDecades = LongDecades(Long.MAX_VALUE)
  }
}

/**
 * Converts this value to a duration of decades.
 */
val Long.decades: LongDecades
  get() = LongDecades(this)

/**
 * Multiplies this value by a duration of decades.
 * @throws ArithmeticException if overflow occurs
 */
operator fun Int.times(decades: LongDecades) = decades * this

/**
 * Multiplies this value by a duration of decades.
 * @throws ArithmeticException if overflow occurs
 */
operator fun Long.times(decades: LongDecades) = decades * this




© 2015 - 2025 Weber Informatics LLC | Privacy Policy