Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

InMemoryDataSource.java 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. * Copyright 2000-2014 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.tokka.server.communication.data;
  17. import java.util.ArrayList;
  18. import java.util.Collection;
  19. import java.util.Comparator;
  20. import java.util.List;
  21. import java.util.function.Function;
  22. import java.util.stream.Stream;
  23. /**
  24. * {@link DataSource} wrapper for {@link Collection}s.
  25. *
  26. * @param <T>
  27. * data type
  28. */
  29. public class InMemoryDataSource<T> implements DataSource<T, Comparator<T>> {
  30. // FIXME: Missing Query object
  31. private Function<Object, Stream<T>> request;
  32. /**
  33. * Constructs a new ListDataSource. This method makes a protective copy of
  34. * the contents of the Collection.
  35. *
  36. * @param collection
  37. * initial data
  38. */
  39. public InMemoryDataSource(Collection<T> collection) {
  40. final List<T> backend = new ArrayList<T>(collection);
  41. request = query -> backend.stream();
  42. }
  43. /**
  44. * Chaining constructor for making modified {@link InMemoryDataSource}s.
  45. * This Constructor is used internally for making sorted and filtered
  46. * variants of a base data source with actual data.
  47. *
  48. * @param request
  49. * request for the new data source
  50. */
  51. protected InMemoryDataSource(Function<Object, Stream<T>> request) {
  52. this.request = request;
  53. }
  54. @Override
  55. public Stream<T> apply(Object query) {
  56. return request.apply(query);
  57. }
  58. @Override
  59. public DataSource<T, Comparator<T>> sortingBy(Comparator<T> sortOrder) {
  60. return new InMemoryDataSource<T>(q -> request.apply(q)
  61. .sorted(sortOrder));
  62. }
  63. }