@@ -25,63 +25,83 @@ internal interface Aggregator<in Value, out Return> {
25
25
/* *
26
26
* Base function of [Aggregator].
27
27
*
28
- * Aggregates the given values, taking [type ] into account,
28
+ * Aggregates the given values, taking [valueType ] into account,
29
29
* filtering nulls (only if [type.isMarkedNullable][KType.isMarkedNullable]),
30
30
* and computes a single resulting value.
31
31
*
32
- * When using [AggregatorBase], this can be supplied by the [AggregatorBase.aggregator ] argument.
32
+ * When using [AggregatorBase], this can be supplied by the [AggregatorBase.aggregateSingle ] argument.
33
33
*
34
- * When the exact [type ] is unknown, use [aggregateCalculatingType ].
34
+ * When the exact [valueType ] is unknown, use [aggregateCalculatingValueType ].
35
35
*/
36
- fun aggregate (values : Iterable <Value ?>, type : KType ): Return
36
+ fun aggregateSingleIterable (values : Iterable <Value ?>, valueType : KType ): Return
37
+
38
+ fun calculateValueType (valueTypes : Set <KType >): KType
39
+
40
+ fun calculateValueType (values : Iterable <Value ?>): KType
37
41
38
42
/* *
39
43
* Aggregates the data in the given column and computes a single resulting value.
40
- * Calls [aggregate ] (with [Iterable] and [KType]).
44
+ * Calls [aggregateSingleColumn ] (with [Iterable] and [KType]).
41
45
*
42
- * See [AggregatorBase.aggregate ].
46
+ * See [AggregatorBase.aggregateSingleIterable ].
43
47
*/
44
- fun aggregate (column : DataColumn <Value ?>): Return
48
+ fun aggregateSingleColumn (column : DataColumn <Value ?>): Return
45
49
46
50
/* *
47
51
* Aggregates the data in the multiple given columns and computes a single resulting value.
48
52
*/
49
- fun aggregate (columns : Iterable <DataColumn <Value ?>>): Return
53
+ fun aggregateMultipleColumns (columns : Iterable <DataColumn <Value ?>>): Return
50
54
51
55
/* *
52
- * Special case of [aggregate] with [Iterable] that calculates the common type of the values at runtime.
53
- * Without [valueTypes], this is a heavy operation and should be avoided when possible.
54
- *
55
- * @param values The values to be aggregated.
56
- * @param valueTypes The types of the values.
57
- * If provided, this can be used to avoid calculating the types of [values] at runtime with reflection.
58
- * It should contain all types of [values].
59
- * If `null` or empty, the types of [values] will be calculated at runtime (heavy!).
60
- */
61
- fun aggregateCalculatingType (values : Iterable <Value ?>, valueTypes : Set <KType >? = null): Return
62
-
63
- /* *
64
- * Function that can give the return type of [aggregate] as [KType], given the type of the input.
56
+ * Function that can give the return type of [aggregateSingleIterable] as [KType], given the type of the input.
65
57
* This allows aggregators to avoid runtime type calculations.
66
58
*
67
59
* @param type The type of the input values.
68
60
* @param emptyInput If `true`, the input values are considered empty. This often affects the return type.
69
- * @return The return type of [aggregate ] as [KType].
61
+ * @return The return type of [aggregateSingleIterable ] as [KType].
70
62
*/
71
63
fun calculateReturnTypeOrNull (type : KType , emptyInput : Boolean ): KType ?
72
64
73
65
/* *
74
- * Function that can give the return type of [aggregate ] with columns as [KType],
66
+ * Function that can give the return type of [aggregateSingleIterable ] with columns as [KType],
75
67
* given the multiple types of the input.
76
68
* This allows aggregators to avoid runtime type calculations.
77
69
*
78
70
* @param colTypes The types of the input columns.
79
71
* @param colsEmpty If `true`, all the input columns are considered empty. This often affects the return type.
80
- * @return The return type of [aggregate ] as [KType].
72
+ * @return The return type of [aggregateSingleIterable ] as [KType].
81
73
*/
82
- fun calculateReturnTypeOrNull (colTypes : Set <KType >, colsEmpty : Boolean ): KType ?
74
+ fun calculateReturnTypeMultipleColumnsOrNull (colTypes : Set <KType >, colsEmpty : Boolean ): KType ?
75
+
76
+ val ref: Aggregator <Value , Return > get() = this
83
77
}
84
78
79
+ internal fun <Value , Return > Aggregator <Value , Return >.aggregate (values : Iterable <Value ?>, valueType : KType ) =
80
+ aggregateSingleIterable(values, valueType)
81
+
82
+ internal fun <Value , Return > Aggregator <Value , Return >.calculateValueType (
83
+ values : Iterable <Value ?>,
84
+ valueTypes : Set <KType >? = null,
85
+ ) = if (valueTypes != null && valueTypes.isNotEmpty()) {
86
+ calculateValueType(valueTypes)
87
+ } else {
88
+ calculateValueType(values)
89
+ }
90
+
91
+ internal fun <Value , Return > Aggregator <Value , Return >.aggregateCalculatingValueType (
92
+ values : Iterable <Value ?>,
93
+ valueTypes : Set <KType >? = null,
94
+ ) = aggregateSingleIterable(
95
+ values = values,
96
+ valueType = calculateValueType(values, valueTypes),
97
+ )
98
+
99
+ internal fun <Value , Return > Aggregator <Value , Return >.aggregate (column : DataColumn <Value ?>) =
100
+ aggregateSingleColumn(column)
101
+
102
+ internal fun <Value , Return > Aggregator <Value , Return >.aggregate (columns : Iterable <DataColumn <Value ?>>) =
103
+ aggregateMultipleColumns(columns)
104
+
85
105
@Suppress(" UNCHECKED_CAST" )
86
106
@PublishedApi
87
107
internal fun <Type > Aggregator <* , * >.cast (): Aggregator <Type , Type > = this as Aggregator <Type , Type >
@@ -90,15 +110,18 @@ internal fun <Type> Aggregator<*, *>.cast(): Aggregator<Type, Type> = this as Ag
90
110
@PublishedApi
91
111
internal fun <Value , Return > Aggregator <* , * >.cast2 (): Aggregator <Value , Return > = this as Aggregator <Value , Return >
92
112
93
- /* * Type alias for [Aggregator.calculateReturnTypeOrNull ] */
113
+ /* * Type alias for [Aggregator.calculateReturnTypeMultipleColumnsOrNull ] */
94
114
internal typealias CalculateReturnTypeOrNull = (type: KType , emptyInput: Boolean ) -> KType ?
95
115
96
116
/* *
97
- * Type alias for the argument for [Aggregator.aggregate ].
117
+ * Type alias for the argument for [Aggregator.aggregateSingleIterable ].
98
118
* Nulls have already been filtered out when this argument is called.
99
119
*/
100
120
internal typealias Aggregate <Value , Return > = Iterable <Value & Any >.(type: KType ) -> Return
101
121
122
+ internal typealias AggregateBy <Source , Value , Return > =
123
+ Iterable <Source >.(sourceType: KType , valueType: KType , selector: (Source ) -> Value ) -> Return
124
+
102
125
/* * Common case for [CalculateReturnTypeOrNull], preserves return type, but makes it nullable for empty inputs. */
103
126
internal val preserveReturnTypeNullIfEmpty: CalculateReturnTypeOrNull = { type, emptyInput ->
104
127
type.withNullability(emptyInput)
0 commit comments