AggregatedList(list: ObservableList<out E>, toKey: (E) -> K, assemble: (K, ObservableList<E>) -> A)
Given an ObservableList<E> and a grouping key K, AggregatedList groups the elements by the key into a fresh ObservableList<E> for each group and exposes the groups as an observable list of As by calling assemble on each.
Changes done to elements of the input list are reflected in the observable list of the respective group, whereas additions/removals of elements in the underlying list are reflected in the exposed ObservableList<A> by adding/deleting aggregations as expected.
The ordering of the exposed list is based on the hashCode of keys. The ordering of the groups themselves is based on the hashCode of elements.
Warning: If there are two elements E in the source list that have the same hashCode then it is not deterministic which one will be removed if one is removed from the source list
Example: val statesGroupedByCurrency = AggregatedList(states, { state -> state.currency }) { currency, group -> object { val currency = currency val states = group } }
The above creates an observable list of (currency, statesOfCurrency) pairs.
Note that update events to the source list are discarded, assuming the key of elements does not change. TODO Should we handle this case? It requires additional bookkeeping of sourceIndex->(aggregationIndex, groupIndex)
list
- The underlying list.toKey
- Function to extract the key from an element.assemble
- Function to assemble the aggregation into the exposed A.