com.vaadin.data.provider.HierarchicalDataProvider Maven / Gradle / Ivy
/*
* Copyright (C) 2000-2024 Vaadin Ltd
*
* This program is available under Vaadin Commercial License and Service Terms.
*
* See for the full
* 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
* data type
* @param
* filter type
*/
public interface HierarchicalDataProvider extends DataProvider {
/**
* 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 query) {
if (query instanceof HierarchicalQuery, ?>) {
return getChildCount((HierarchicalQuery) 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.
*
* NOTE: If your data request is likely to involve I/O
* channels, see {@link DataProvider#fetch(Query)} for instructions
* on how to handle the stream without risking resource leaks.
*
* @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 fetch(Query query) {
if (query instanceof HierarchicalQuery, ?>) {
return fetchChildren((HierarchicalQuery) 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 query);
/**
* Fetches data from this HierarchicalDataProvider using given
* {@code query}. Only the immediate children of
* {@link HierarchicalQuery#getParent()} will be returned.
*
* NOTE: If your data request is likely to involve I/O
* channels, see {@link DataProvider#fetch(Query)} for instructions
* on how to handle the stream without risking resource leaks.
*
* @param query
* given query to request data with
* @return a stream of data objects resulting from the query
*/
public Stream fetchChildren(HierarchicalQuery 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);
}