Cocoa Beans 0.0.54-beta0 Help

🔄 Collection Observable Conversion

Available Since 0.0.50

Introduction

as is a conversion operator on CollectionObservable that transforms a collection observable into a different collection type using a ObservableCollectionType.

For example, you can derive a SetObservable from a ListObservable — duplicates are automatically collapsed, and the result stays in sync with the source:

ListObservable<Integer> numbers = Observable.list(); numbers.addAll(List.of(1, 2, 2, 3)); SetObservable<Integer> uniqueNumbers = numbers.as(ObservableCollectionType.toSet()); // Set.of(1, 2, 3)

The ObservableCollectionType utility class provides ready-made collectors for common conversions.

Usage

@Test void listToSetConversion() { ListObservable<Integer> numbers = Observable.list(); numbers.addAll(List.of(1, 2, 2, 3)); // derive a SetObservable — duplicates are collapsed automatically SetObservable<Integer> unique = numbers.as(ObservableCollectionType.toSet()); assertEquals(Set.of(1, 2, 3), unique.get()); // the derived set is read-only assertThrows(UnsupportedOperationException.class, () -> unique.add(4)); // changes to the source are reflected numbers.add(4); assertEquals(Set.of(1, 2, 3, 4), unique.get()); // adding a duplicate in the source doesn't affect the set numbers.add(2); assertEquals(Set.of(1, 2, 3, 4), unique.get()); }
@Test void listToListConversion() { ListObservable<Integer> source = Observable.list(); source.addAll(List.of(10, 20, 30)); // derive a read-only ListObservable copy ListObservable<Integer> derived = source.as(ObservableCollectionType.toList()); assertEquals(List.of(10, 20, 30), derived.get()); // previous snapshots are frozen — not affected by later changes List<Integer> snapshot = derived.get(); source.add(40); assertEquals(List.of(10, 20, 30), snapshot); assertEquals(List.of(10, 20, 30, 40), derived.get()); // chain with other operators ListObservable<String> labels = derived.mapEach(n -> "#" + n); assertEquals(List.of("#10", "#20", "#30", "#40"), labels.get()); }

Built-in collectors

Method

Source

Result

Default snapshot

Default collection

ObservableCollectionType.toSet()

any CollectionObservable<E>

SetObservable<E>

Set::copyOf

HashSet::new

ObservableCollectionType.toList()

any CollectionObservable<E>

ListObservable<E>

List::copyOf

ArrayList::new

Both methods also accept custom factory functions for the snapshot and mutable-collection creation, so you can use a LinkedHashSet, CopyOnWriteArrayList, or any other implementation:

SetObservable<Integer> ordered = base.as( ObservableCollectionType.toSet(LinkedHashSet::new, LinkedHashSet::new) );

Behavior

  • The derived collection is read-only — add, remove, addAll, removeAll, removeIf, retainAll, clear all throw UnsupportedOperationException.

  • Snapshots returned by get() are immutable and cached — the snapshot is only recomputed when the source collection changes.

  • Previous snapshots are not affected by later source changes; they remain a frozen view of the collection at the time they were obtained.

  • Derived collections support chaining with other operators like mapEach, flatMapEach, and filter.

Advantages

  • 🔄 Live conversion: always reflects the latest state of the source collection

  • 🧊 Stable snapshots: each get() result is immutable and won't change under you

  • ðŸ§ą Composable: the result is a full CollectionObservable — chain filter, mapEach, etc.

  • 🔌 Extensible: implement your own ObservableCollectionType for custom collection types

  • ðŸŠķ Lazy: conversion only happens when get() is called and the source has changed

Last modified: 16 May 2026