From ba63dd787441c1949b17ff0484fa98ddfc2b94d6 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Wed, 20 Sep 2017 13:04:20 -0700 Subject: [PATCH] Implement a range that works for NumberInterfaces. --- .../org/nwapw/abacus/number/NumberRange.kt | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 core/src/main/kotlin/org/nwapw/abacus/number/NumberRange.kt 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