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.

InMemoryHierarchicalDataProvider.java 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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.Comparator;
  18. import java.util.Objects;
  19. import java.util.Optional;
  20. import java.util.stream.Stream;
  21. import com.vaadin.data.HierarchyData;
  22. import com.vaadin.data.ValueProvider;
  23. import com.vaadin.server.SerializableComparator;
  24. import com.vaadin.server.SerializableFunction;
  25. import com.vaadin.server.SerializablePredicate;
  26. import com.vaadin.shared.data.sort.SortDirection;
  27. /**
  28. * A {@link DataProvider} for in-memory hierarchical data.
  29. *
  30. * @see HierarchyData
  31. *
  32. * @author Vaadin Ltd
  33. * @since 8.1
  34. *
  35. * @param <T>
  36. * data type
  37. */
  38. public class InMemoryHierarchicalDataProvider<T> extends
  39. AbstractHierarchicalDataProvider<T, SerializablePredicate<T>> implements
  40. ConfigurableFilterDataProvider<T, SerializablePredicate<T>, SerializablePredicate<T>> {
  41. private final HierarchyData<T> hierarchyData;
  42. private SerializablePredicate<T> filter = null;
  43. private SerializableComparator<T> sortOrder = null;
  44. /**
  45. * Constructs a new InMemoryHierarchicalDataProvider.
  46. * <p>
  47. * All changes made to the given HierarchyData object will also be visible
  48. * through this data provider.
  49. *
  50. * @param hierarchyData
  51. * the backing HierarchyData for this provider
  52. */
  53. public InMemoryHierarchicalDataProvider(HierarchyData<T> hierarchyData) {
  54. this.hierarchyData = hierarchyData;
  55. }
  56. /**
  57. * Return the underlying hierarchical data of this provider.
  58. *
  59. * @return the underlying data of this provider
  60. */
  61. public HierarchyData<T> getData() {
  62. return hierarchyData;
  63. }
  64. @Override
  65. public boolean isInMemory() {
  66. return true;
  67. }
  68. @Override
  69. public boolean hasChildren(T item) {
  70. return !hierarchyData.getChildren(item).isEmpty();
  71. }
  72. @Override
  73. public int getChildCount(
  74. HierarchicalQuery<T, SerializablePredicate<T>> query) {
  75. return (int) fetchChildren(query).count();
  76. }
  77. @Override
  78. public Stream<T> fetchChildren(
  79. HierarchicalQuery<T, SerializablePredicate<T>> query) {
  80. Stream<T> childStream = getFilteredStream(
  81. hierarchyData.getChildren(query.getParent()).stream(),
  82. query.getFilter());
  83. Optional<Comparator<T>> comparing = Stream
  84. .of(query.getInMemorySorting(), sortOrder)
  85. .filter(c -> c != null)
  86. .reduce((c1, c2) -> c1.thenComparing(c2));
  87. if (comparing.isPresent()) {
  88. childStream = childStream.sorted(comparing.get());
  89. }
  90. return childStream.skip(query.getOffset()).limit(query.getLimit());
  91. }
  92. @Override
  93. public void setFilter(SerializablePredicate<T> filter) {
  94. this.filter = filter;
  95. refreshAll();
  96. }
  97. /**
  98. * Adds a filter to be applied to all queries. The filter will be used in
  99. * addition to any filter that has been set or added previously.
  100. *
  101. * @see #addFilter(ValueProvider, SerializablePredicate)
  102. * @see #addFilterByValue(ValueProvider, Object)
  103. * @see #setFilter(SerializablePredicate)
  104. *
  105. * @param filter
  106. * the filter to add, not <code>null</code>
  107. */
  108. public void addFilter(SerializablePredicate<T> filter) {
  109. Objects.requireNonNull(filter, "Filter cannot be null");
  110. if (this.filter == null) {
  111. setFilter(filter);
  112. } else {
  113. SerializablePredicate<T> oldFilter = this.filter;
  114. setFilter(item -> oldFilter.test(item) && filter.test(item));
  115. }
  116. }
  117. /**
  118. * Sets the comparator to use as the default sorting for this data provider.
  119. * This overrides the sorting set by any other method that manipulates the
  120. * default sorting of this data provider.
  121. * <p>
  122. * The default sorting is used if the query defines no sorting. The default
  123. * sorting is also used to determine the ordering of items that are
  124. * considered equal by the sorting defined in the query.
  125. *
  126. * @see #setSortOrder(ValueProvider, SortDirection)
  127. * @see #addSortComparator(SerializableComparator)
  128. *
  129. * @param comparator
  130. * a comparator to use, or <code>null</code> to clear any
  131. * previously set sort order
  132. */
  133. public void setSortComparator(SerializableComparator<T> comparator) {
  134. sortOrder = comparator;
  135. refreshAll();
  136. }
  137. /**
  138. * Adds a comparator to the default sorting for this data provider. If no
  139. * default sorting has been defined, then the provided comparator will be
  140. * used as the default sorting. If a default sorting has been defined, then
  141. * the provided comparator will be used to determine the ordering of items
  142. * that are considered equal by the previously defined default sorting.
  143. * <p>
  144. * The default sorting is used if the query defines no sorting. The default
  145. * sorting is also used to determine the ordering of items that are
  146. * considered equal by the sorting defined in the query.
  147. *
  148. * @see #setSortComparator(SerializableComparator)
  149. * @see #addSortOrder(ValueProvider, SortDirection)
  150. *
  151. * @param comparator
  152. * a comparator to add, not <code>null</code>
  153. */
  154. public void addSortComparator(SerializableComparator<T> comparator) {
  155. Objects.requireNonNull(comparator, "Sort order to add cannot be null");
  156. SerializableComparator<T> originalComparator = sortOrder;
  157. if (originalComparator == null) {
  158. setSortComparator(comparator);
  159. } else {
  160. setSortComparator((a, b) -> {
  161. int result = originalComparator.compare(a, b);
  162. if (result == 0) {
  163. result = comparator.compare(a, b);
  164. }
  165. return result;
  166. });
  167. }
  168. }
  169. @Override
  170. public <C> DataProvider<T, C> withConvertedFilter(
  171. SerializableFunction<C, SerializablePredicate<T>> filterConverter) {
  172. Objects.requireNonNull(filterConverter,
  173. "Filter converter can't be null");
  174. return new DataProviderWrapper<T, C, SerializablePredicate<T>>(this) {
  175. @Override
  176. protected SerializablePredicate<T> getFilter(Query<T, C> query) {
  177. return query.getFilter().map(filterConverter).orElse(null);
  178. }
  179. @Override
  180. public int size(Query<T, C> t) {
  181. if (t instanceof HierarchicalQuery<?, ?>) {
  182. return dataProvider.size(new HierarchicalQuery<>(
  183. t.getOffset(), t.getLimit(), t.getSortOrders(),
  184. t.getInMemorySorting(), getFilter(t),
  185. ((HierarchicalQuery<T, C>) t).getParent()));
  186. }
  187. throw new IllegalArgumentException(
  188. "Hierarchical data provider doesn't support non-hierarchical queries");
  189. }
  190. @Override
  191. public Stream<T> fetch(Query<T, C> t) {
  192. if (t instanceof HierarchicalQuery<?, ?>) {
  193. return dataProvider.fetch(new HierarchicalQuery<>(
  194. t.getOffset(), t.getLimit(), t.getSortOrders(),
  195. t.getInMemorySorting(), getFilter(t),
  196. ((HierarchicalQuery<T, C>) t).getParent()));
  197. }
  198. throw new IllegalArgumentException(
  199. "Hierarchical data provider doesn't support non-hierarchical queries");
  200. }
  201. };
  202. }
  203. private Stream<T> getFilteredStream(Stream<T> stream,
  204. Optional<SerializablePredicate<T>> queryFilter) {
  205. if (filter != null) {
  206. stream = stream.filter(filter);
  207. }
  208. return queryFilter.map(stream::filter).orElse(stream);
  209. }
  210. }