|
| 1 | +package org.sdkotlin.integral |
| 2 | + |
| 3 | +@JvmInline |
| 4 | +value class Octal internal constructor( |
| 5 | + internal val data: Int, |
| 6 | +) : Integral<Octal> { |
| 7 | + |
| 8 | + companion object { |
| 9 | + |
| 10 | + val MIN_VALUE = Octal(Int.MIN_VALUE) |
| 11 | + val MAX_VALUE = Octal(Int.MAX_VALUE) |
| 12 | + val NEGATIVE_ONE = Octal(-1) |
| 13 | + val ZERO = Octal(0) |
| 14 | + val ONE = Octal(1) |
| 15 | + |
| 16 | + internal fun octalRemainder(v1: Octal, v2: Octal): Octal = |
| 17 | + Octal((v1.toLong() % v2.toLong()).toInt()) |
| 18 | + } |
| 19 | + |
| 20 | + internal constructor(octalValue: String) : this(octalValue.toInt(8)) |
| 21 | + |
| 22 | + override val minValue: Octal get() = MIN_VALUE |
| 23 | + override val maxValue: Octal get() = MAX_VALUE |
| 24 | + override val zero: Octal get() = ZERO |
| 25 | + override val one: Octal get() = ONE |
| 26 | + |
| 27 | + override operator fun plus(other: Octal): Octal = |
| 28 | + Octal(data.plus(other.data)) |
| 29 | + |
| 30 | + override operator fun minus(other: Octal): Octal = |
| 31 | + Octal(data.minus(other.data)) |
| 32 | + |
| 33 | + override operator fun unaryMinus(): Octal = |
| 34 | + Octal(-data) |
| 35 | + |
| 36 | + override operator fun rem(other: Octal): Octal = |
| 37 | + octalRemainder(this, other) |
| 38 | + |
| 39 | + fun mod(other: Octal): Octal = rem(other) |
| 40 | + |
| 41 | + override fun compareTo(other: Octal): Int = |
| 42 | + data.compareTo(other.data) |
| 43 | + |
| 44 | + operator fun rangeTo(other: Octal): ClosedRange<Octal> = |
| 45 | + IntegralRange(this, other) |
| 46 | + |
| 47 | + operator fun rangeUntil(other: Octal): OpenEndRange<Octal> = |
| 48 | + IntegralRange(this, other - other.one) |
| 49 | + |
| 50 | + fun toUInt(): UInt = data.toUInt() |
| 51 | + |
| 52 | + fun toInt(): Int = data |
| 53 | + |
| 54 | + fun toLong(): Long = data.toLong() |
| 55 | + |
| 56 | + fun toUOctal(): UOctal = UOctal(data) |
| 57 | + |
| 58 | + override fun toString() = "%o".format(data) |
| 59 | +} |
| 60 | + |
| 61 | +fun Int.toOctal(): Octal = Octal(this) |
| 62 | + |
| 63 | +fun String.toOctal(): Octal = Octal(this) |
| 64 | + |
| 65 | +infix fun Octal.downTo(to: Octal): IntegralProgression<Octal> = |
| 66 | + IntegralProgression.fromClosedRange(this, to, Octal.NEGATIVE_ONE) |
0 commit comments