in model/src/main/java/jetbrains/jetpad/model/collections/list/ObservableCollections.java [114:157]
public static <ItemT> ReadableProperty<Integer> count(
final ObservableCollection<ItemT> collection, final Predicate<? super ItemT> predicate) {
return new BaseDerivedProperty<Integer>(simpleCount(predicate, collection)) {
private Registration myCollectionRegistration;
private int myCount;
@Override
protected void doAddListeners() {
myCollectionRegistration = collection.addListener(new CollectionAdapter<ItemT>() {
@Override
public void onItemAdded(CollectionItemEvent<? extends ItemT> event) {
if (predicate.test(event.getNewItem())) {
myCount++;
}
somethingChanged();
}
@Override
public void onItemRemoved(CollectionItemEvent<? extends ItemT> event) {
if (predicate.test(event.getOldItem())) {
myCount--;
}
somethingChanged();
}
});
myCount = simpleCount(predicate, collection);
}
@Override
protected void doRemoveListeners() {
myCollectionRegistration.remove();
myCollectionRegistration = null;
}
@Override
protected Integer doGet() {
if (myCollectionRegistration == null) {
return simpleCount(predicate, collection);
} else {
return myCount;
}
}
};
}