diff --git a/core/src/main/kotlin/org/nwapw/abacus/number/NumberRange.kt b/core/src/main/kotlin/org/nwapw/abacus/number/NumberRange.kt new file mode 100644 index 0000000..6adfc65 --- /dev/null +++ b/core/src/main/kotlin/org/nwapw/abacus/number/NumberRange.kt @@ -0,0 +1,28 @@ +package org.nwapw.abacus.number + +import org.nwapw.abacus.Abacus + +/** + * A closed range designed specifically for [NumberInterface] + * + * Besides providing the usual functionality of a [ClosedRange], this range + * also handles promotion - that is, it's safe to use it with numbers of different + * implementations, even as starting and ending points. + * + * @property abacus the abacus instance used for promotion. + * @property start the starting point of the range. + * @property endInclusive the ending point of the range. + */ +class NumberRange(val abacus: Abacus, + override val start: NumberInterface, + override val endInclusive: NumberInterface): ClosedRange { + + override operator fun contains(value: NumberInterface): Boolean { + val promotionResult = abacus.promotionManager.promote(start, endInclusive, value) + val newStart = promotionResult.items[0] + val newEnd = promotionResult.items[1] + val newValue = promotionResult.items[2] + return newValue >= newStart && newValue <= newEnd + } + +} \ No newline at end of file