You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

HierarchicalDataProvider.java 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Copyright 2000-2016 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.data.provider;
  17. import java.util.stream.Stream;
  18. /**
  19. * A common interface for fetching hierarchical data from a data source, such as
  20. * an in-memory collection or a backend database.
  21. *
  22. * @author Vaadin Ltd
  23. * @since 8.1
  24. *
  25. * @param <T>
  26. * data type
  27. * @param <F>
  28. * filter type
  29. */
  30. public interface HierarchicalDataProvider<T, F> extends DataProvider<T, F> {
  31. /**
  32. * Get the number of immediate child data items for the parent item returned
  33. * by a given query.
  34. *
  35. * @param query
  36. * given query to request the count for
  37. * @return the count of child data items for the data item
  38. * {@link HierarchicalQuery#getParent()}
  39. *
  40. * @throws IllegalArgumentException
  41. * if the query is not of type HierarchicalQuery
  42. */
  43. @Override
  44. public default int size(Query<T, F> query) {
  45. if (query instanceof HierarchicalQuery<?, ?>) {
  46. return getChildCount((HierarchicalQuery<T, F>) query);
  47. }
  48. throw new IllegalArgumentException(
  49. "Hierarchical data provider doesn't support non-hierarchical queries");
  50. }
  51. /**
  52. * Fetches data from this HierarchicalDataProvider using given
  53. * {@code query}. Only the immediate children of
  54. * {@link HierarchicalQuery#getParent()} will be returned.
  55. *
  56. * @param query
  57. * given query to request data with
  58. * @return a stream of data objects resulting from the query
  59. *
  60. * @throws IllegalArgumentException
  61. * if the query is not of type HierarchicalQuery
  62. */
  63. @Override
  64. public default Stream<T> fetch(Query<T, F> query) {
  65. if (query instanceof HierarchicalQuery<?, ?>) {
  66. return fetchChildren((HierarchicalQuery<T, F>) query);
  67. }
  68. throw new IllegalArgumentException(
  69. "Hierarchical data provider doesn't support non-hierarchical queries");
  70. }
  71. /**
  72. * Get the number of immediate child data items for the parent item returned
  73. * by a given query.
  74. *
  75. * @param query
  76. * given query to request the count for
  77. * @return the count of child data items for the data item
  78. * {@link HierarchicalQuery#getParent()}
  79. */
  80. public int getChildCount(HierarchicalQuery<T, F> query);
  81. /**
  82. * Fetches data from this HierarchicalDataProvider using given
  83. * {@code query}. Only the immediate children of
  84. * {@link HierarchicalQuery#getParent()} will be returned.
  85. *
  86. * @param query
  87. * given query to request data with
  88. * @return a stream of data objects resulting from the query
  89. */
  90. public Stream<T> fetchChildren(HierarchicalQuery<T, F> query);
  91. /**
  92. * Check whether a given item has any children associated with it.
  93. *
  94. * @param item
  95. * the item to check for children
  96. * @return whether the given item has children
  97. */
  98. public boolean hasChildren(T item);
  99. }