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);
}
}
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);
}
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);
}
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);
}
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());
}
Aggregations