aboutsummaryrefslogtreecommitdiffstats
path: root/server/src/main/java/com/vaadin/data/provider/HierarchicalDataProvider.java
blob: 8dabdce2cc3280b6b26f0e5d3595db5b05f42e93 (plain)
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
/*
 * 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.util.stream.Stream;

/**
 * A common interface for fetching hierarchical data from a data source, such as
 * an in-memory collection or a backend database.
 *
 * @author Vaadin Ltd
 * @since 8.1
 *
 * @param <T>
 *            data type
 * @param <F>
 *            filter type
 */
public interface HierarchicalDataProvider<T, F> extends DataProvider<T, F> {

    /**
     * Get the number of immediate child data items for the parent item returned
     * by a given query.
     * 
     * @param query
     *            given query to request the count for
     * @return the count of child data items for the data item
     *         {@link HierarchicalQuery#getParent()}
     * 
     * @throws IllegalArgumentException
     *             if the query is not of type HierarchicalQuery
     */
    @Override
    public default int size(Query<T, F> query) {
        if (query instanceof HierarchicalQuery<?, ?>) {
            return getChildCount((HierarchicalQuery<T, F>) query);
        }
        throw new IllegalArgumentException(
                "Hierarchical data provider doesn't support non-hierarchical queries");
    }

    /**
     * Fetches data from this HierarchicalDataProvider using given
     * {@code query}. Only the immediate children of
     * {@link HierarchicalQuery#getParent()} will be returned.
     * 
     * @param query
     *            given query to request data with
     * @return a stream of data objects resulting from the query
     * 
     * @throws IllegalArgumentException
     *             if the query is not of type HierarchicalQuery
     */
    @Override
    public default Stream<T> fetch(Query<T, F> query) {
        if (query instanceof HierarchicalQuery<?, ?>) {
            return fetchChildren((HierarchicalQuery<T, F>) query);
        }
        throw new IllegalArgumentException(
                "Hierarchical data provider doesn't support non-hierarchical queries");
    }

    /**
     * Get the number of immediate child data items for the parent item returned
     * by a given query.
     * 
     * @param query
     *            given query to request the count for
     * @return the count of child data items for the data item
     *         {@link HierarchicalQuery#getParent()}
     */
    public int getChildCount(HierarchicalQuery<T, F> query);

    /**
     * Fetches data from this HierarchicalDataProvider using given
     * {@code query}. Only the immediate children of
     * {@link HierarchicalQuery#getParent()} will be returned.
     * 
     * @param query
     *            given query to request data with
     * @return a stream of data objects resulting from the query
     */
    public Stream<T> fetchChildren(HierarchicalQuery<T, F> query);

    /**
     * Check whether a given item has any children associated with it.
     * 
     * @param item
     *            the item to check for children
     * @return whether the given item has children
     */
    public boolean hasChildren(T item);

}