mirror of
https://github.com/DanilaFe/abacus
synced 2024-12-22 15:30:09 -08:00
Implement a range that works for NumberInterfaces.
This commit is contained in:
parent
566598b702
commit
ba63dd7874
28
core/src/main/kotlin/org/nwapw/abacus/number/NumberRange.kt
Normal file
28
core/src/main/kotlin/org/nwapw/abacus/number/NumberRange.kt
Normal file
|
@ -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<NumberInterface> {
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user