Search in sources :

Example 21 with Range

use of com.vaadin.flow.internal.Range in project flow by vaadin.

the class DataCommunicator method flush.

private void flush() {
    Set<String> oldActive = new HashSet<>(activeKeyOrder);
    Range effectiveRequested;
    final Range previousActive = Range.withLength(activeStart, activeKeyOrder.size());
    // With defined size the backend is only queried when necessary
    if (definedSize && (resendEntireRange || sizeReset)) {
        assumedSize = getDataProviderSize();
    } else if (!definedSize && (!skipCountIncreaseUntilReset || sizeReset)) {
        // with undefined size, size estimate is checked when scrolling down
        updateUndefinedSize();
    }
    effectiveRequested = requestedRange.restrictTo(Range.withLength(0, assumedSize));
    resendEntireRange |= !(previousActive.intersects(effectiveRequested) || (previousActive.isEmpty() && effectiveRequested.isEmpty()));
    UI ui = getUI();
    if (ui != null && executor != null) {
        // will perform fetch from data provider with given range.
        if (ui.getPushConfiguration().getPushMode() != PushMode.AUTOMATIC) {
            throw new IllegalStateException("Asynchronous DataCommunicator updates require Push to be enabled and PushMode.AUTOMATIC");
        }
        if (future != null) {
            future.cancel(true);
        }
        future = CompletableFuture.supplyAsync(() -> collectKeysToFlush(previousActive, effectiveRequested), executor);
        future.thenAccept(activation -> {
            if (ui == null) {
                return;
            }
            ui.access(() -> {
                performUpdate(oldActive, effectiveRequested, previousActive, activation);
            });
        });
    } else {
        Activation activation = collectKeysToFlush(previousActive, effectiveRequested);
        performUpdate(oldActive, effectiveRequested, previousActive, activation);
    }
}
Also used : UI(com.vaadin.flow.component.UI) Range(com.vaadin.flow.internal.Range) HashSet(java.util.HashSet)

Example 22 with Range

use of com.vaadin.flow.internal.Range in project flow by vaadin.

the class HierarchyMapperWithDataTest method fetchRangeOfRows.

@Test
public void fetchRangeOfRows() {
    expand(testData.get(0));
    expand(testData.get(1));
    List<Node> expectedResult = testData.stream().filter(n -> roots.contains(n) || n.getParent().equals(testData.get(0)) || n.getParent().equals(testData.get(1))).collect(Collectors.toList());
    // Range containing deepest level of expanded nodes without their
    // parents in addition to root nodes at the end.
    Range range = Range.between(3, mapper.getTreeSize());
    verifyFetchIsCorrect(expectedResult, range);
    // Only the expanded two nodes, nothing more.
    range = Range.between(0, 2);
    verifyFetchIsCorrect(expectedResult, range);
    // Fetch everything
    range = Range.between(0, mapper.getTreeSize());
    verifyFetchIsCorrect(expectedResult, range);
}
Also used : IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) Collection(java.util.Collection) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) Objects(java.util.Objects) List(java.util.List) Stream(java.util.stream.Stream) Rule(org.junit.Rule) SerializablePredicate(com.vaadin.flow.function.SerializablePredicate) Comparator(java.util.Comparator) Assert(org.junit.Assert) ExpectedException(org.junit.rules.ExpectedException) Assert.assertEquals(org.junit.Assert.assertEquals) Before(org.junit.Before) Range(com.vaadin.flow.internal.Range) Range(com.vaadin.flow.internal.Range) Test(org.junit.Test)

Example 23 with Range

use of com.vaadin.flow.internal.Range in project flow by vaadin.

the class HierarchyMapperWithDataTest method fetchRangeOfRowsWithSorting.

@Test
public void fetchRangeOfRowsWithSorting() {
    // Expand before sort
    expand(testData.get(0));
    expand(testData.get(1));
    // Construct a sorted version of test data with correct filters
    List<List<Node>> levels = new ArrayList<>();
    Comparator<Node> comparator = Comparator.comparing(Node::getNumber).reversed();
    levels.add(testData.stream().filter(n -> n.getParent() == null).sorted(comparator).collect(Collectors.toList()));
    levels.add(testData.stream().filter(n -> n.getParent() == testData.get(0)).sorted(comparator).collect(Collectors.toList()));
    levels.add(testData.stream().filter(n -> n.getParent() == testData.get(1)).sorted(comparator).collect(Collectors.toList()));
    List<Node> expectedResult = levels.get(0).stream().flatMap(root -> {
        Stream<Node> nextLevel = levels.get(1).stream().filter(n -> n.getParent() == root).flatMap(node -> Stream.concat(Stream.of(node), levels.get(2).stream().filter(n -> n.getParent() == node)));
        return Stream.concat(Stream.of(root), nextLevel);
    }).collect(Collectors.toList());
    // Apply sorting
    mapper.setInMemorySorting(comparator::compare);
    // Range containing deepest level of expanded nodes without their
    // parents in addition to root nodes at the end.
    Range range = Range.between(8, mapper.getTreeSize());
    verifyFetchIsCorrect(expectedResult, range);
    // Only the root nodes, nothing more.
    range = Range.between(0, ROOT_COUNT);
    verifyFetchIsCorrect(expectedResult, range);
    // Fetch everything
    range = Range.between(0, mapper.getTreeSize());
    verifyFetchIsCorrect(expectedResult, range);
}
Also used : IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) Collection(java.util.Collection) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) Objects(java.util.Objects) List(java.util.List) Stream(java.util.stream.Stream) Rule(org.junit.Rule) SerializablePredicate(com.vaadin.flow.function.SerializablePredicate) Comparator(java.util.Comparator) Assert(org.junit.Assert) ExpectedException(org.junit.rules.ExpectedException) Assert.assertEquals(org.junit.Assert.assertEquals) Before(org.junit.Before) Range(com.vaadin.flow.internal.Range) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) IntStream(java.util.stream.IntStream) Stream(java.util.stream.Stream) Range(com.vaadin.flow.internal.Range) Test(org.junit.Test)

Example 24 with Range

use of com.vaadin.flow.internal.Range in project flow by vaadin.

the class RangeTest method combine_subRange.

@Test
public void combine_subRange() {
    Range r1 = Range.between(0, 10);
    Range r2 = Range.between(2, 8);
    // Test both ways, should give the same result
    Range combined1 = r1.combineWith(r2);
    Range combined2 = r2.combineWith(r1);
    assertEquals(combined1, combined2);
    assertEquals(r1, combined1);
}
Also used : Range(com.vaadin.flow.internal.Range) Test(org.junit.Test)

Example 25 with Range

use of com.vaadin.flow.internal.Range in project flow by vaadin.

the class RangeTest method combine_barelyOverlapping.

@Test
public void combine_barelyOverlapping() {
    Range r1 = Range.between(0, 10);
    Range r2 = Range.between(10, 20);
    // Test both ways, should give the same result
    Range combined1 = r1.combineWith(r2);
    Range combined2 = r2.combineWith(r1);
    assertEquals(combined1, combined2);
    assertEquals(0, combined1.getStart());
    assertEquals(20, combined1.getEnd());
}
Also used : Range(com.vaadin.flow.internal.Range) Test(org.junit.Test)

Aggregations

Range (com.vaadin.flow.internal.Range)33 Test (org.junit.Test)28 ArrayList (java.util.ArrayList)5 Comparator (java.util.Comparator)4 List (java.util.List)4 Objects (java.util.Objects)4 Collectors (java.util.stream.Collectors)4 Stream (java.util.stream.Stream)4 SerializablePredicate (com.vaadin.flow.function.SerializablePredicate)3 Arrays (java.util.Arrays)3 Collection (java.util.Collection)3 HashSet (java.util.HashSet)3 UI (com.vaadin.flow.component.UI)2 IntStream (java.util.stream.IntStream)2 Assert (org.junit.Assert)2 Assert.assertEquals (org.junit.Assert.assertEquals)2 Assert.assertTrue (org.junit.Assert.assertTrue)2 Before (org.junit.Before)2 Rule (org.junit.Rule)2 ExpectedException (org.junit.rules.ExpectedException)2