@@ -653,7 +653,7 @@ private inline fun unaryFunction(
653653 * - Short circuits UNSET and ERROR parameter to return ERROR.
654654 * - Short circuits NULL [Value] parameter to return NULL [Value].
655655 * - Extracts Boolean for [function] evaluation.
656- * - All other [Value] types return ERROR.
656+ * - All other parameter types return ERROR.
657657 * - Catches evaluation exceptions and returns them as an ERROR.
658658 */
659659@JvmName(" unaryBooleanFunction" )
@@ -671,20 +671,22 @@ private inline fun unaryFunction(crossinline function: (Boolean) -> EvaluateResu
671671 * - Short circuits NULL [Value] parameter to return NULL [Value].
672672 * - Extracts Boolean for [function] evaluation.
673673 * - Wraps the primitive String result as [EvaluateResult].
674- * - All other [Value] types return ERROR.
674+ * - All other parameter types return ERROR.
675675 * - Catches evaluation exceptions and returns them as an ERROR.
676676 */
677677@JvmName(" unaryStringFunctionPrimitive" )
678678private inline fun unaryFunctionPrimitive (crossinline function : (String ) -> String ) =
679- unaryFunction { s: String -> EvaluateResult .string(function(s)) }
679+ unaryFunction { s: String ->
680+ EvaluateResult .string(function(s))
681+ }
680682
681683/* *
682684 * Unary String Function
683685 * - Validates there is exactly 1 parameter.
684686 * - Short circuits UNSET and ERROR parameter to return ERROR.
685687 * - Short circuits NULL [Value] parameter to return NULL [Value].
686688 * - Extracts String for [function] evaluation.
687- * - All other [Value] types return ERROR.
689+ * - All other parameter types return ERROR.
688690 * - Catches evaluation exceptions and returns them as an ERROR.
689691 */
690692@JvmName(" unaryStringFunction" )
@@ -701,7 +703,7 @@ private inline fun unaryFunction(crossinline function: (String) -> EvaluateResul
701703 * - Short circuits UNSET and ERROR parameter to return ERROR.
702704 * - Short circuits NULL [Value] parameter to return NULL [Value].
703705 * - Extracts String for [function] evaluation.
704- * - All other [Value] types return ERROR.
706+ * - All other parameter types return ERROR.
705707 * - Catches evaluation exceptions and returns them as an ERROR.
706708 */
707709@JvmName(" unaryLongFunction" )
@@ -718,7 +720,7 @@ private inline fun unaryFunction(crossinline function: (Long) -> EvaluateResult)
718720 * - Short circuits UNSET and ERROR parameter to return ERROR.
719721 * - Short circuits NULL [Value] parameter to return NULL [Value].
720722 * - Extracts Timestamp for [function] evaluation.
721- * - All other [Value] types return ERROR.
723+ * - All other parameter types return ERROR.
722724 * - Catches evaluation exceptions and returns them as an ERROR.
723725 */
724726@JvmName(" unaryTimestampFunction" )
@@ -733,9 +735,10 @@ private inline fun unaryFunction(crossinline function: (Timestamp) -> EvaluateRe
733735 * Unary Timestamp Function
734736 * - Validates there is exactly 1 parameter.
735737 * - Short circuits UNSET and ERROR parameter to return ERROR.
736- * - Short circuits NULL [Value] parameter to return NULL [Value], however NULL [Value]s can appear inside of array.
738+ * - Short circuits NULL [Value] parameter to return NULL [Value], however NULL [Value]s can appear
739+ * inside of array.
737740 * - Extracts Timestamp from [Value] for evaluation.
738- * - All other [Value] types return ERROR.
741+ * - All other parameter types return ERROR.
739742 * - Catches evaluation exceptions and returns them as an ERROR.
740743 */
741744@JvmName(" unaryArrayFunction" )
@@ -751,9 +754,9 @@ private inline fun unaryFunction(crossinline longOp: (List<Value>) -> EvaluateRe
751754 * - Validates there is exactly 1 parameter.
752755 * - Short circuits UNSET and ERROR parameter to return ERROR.
753756 * - Short circuits NULL [Value] parameter to return NULL [Value].
754- * - Depending on [Value] type, either the Timestamp or String is extracted and evaluated by
755- * either [byteOp] or [stringOp].
756- * - All other [Value] types return ERROR.
757+ * - Depending on [Value] type, either the Timestamp or String is extracted and evaluated by either
758+ * [byteOp] or [stringOp].
759+ * - All other parameter types return ERROR.
757760 * - Catches evaluation exceptions and returns them as an ERROR.
758761 */
759762private inline fun unaryFunction (
@@ -775,7 +778,7 @@ private inline fun unaryFunction(
775778 * - Short circuits UNSET and ERROR parameter to return ERROR.
776779 * - Short circuits NULL [Value] parameter to return NULL [Value].
777780 * - If [Value] type is [valueTypeCase] then use [valueExtractor] for [function] evaluation.
778- * - All other [Value] types return ERROR.
781+ * - All other parameter types return ERROR.
779782 * - Catches evaluation exceptions and returns them as an ERROR.
780783 */
781784private inline fun <T > unaryFunctionType (
@@ -800,7 +803,7 @@ private inline fun <T> unaryFunctionType(
800803 * - Short circuits NULL [Value] parameter to return NULL [Value].
801804 * - If [Value] type is [valueTypeCase1] then use [valueExtractor1] for [function1] evaluation.
802805 * - If [Value] type is [valueTypeCase2] then use [valueExtractor2] for [function2] evaluation.
803- * - All other [Value] types return ERROR.
806+ * - All other parameter types return ERROR.
804807 * - Catches evaluation exceptions and returns them as an ERROR.
805808 */
806809private inline fun <T1 , T2 > unaryFunctionType (
@@ -852,9 +855,10 @@ private inline fun binaryFunction(
852855 * Binary (Map, String) Function
853856 * - Validates there is exactly 2 parameters.
854857 * - First, short circuits UNSET and ERROR parameters to return ERROR.
855- * - Second short circuits NULL [Value] parameters to return NULL [Value], however NULL [Value]s can appear inside of Map.
858+ * - Second short circuits NULL [Value] parameters to return NULL [Value], however NULL [Value]s can
859+ * appear inside of Map.
856860 * - Extracts Map and String for [function] evaluation.
857- * - All other [Value] types return ERROR.
861+ * - All other parameter types return ERROR.
858862 * - Catches evaluation exceptions and returns them as an ERROR.
859863 */
860864@JvmName(" binaryMapStringFunction" )
@@ -873,9 +877,10 @@ private inline fun binaryFunction(
873877 * Binary (Value, Array) Function
874878 * - Validates there is exactly 2 parameters.
875879 * - First, short circuits UNSET and ERROR parameters to return ERROR.
876- * - Second short circuits NULL [Value] parameters to return NULL [Value], however NULL [Value]s can appear inside of Array.
880+ * - Second short circuits NULL [Value] parameters to return NULL [Value], however NULL [Value]s can
881+ * appear inside of Array.
877882 * - Extracts Value and Array for [function] evaluation.
878- * - All other [Value] types return ERROR.
883+ * - All other parameter types return ERROR.
879884 * - Catches evaluation exceptions and returns them as an ERROR.
880885 */
881886@JvmName(" binaryValueArrayFunction" )
@@ -889,9 +894,10 @@ private inline fun binaryFunction(
889894 * Binary (Array, Value) Function
890895 * - Validates there is exactly 2 parameters.
891896 * - First, short circuits UNSET and ERROR parameters to return ERROR.
892- * - Second short circuits NULL [Value] parameters to return NULL [Value], however NULL [Value]s can appear inside of Array.
897+ * - Second short circuits NULL [Value] parameters to return NULL [Value], however NULL [Value]s can
898+ * appear inside of Array.
893899 * - Extracts Array and Value for [function] evaluation.
894- * - All other [Value] types return ERROR.
900+ * - All other parameter types return ERROR.
895901 * - Catches evaluation exceptions and returns them as an ERROR.
896902 */
897903@JvmName(" binaryArrayValueFunction" )
@@ -907,7 +913,7 @@ private inline fun binaryFunction(
907913 * - First, short circuits UNSET and ERROR parameters to return ERROR.
908914 * - Second short circuits NULL [Value] parameters to return NULL [Value].
909915 * - Extracts String and String for [function] evaluation.
910- * - All other [Value] types return ERROR.
916+ * - All other parameter types return ERROR.
911917 * - Catches evaluation exceptions and returns them as an ERROR.
912918 */
913919@JvmName(" binaryStringStringFunction" )
@@ -927,7 +933,7 @@ private inline fun binaryFunction(crossinline function: (String, String) -> Eval
927933 * - First, short circuits UNSET and ERROR parameters to return ERROR.
928934 * - Second short circuits NULL [Value] parameters to return NULL [Value].
929935 * - Extracts String and Regex via [patternConstructor] for [function] evaluation.
930- * - All other [Value] types return ERROR.
936+ * - All other parameter types return ERROR.
931937 * - Catches evaluation exceptions and returns them as an ERROR.
932938 */
933939@JvmName(" binaryStringPatternConstructorFunction" )
@@ -955,7 +961,7 @@ private inline fun binaryPatternConstructorFunction(
955961 * - Second short circuits NULL [Value] parameters to return NULL [Value].
956962 * - Extracts String and Regex for [function] evaluation.
957963 * - Caches previously seen Regex to avoid compilation overhead.
958- * - All other [Value] types return ERROR.
964+ * - All other parameter types return ERROR.
959965 * - Catches evaluation exceptions and returns them as an ERROR.
960966 */
961967@JvmName(" binaryStringPatternFunction" )
@@ -971,9 +977,7 @@ private inline fun binaryPatternFunction(crossinline function: (Pattern, String)
971977 function
972978 )
973979
974- /* *
975- * Simple one entry cache.
976- */
980+ /* * Simple one entry cache. */
977981private inline fun <T > cache (crossinline ifAbsent : (String ) -> T ): (String ) -> T ? {
978982 var cache: Pair <String ?, T ?> = Pair (null , null )
979983 return block@{ s: String ->
@@ -990,9 +994,10 @@ private inline fun <T> cache(crossinline ifAbsent: (String) -> T): (String) -> T
990994 * Binary (Array, Array) Function
991995 * - Validates there is exactly 2 parameters.
992996 * - First, short circuits UNSET and ERROR parameters to return ERROR.
993- * - Second short circuits NULL [Value] parameters to return NULL [Value], however NULL [Value]s can appear inside of Array.
997+ * - Second short circuits NULL [Value] parameters to return NULL [Value], however NULL [Value]s can
998+ * appear inside of Array.
994999 * - Extracts Array and Array for [function] evaluation.
995- * - All other [Value] types return ERROR.
1000+ * - All other parameter types return ERROR.
9961001 * - Catches evaluation exceptions and returns them as an ERROR.
9971002 */
9981003@JvmName(" binaryArrayArrayFunction" )
@@ -1015,7 +1020,7 @@ private inline fun binaryFunction(
10151020 * - First parameter must be [Value] of [valueTypeCase1].
10161021 * - Second parameter must be [Value] of [valueTypeCase2].
10171022 * - Extract parameter values via [valueExtractor1] and [valueExtractor2] for [function] evaluation.
1018- * - All other [Value] types return ERROR.
1023+ * - All other parameter types return ERROR.
10191024 * - Catches evaluation exceptions and returns them as an ERROR.
10201025 */
10211026private inline fun <T1 , T2 > binaryFunctionType (
@@ -1057,7 +1062,7 @@ private inline fun <T1, T2> binaryFunctionType(
10571062 * - First parameter must be [Value] of [valueTypeCase1].
10581063 * - Second parameter must be [Value] of [valueTypeCase2].
10591064 * - Extract parameter values via [valueExtractor1] and [valueExtractor2] for [function] evaluation.
1060- * - All other [Value] types return ERROR.
1065+ * - All other parameter types return ERROR.
10611066 * - Catches evaluation exceptions and returns them as an ERROR.
10621067 */
10631068private inline fun <T1 , T2 > binaryFunctionConstructorType (
@@ -1118,7 +1123,7 @@ private inline fun ternaryLazyFunction(
11181123 * - If 2nd parameter is NULL, short circuit and return ERROR.
11191124 * - If 1st or 3rd parameter is NULL, short circuit and return NULL.
11201125 * - Extracts Timestamp, String and Long for [function] evaluation.
1121- * - All other [Value] types return ERROR.
1126+ * - All other parameter types return ERROR.
11221127 * - Catches evaluation exceptions and returns them as an ERROR.
11231128 */
11241129private inline fun ternaryTimestampFunction (
@@ -1190,7 +1195,7 @@ private inline fun variadicNullableValueFunction(
11901195 * - First short circuits UNSET and ERROR parameters to return ERROR.
11911196 * - Second short circuits NULL [Value] parameters to return NULL [Value].
11921197 * - Extract String parameters into List for [function] evaluation.
1193- * - All other [Value] types return ERROR.
1198+ * - All other parameter types return ERROR.
11941199 * - Catches evaluation exceptions and returns them as an ERROR.
11951200 */
11961201@JvmName(" variadicStringFunction" )
@@ -1205,7 +1210,7 @@ private inline fun variadicFunction(
12051210 * - Second short circuits NULL [Value] parameters to return NULL [Value].
12061211 * - Parameter must be [Value] of [valueTypeCase].
12071212 * - Extract parameter values via [valueExtractor] into List for [function] evaluation.
1208- * - All other [Value] types return ERROR.
1213+ * - All other parameter types return ERROR.
12091214 * - Catches evaluation exceptions and returns them as an ERROR.
12101215 */
12111216private inline fun <T > variadicFunctionType (
@@ -1233,7 +1238,7 @@ private inline fun <T> variadicFunctionType(
12331238 * - First short circuits UNSET and ERROR parameters to return ERROR.
12341239 * - Second short circuits NULL [Value] parameters to return NULL [Value].
12351240 * - Extract String parameters into BooleanArray for [function] evaluation.
1236- * - All other [Value] types return ERROR.
1241+ * - All other parameter types return ERROR.
12371242 * - Catches evaluation exceptions and returns them as an ERROR.
12381243 */
12391244@JvmName(" variadicBooleanFunction" )
@@ -1270,6 +1275,17 @@ private inline fun comparison(crossinline f: (Value, Value) -> Boolean?): Evalua
12701275 else EvaluateResult .boolean(f(p1, p2))
12711276 }
12721277
1278+ /* *
1279+ * Unary (Number) Arithmetic Function
1280+ * - Validates there is exactly 1 parameter.
1281+ * - Short circuits UNSET and ERROR parameter to return ERROR.
1282+ * - Short circuits NULL [Value] parameter to return NULL [Value].
1283+ * - If parameter type is Integer then [intOp] will be used for evaluation.
1284+ * - If parameter type is Double then [doubleOp] will be used for evaluation.
1285+ * - All other parameter types return ERROR.
1286+ * - Primitive result is wrapped as EvaluateResult.
1287+ * - Catches evaluation exceptions and returns them as an ERROR.
1288+ */
12731289private inline fun arithmeticPrimitive (
12741290 crossinline intOp : (Long ) -> Long ,
12751291 crossinline doubleOp : (Double ) -> Double
@@ -1279,6 +1295,18 @@ private inline fun arithmeticPrimitive(
12791295 { x: Double -> EvaluateResult .double(doubleOp(x)) }
12801296 )
12811297
1298+ /* *
1299+ * Binary Arithmetic Function
1300+ * - Validates there is exactly 2 parameter.
1301+ * - Short circuits UNSET and ERROR parameter to return ERROR.
1302+ * - Short circuits NULL [Value] parameter to return NULL [Value].
1303+ * - If both parameter types are Integer then [intOp] will be used for evaluation.
1304+ * - Otherwise if both parameters are either Integer or Double, then the values are converted to
1305+ * Double, and then [doubleOp] will be used for evaluation.
1306+ * - All other parameter types return ERROR.
1307+ * - Primitive result is wrapped as EvaluateResult.
1308+ * - Catches evaluation exceptions and returns them as an ERROR.
1309+ */
12821310private inline fun arithmeticPrimitive (
12831311 crossinline intOp : (Long , Long ) -> Long ,
12841312 crossinline doubleOp : (Double , Double ) -> Double
@@ -1288,13 +1316,43 @@ private inline fun arithmeticPrimitive(
12881316 { x: Double , y: Double -> EvaluateResult .double(doubleOp(x, y)) }
12891317 )
12901318
1319+ /* *
1320+ * Binary Arithmetic Function
1321+ * - Validates there is exactly 2 parameter.
1322+ * - Short circuits UNSET and ERROR parameter to return ERROR.
1323+ * - Short circuits NULL [Value] parameter to return NULL [Value].
1324+ * - If any of parameters are Integer, they will be converted to Double.
1325+ * - After conversion, if both parameters are Double, the [doubleOp] will be used for evaluation.
1326+ * - All other parameter types return ERROR.
1327+ * - Catches evaluation exceptions and returns them as an ERROR.
1328+ */
12911329private inline fun arithmeticPrimitive (
12921330 crossinline doubleOp : (Double , Double ) -> Double
12931331): EvaluateFunction = arithmetic { x: Double , y: Double -> EvaluateResult .double(doubleOp(x, y)) }
12941332
1295- private inline fun arithmetic (crossinline doubleOp : (Double ) -> EvaluateResult ): EvaluateFunction =
1296- arithmetic({ n: Long -> doubleOp(n.toDouble()) }, doubleOp)
1333+ /* *
1334+ * Unary Arithmetic Function
1335+ * - Validates there is exactly 1 parameter.
1336+ * - Short circuits UNSET and ERROR parameter to return ERROR.
1337+ * - Short circuits NULL [Value] parameter to return NULL [Value].
1338+ * - If parameter is Integer, it will be converted to Double.
1339+ * - After conversion, if parameter is Double, the [function] will be used for evaluation.
1340+ * - All other parameter types return ERROR.
1341+ * - Catches evaluation exceptions and returns them as an ERROR.
1342+ */
1343+ private inline fun arithmetic (crossinline function : (Double ) -> EvaluateResult ): EvaluateFunction =
1344+ arithmetic({ n: Long -> function(n.toDouble()) }, function)
12971345
1346+ /* *
1347+ * Unary Arithmetic Function
1348+ * - Validates there is exactly 1 parameter.
1349+ * - Short circuits UNSET and ERROR parameter to return ERROR.
1350+ * - Short circuits NULL [Value] parameter to return NULL [Value].
1351+ * - If [Value] type is Integer then [intOp] will be used for evaluation.
1352+ * - If [Value] type is Double then [doubleOp] will be used for evaluation.
1353+ * - All other parameter types return ERROR.
1354+ * - Catches evaluation exceptions and returns them as an ERROR.
1355+ */
12981356private inline fun arithmetic (
12991357 crossinline intOp : (Long ) -> EvaluateResult ,
13001358 crossinline doubleOp : (Double ) -> EvaluateResult
@@ -1308,6 +1366,17 @@ private inline fun arithmetic(
13081366 doubleOp,
13091367 )
13101368
1369+ /* *
1370+ * Binary Arithmetic Function
1371+ * - Validates there is exactly 2 parameter.
1372+ * - Short circuits UNSET and ERROR parameter to return ERROR.
1373+ * - Short circuits NULL [Value] parameter to return NULL [Value].
1374+ * - Second parameter is expected to be Long.
1375+ * - If first parameter type is Integer then [intOp] will be used for evaluation.
1376+ * - If first parameter type is Double then [doubleOp] will be used for evaluation.
1377+ * - All other parameter types return ERROR.
1378+ * - Catches evaluation exceptions and returns them as an ERROR.
1379+ */
13111380@JvmName(" arithmeticNumberLong" )
13121381private inline fun arithmetic (
13131382 crossinline intOp : (Long , Long ) -> EvaluateResult ,
@@ -1322,6 +1391,17 @@ private inline fun arithmetic(
13221391 else EvaluateResultError
13231392}
13241393
1394+ /* *
1395+ * Binary Arithmetic Function
1396+ * - Validates there is exactly 2 parameter.
1397+ * - Short circuits UNSET and ERROR parameter to return ERROR.
1398+ * - Short circuits NULL [Value] parameter to return NULL [Value].
1399+ * - If both parameter types are Integer then [intOp] will be used for evaluation.
1400+ * - Otherwise if both parameters are either Integer or Double, then the values are converted to
1401+ * Double, and then [doubleOp] will be used for evaluation.
1402+ * - All other parameter types return ERROR.
1403+ * - Catches evaluation exceptions and returns them as an ERROR.
1404+ */
13251405private inline fun arithmetic (
13261406 crossinline intOp : (Long , Long ) -> EvaluateResult ,
13271407 crossinline doubleOp : (Double , Double ) -> EvaluateResult
@@ -1343,8 +1423,18 @@ private inline fun arithmetic(
13431423 }
13441424}
13451425
1426+ /* *
1427+ * Binary Arithmetic Function
1428+ * - Validates there is exactly 2 parameter.
1429+ * - Short circuits UNSET and ERROR parameter to return ERROR.
1430+ * - Short circuits NULL [Value] parameter to return NULL [Value].
1431+ * - If any of parameters are Integer, they will be converted to Double.
1432+ * - After conversion, if both parameters are Double, the [function] will be used for evaluation.
1433+ * - All other parameter types return ERROR.
1434+ * - Catches evaluation exceptions and returns them as an ERROR.
1435+ */
13461436private inline fun arithmetic (
1347- crossinline op : (Double , Double ) -> EvaluateResult
1437+ crossinline function : (Double , Double ) -> EvaluateResult
13481438): EvaluateFunction = binaryFunction { p1: Value , p2: Value ->
13491439 val v1: Double =
13501440 when (p1.valueTypeCase) {
@@ -1358,5 +1448,5 @@ private inline fun arithmetic(
13581448 ValueTypeCase .DOUBLE_VALUE -> p2.doubleValue
13591449 else -> return @binaryFunction EvaluateResultError
13601450 }
1361- op (v1, v2)
1451+ function (v1, v2)
13621452}
0 commit comments