Java Code Examples for org.reactfx.value.Val#create()

The following examples show how to use org.reactfx.value.Val#create() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LiveList.java    From ReactFX with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Experimental
static <E, T> Val<T> collapseDynamic(
        ObservableList<? extends E> list,
        ObservableValue<? extends Function<? super List<E>, ? extends T>> f) {
    return Val.create(
            () -> f.getValue().apply(Collections.unmodifiableList(list)),
            list, f);
}
 
Example 2
Source File: SizeTracker.java    From Flowless with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
 * Constructs a SizeTracker
 *
 * @param orientation if vertical, breadth = width and length = height;
 *                    if horizontal, breadth = height and length = width
 */
public SizeTracker(
        OrientationHelper orientation,
        ObservableObjectValue<Bounds> viewportBounds,
        MemoizationList<? extends Cell<?, ?>> lazyCells) {
    this.orientation = orientation;
    this.viewportBounds = viewportBounds;
    this.cells = lazyCells;
    this.breadths = lazyCells.map(orientation::minBreadth).memoize();
    this.maxKnownMinBreadth = breadths.memoizedItems()
            .reduce(Math::max)
            .orElseConst(0.0);
    this.breadthForCells = Val.combine(
            maxKnownMinBreadth,
            viewportBounds,
            (a, b) -> Math.max(a, orientation.breadth(b)));

    Val<Function<Cell<?, ?>, Double>> lengthFn = avoidFalseInvalidations(breadthForCells).map(
            breadth -> cell -> orientation.prefLength(cell, breadth));

    this.lengths = cells.mapDynamic(lengthFn).memoize();

    LiveList<Double> knownLengths = this.lengths.memoizedItems();
    Val<Double> sumOfKnownLengths = knownLengths.reduce((a, b) -> a + b).orElseConst(0.0);
    Val<Integer> knownLengthCount = knownLengths.sizeProperty();

    this.averageLengthEstimate = Val.create(
            () -> {
                // make sure to use pref lengths of all present cells
                for(int i = 0; i < cells.getMemoizedCount(); ++i) {
                    int j = cells.indexOfMemoizedItem(i);
                    lengths.force(j, j + 1);
                }

                int count = knownLengthCount.getValue();
                return count == 0
                        ? null
                        : sumOfKnownLengths.getValue() / count;
            },
            sumOfKnownLengths, knownLengthCount);

    this.totalLengthEstimate = Val.combine(
            averageLengthEstimate, cells.sizeProperty(),
            (avg, n) -> n * avg);

    Val<Integer> firstVisibleIndex = Val.create(
            () -> cells.getMemoizedCount() == 0 ? null : cells.indexOfMemoizedItem(0),
            cells, cells.memoizedItems()); // need to observe cells.memoizedItems()
            // as well, because they may change without a change in cells.

    Val<? extends Cell<?, ?>> firstVisibleCell = cells.memoizedItems()
            .collapse(visCells -> visCells.isEmpty() ? null : visCells.get(0));

    Val<Integer> knownLengthCountBeforeFirstVisibleCell = Val.create(() -> {
        return firstVisibleIndex.getOpt()
                .map(i -> lengths.getMemoizedCountBefore(Math.min(i, lengths.size())))
                .orElse(0);
    }, lengths, firstVisibleIndex);

    Val<Double> totalKnownLengthBeforeFirstVisibleCell = knownLengths.reduceRange(
            knownLengthCountBeforeFirstVisibleCell.map(n -> new IndexRange(0, n)),
            (a, b) -> a + b).orElseConst(0.0);

    Val<Double> unknownLengthEstimateBeforeFirstVisibleCell = Val.combine(
            firstVisibleIndex,
            knownLengthCountBeforeFirstVisibleCell,
            averageLengthEstimate,
            (firstIdx, knownCnt, avgLen) -> (firstIdx - knownCnt) * avgLen);

    Val<Double> firstCellMinY = firstVisibleCell.flatMap(orientation::minYProperty);

    lengthOffsetEstimate = Val.combine(
            totalKnownLengthBeforeFirstVisibleCell,
            unknownLengthEstimateBeforeFirstVisibleCell,
            firstCellMinY,
            (a, b, minY) -> a + b - minY).orElseConst(0.0);

    // pinning totalLengthEstimate and lengthOffsetEstimate
    // binds it all together and enables memoization
    this.subscription = Subscription.multi(
            totalLengthEstimate.pin(),
            lengthOffsetEstimate.pin());
}
 
Example 3
Source File: LiveList.java    From ReactFX with BSD 2-Clause "Simplified" License 4 votes vote down vote up
static Val<Integer> sizeOf(ObservableList<?> list) {
    return Val.create(() -> list.size(), list);
}
 
Example 4
Source File: LiveList.java    From ReactFX with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Experimental
static <E, T> Val<T> collapse(
        ObservableList<? extends E> list,
        Function<? super List<E>, ? extends T> f) {
    return Val.create(() -> f.apply(Collections.unmodifiableList(list)), list);
}