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.

DataSource.java 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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.io.Serializable;
  18. import java.util.Arrays;
  19. import java.util.Collection;
  20. import com.vaadin.tokka.event.Registration;
  21. /**
  22. * Minimal DataSource API for communication between the DataProvider and a back
  23. * end service.
  24. *
  25. * @since
  26. * @param <T>
  27. * data type
  28. */
  29. public interface DataSource<T> extends Iterable<T>, Serializable {
  30. /**
  31. * Saves a data object to the back end. If it's a new object, it should be
  32. * created in the back end. Existing objects with changes should be stored.
  33. *
  34. * @param data
  35. * data object to save
  36. */
  37. void save(T data);
  38. /**
  39. * Removes the given data object from the back end.
  40. *
  41. * @param data
  42. * data object to remove
  43. */
  44. void remove(T data);
  45. /**
  46. * Adds a new DataChangeHandler to this DataSource. DataChangeHandler is
  47. * called when changes occur in DataSource.
  48. *
  49. * @param handler
  50. * data change handler
  51. */
  52. Registration addDataChangeHandler(DataChangeHandler<T> handler);
  53. /**
  54. * This method creates a new {@link ListDataSource} from a given Collection.
  55. * The ListDataSource creates a protective List copy of all the contents in
  56. * the Collection.
  57. *
  58. * @param data
  59. * collection of data
  60. * @return list data source
  61. */
  62. public static <T> ListDataSource<T> create(Collection<T> data) {
  63. return new ListDataSource<>(data);
  64. }
  65. /**
  66. * This method creates a new {@link ListDataSource} from given objects.
  67. *
  68. * @param data
  69. * data objects
  70. * @return list data source
  71. */
  72. public static <T> ListDataSource<T> create(T... data) {
  73. return new ListDataSource<>(Arrays.asList(data));
  74. }
  75. }