1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
/*
* Copyright 2000-2016 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.data.provider;
import java.io.Serializable;
import java.util.Objects;
import java.util.stream.Stream;
import com.vaadin.data.ValueProvider;
/**
* Data provider that uses one callback for fetching items from a back end and
* another callback for counting the number of available items.
*
* @author Vaadin Ltd
* @since 8.0
*
* @param <T>
* data provider data type
* @param <F>
* data provider filter type
*/
public class CallbackDataProvider<T, F>
extends AbstractBackEndDataProvider<T, F> {
/**
* Callback interface for fetching a stream of items from a backend based on
* a query.
*
* @param <T>
* the type of the items to fetch
* @param <F>
* the type of the optional filter in the query,
* <code>Void</code> if filtering is not supported
*/
@FunctionalInterface
public interface FetchCallback<T, F> extends Serializable {
/**
* Fetches a stream of items based on a query. The query defines the
* paging of the items to fetch through {@link Query#getOffset()} and
* {@link Query#getLimit()}, the sorting through
* {@link Query#getSortOrders()} and optionally also any filtering to
* use through {@link Query#getFilter()}.
*
* @param query
* the query that defines which items to fetch
* @return a stream of items
*/
public Stream<T> fetch(Query<T, F> query);
}
/**
* Callback interface for counting the number of items in a backend based on
* a query.
*
* @param <T>
* the type of the items to count
* @param <F>
* the type of the optional filter in the query,
* <code>Void</code> if filtering is not supported
*/
@FunctionalInterface
public interface CountCallback<T, F> extends Serializable {
/**
* Counts the number of available items based on a query. The query
* optionally defines any filtering to use through
* {@link Query#getFilter()}. The query also contains information about
* paging and sorting although that information is generally not
* applicable for determining the number of items.
*
* @param query
* the query that defines which items to count
* @return the number of available items
*/
public int count(Query<T, F> query);
}
private final FetchCallback<T, F> fetchCallback;
private final CountCallback<T, F> countCallback;
private final ValueProvider<T, Object> idGetter;
/**
* Constructs a new DataProvider to request data using callbacks for
* fetching and counting items in the back end.
*
* @param fetchCallback
* function that returns a stream of items from the back end for
* a query
* @param countCallback
* function that return the number of items in the back end for a
* query
*
* @see #CallbackDataProvider(FetchCallback, CountCallback, ValueProvider)
*/
public CallbackDataProvider(FetchCallback<T, F> fetchCallback,
CountCallback<T, F> countCallback) {
this(fetchCallback, countCallback, t -> t);
}
/**
* Constructs a new DataProvider to request data using callbacks for
* fetching and counting items in the back end.
*
* @param fetchCallBack
* function that requests data from back end based on query
* @param countCallback
* function that returns the amount of data in back end for query
* @param identifierGetter
* function that returns the identifier for a given item
*/
public CallbackDataProvider(FetchCallback<T, F> fetchCallBack,
CountCallback<T, F> countCallback,
ValueProvider<T, Object> identifierGetter) {
Objects.requireNonNull(fetchCallBack, "Request function can't be null");
Objects.requireNonNull(countCallback, "Count callback can't be null");
Objects.requireNonNull(identifierGetter,
"Identifier getter function can't be null");
this.fetchCallback = fetchCallBack;
this.countCallback = countCallback;
this.idGetter = identifierGetter;
}
@Override
public Stream<T> fetchFromBackEnd(Query<T, F> query) {
return fetchCallback.fetch(query);
}
@Override
protected int sizeInBackEnd(Query<T, F> query) {
return countCallback.count(query);
}
@Override
public Object getId(T item) {
Object itemId = idGetter.apply(item);
assert itemId != null : "CallbackDataProvider got null as an id for item: "
+ item;
return itemId;
}
}
|