Cocoa Beans 0.0.54-beta0 Help

๐Ÿ”Ž Filtering Collection Observable

Available Since 0.0.46

Introduction

Sometimes we want to derive a live view of a collection by filtering its elements based on an Observable<Boolean> owned by each element.

This is different from a simple Predicate<E> filter, because the membership of each element can change over time (for example: a player becomes alive/dead, online/offline) and the filtered collection should update automatically.

Usage

@Test void filteredCollection() { SetObservable<GamePlayer> players = Observable.set(); SetObservable<GamePlayer> alivePlayers = players.filter(GamePlayer::getAlive); GamePlayer a = new GamePlayer(UUID.randomUUID()); GamePlayer b = new GamePlayer(UUID.randomUUID()); players.add(a); players.add(b); assertEquals(Set.of(), alivePlayers.get()); a.setOnline(true); assertEquals(Set.of(), alivePlayers.get()); a.respawn(); assertEquals(Set.of(a), alivePlayers.get()); b.setOnline(true); b.respawn(); assertEquals(Set.of(a, b), alivePlayers.get()); }
public class GamePlayer { private final UUID uuid; private final MutableObservable<Boolean> online; private final MutableObservable<PlayerState> state; // Derived observable predicate per element private final Observable<Boolean> alive; public GamePlayer(UUID uuid) { this.uuid = uuid; this.online = Observable.mutable(false); this.state = Observable.mutable(PlayerState.DEAD); this.alive = Observable.compound(online, state) .map(args -> args.arg0() && args.arg1().isAlive()); } public UUID getUUID() { return uuid; } public Observable<Boolean> getAlive() { return alive; } public void setOnline(boolean online) { this.online.set(online); } public void setSpectator() { this.state.set(PlayerState.SPECTATOR); } public void setDead() { this.state.set(PlayerState.DEAD); } public void respawn() { this.state.set(PlayerState.ALIVE); } }
public enum PlayerState { DEAD, ALIVE, SPECTATOR; public boolean isAlive() { return this == ALIVE; } }

Expected behavior

  • When an element is added to the source collection:

    • Subscribe to its predicate observable

    • Include/exclude it based on the current predicate value

  • When an element is removed from the source collection:

    • Unsubscribe from its predicate observable

    • Remove it from the filtered collection

  • When the predicate observable changes:

    • true: element appears in filtered view

    • false: element disappears from filtered view

Notes per collection type

Source

Result

Element duplication

ListObservable<E>

ListObservable<E>

Source order and multiplicity preserved — each occurrence is included/excluded independently

SetObservable<E>

SetObservable<E>

The source already deduplicates by element

Advantages

  • โœ… Live subset view: Always up-to-date without manual bookkeeping

  • ๐Ÿง  Reactive membership: Elements can enter/leave the view when their internal state changes

  • ๐Ÿงน No observer leaks: Observers are removed when elements leave the source collection

  • โšก Efficient updates: Updates only the impacted elements rather than recomputing everything

  • โ›“ Chaining: Can be chained with other operators

Last modified: 09 May 2026