From 4c94abb18b169175c9b85112a5b09edc756fc71e Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Wed, 20 Sep 2017 13:19:55 -0700 Subject: [PATCH] Create the NumberRangeBuilder utility class. --- .../nwapw/abacus/number/NumberRangeBuilder.kt | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 core/src/main/kotlin/org/nwapw/abacus/number/NumberRangeBuilder.kt diff --git a/core/src/main/kotlin/org/nwapw/abacus/number/NumberRangeBuilder.kt b/core/src/main/kotlin/org/nwapw/abacus/number/NumberRangeBuilder.kt new file mode 100644 index 0000000..c0ed09c --- /dev/null +++ b/core/src/main/kotlin/org/nwapw/abacus/number/NumberRangeBuilder.kt @@ -0,0 +1,24 @@ +package org.nwapw.abacus.number + +import org.nwapw.abacus.Abacus + +/** + * A utility class for creating [NumberRange] instances. + * + * Unlike a regular [ClosedRange], a NumberRange must have a third parameter, + * which is the [Abacus] instance that is used for promotion. However, the ".." operator + * is infix, and can only take two parameters. The solution is, instead of returning instances + * of NumberRange directly, to return a builder, which then provides a [with] infix function + * to attach it to an instance of Abacus. + * @property start the beginning of the range. + * @property endInclusive the end of the range. + */ +class NumberRangeBuilder(private val start: NumberInterface, private val endInclusive: NumberInterface) { + + /** + * Generate a [NumberRange] with the given instance of [abacus]. + * @return a new range with the given instance of Abacus. + */ + infix fun with(abacus: Abacus) = NumberRange(abacus, start, endInclusive) + +}