import java.io.Serializable;
import java.util.Collection;
+import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
public void beforeClientResponse(boolean initial) {
super.beforeClientResponse(initial);
+ // FIXME: Sorting and Filtering with Backend
+ List<Object> sortOrders = Collections.emptyList();
+ Set<Object> filters = Collections.emptySet();
+
if (initial || reset) {
// FIXME: Rethink the size question.
- rpc.reset((int) dataSource.apply(null).count());
+ int dataSourceSize = (int) dataSource.apply(
+ new Query(0, Integer.MAX_VALUE, sortOrders, filters))
+ .count();
+ rpc.reset(dataSourceSize);
}
if (!pushRows.isEmpty()) {
- // FIXME: Query object
- Stream<T> rowsToPush = dataSource.apply(null)
- .skip(pushRows.getStart()).limit(pushRows.length());
- pushData(pushRows.getStart(), rowsToPush);
+ int offset = pushRows.getStart();
+ int limit = pushRows.length();
+
+ Stream<T> rowsToPush;
+
+ if (dataSource instanceof InMemoryDataSource) {
+ // We can safely request all the data when in memory
+ // FIXME: sorted and filter.
+ rowsToPush = dataSource.apply(new Query()).skip(offset)
+ .limit(limit);
+ } else {
+ Query query = new Query(offset, limit, sortOrders, filters);
+ rowsToPush = dataSource.apply(query);
+ }
+
+ pushData(offset, rowsToPush);
}
if (!updatedData.isEmpty()) {
protected DataRequestRpc createRpc() {
return new SimpleDataRequestRpc();
}
-}
\ No newline at end of file
+}
* @param <T>
* data type
*/
-public interface DataSource<T, SORT> extends Function<Object, Stream<T>>,
+public interface DataSource<T, SORT> extends Function<Query, Stream<T>>,
Serializable {
/**
import java.util.stream.Stream;
/**
- * {@link DataSource} wrapper for {@link Collection}s.
+ * {@link DataSource} wrapper for {@link Collection}s. This class does not
+ * actually handle the {@link Query} parameters.
*
* @param <T>
* data type
*/
public class InMemoryDataSource<T> implements DataSource<T, Comparator<T>> {
- private Function<Object, Stream<T>> request;
+ private Function<Query, Stream<T>> request;
/**
* Constructs a new ListDataSource. This method makes a protective copy of
* @param request
* request for the new data source
*/
- protected InMemoryDataSource(Function<Object, Stream<T>> request) {
+ protected InMemoryDataSource(Function<Query, Stream<T>> request) {
this.request = request;
}
@Override
- public Stream<T> apply(Object query) {
+ public Stream<T> apply(Query query) {
return request.apply(query);
}
--- /dev/null
+/*
+ * Copyright 2000-2014 Vaadin Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.vaadin.tokka.server.communication.data;
+
+import java.io.Serializable;
+import java.util.Collections;
+import java.util.List;
+import java.util.Set;
+
+/**
+ * Query object used to request data from a backend. Contains index limits,
+ * sorting and filtering information.
+ *
+ * @since
+ */
+public class Query implements Serializable {
+
+ private final int offset;
+ private final int limit;
+ private final List<Object> sortOrders;
+ private final Set<Object> filters;
+
+ /**
+ * Constructs a Query for all rows from 0 to {@link Integer#MAX_VALUE}
+ * without sorting and filtering.
+ */
+ public Query() {
+ offset = 0;
+ limit = Integer.MAX_VALUE;
+ sortOrders = Collections.emptyList();
+ filters = Collections.emptySet();
+ }
+
+ /**
+ * Constructs a new Query object with given offset, limit, sorting and
+ * filtering.
+ *
+ * @param offset
+ * first index to fetch
+ * @param limit
+ * fetched item count
+ * @param sortOrders
+ * sorting order for fetching
+ * @param filters
+ * filtering for fetching
+ */
+ public Query(int offset, int limit, List<Object> sortOrders,
+ Set<Object> filters) {
+ this.offset = offset;
+ this.limit = limit;
+ this.sortOrders = sortOrders;
+ this.filters = filters;
+ }
+
+ /**
+ * Gets the first index of items to fetch.
+ *
+ * @return offset for data request
+ */
+ public int getOffset() {
+ return offset;
+ }
+
+ /**
+ * Gets the limit of items to fetch.
+ * <p>
+ * <strong>Note: </strong>It is possible that
+ * {@code offset + limit > item count}
+ *
+ * @return number of items to fetch
+ */
+ public int getLimit() {
+ return limit;
+ }
+
+ /**
+ * Gets the sorting for items to fetch.
+ *
+ * @return list of sort orders
+ */
+ public List<Object> getSortOrders() {
+ return sortOrders;
+ }
+
+ /**
+ * Gets the filters for items to fetch.
+ *
+ * @return set of filters
+ */
+ public Set<Object> getFilters() {
+ return filters;
+ }
+}
import com.vaadin.tokka.server.communication.data.DataSource;
import com.vaadin.tokka.server.communication.data.InMemoryDataSource;
+import com.vaadin.tokka.server.communication.data.Query;
public class InMemoryDataSourceTest {
@Test
public void testListContainsAllData() {
- dataSource.apply(null).forEach(
+ dataSource.apply(new Query()).forEach(
str -> assertTrue(
"Data source contained values not in original data",
data.remove(str)));
Comparator<StrBean> comp = Comparator.comparing(StrBean::getValue)
.thenComparing(StrBean::getRandomNumber)
.thenComparing(StrBean::getId);
- List<StrBean> list = dataSource.sortingBy(comp).apply(null)
+ List<StrBean> list = dataSource.sortingBy(comp).apply(new Query())
.collect(Collectors.toList());
// First value in data is { Xyz, 10, 100 } which should be last in list
public void testDefatulSortWithSpecifiedPostSort() {
Comparator<StrBean> comp = Comparator.comparing(StrBean::getValue)
.thenComparing(Comparator.comparing(StrBean::getId).reversed());
- List<StrBean> list = dataSource.sortingBy(comp).apply(null)
+ List<StrBean> list = dataSource.sortingBy(comp).apply(new Query())
// The sort here should come e.g from a Component
.sorted(Comparator.comparing(StrBean::getRandomNumber))
.collect(Collectors.toList());
@Test
public void testDefatulSortWithFunction() {
List<StrBean> list = dataSource.sortingBy(StrBean::getValue)
- .apply(null).collect(Collectors.toList());
+ .apply(new Query()).collect(Collectors.toList());
Assert.assertEquals("Sorted data and original data sizes don't match",
data.size(), list.size());