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.

AbstractDataSource.java 2.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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.LinkedHashSet;
  18. import java.util.Set;
  19. import com.vaadin.tokka.event.Registration;
  20. /**
  21. * Base class for AbstractDataSource. Provides tracking for
  22. * {@link DataChangeHandler}s and helper methods to call them.
  23. *
  24. * @since
  25. */
  26. public abstract class AbstractDataSource<T> implements DataSource<T> {
  27. protected final Set<DataChangeHandler<T>> handlers = new LinkedHashSet<DataChangeHandler<T>>();
  28. @Override
  29. public Registration addDataChangeHandler(DataChangeHandler<T> handler) {
  30. if (handler != null) {
  31. handlers.add(handler);
  32. return () -> handlers.remove(handler);
  33. }
  34. return () -> { /* NO-OP */ };
  35. }
  36. /**
  37. * Informs all handlers of a generic change in the data. This usually
  38. * triggers a full cache invalidation and refresh of all data, which is
  39. * usually an expensive operation. Should be called when none of the other
  40. * methods help.
  41. *
  42. * @see #fireDataAppend(Object)
  43. * @see #fireDataRemove(Object)
  44. * @see #fireDataUpdate(Object)
  45. */
  46. protected void fireDataChange() {
  47. for (DataChangeHandler<T> handler : handlers) {
  48. handler.onDataChange();
  49. }
  50. }
  51. /**
  52. * Informs all handlers of an added data object in the back end. This method
  53. * should only be called when the newly added data object is the last object
  54. * in the back end. Other additions can be handled with
  55. * {@link #fireDataChange()}.
  56. *
  57. * @param data
  58. * added data object
  59. */
  60. protected void fireDataAppend(T data) {
  61. for (DataChangeHandler<T> handler : handlers) {
  62. handler.onDataAppend(data);
  63. }
  64. }
  65. /**
  66. * Informs all handlers of a data object that was removed from the back end.
  67. *
  68. * @param data
  69. * removed data object
  70. */
  71. protected void fireDataRemove(T data) {
  72. for (DataChangeHandler<T> handler : handlers) {
  73. handler.onDataRemove(data);
  74. }
  75. }
  76. /**
  77. * Informs all handlers of an existing data object that was updated in the
  78. * back end.
  79. *
  80. * @param data
  81. * updated data object
  82. */
  83. protected void fireDataUpdate(T data) {
  84. for (DataChangeHandler<T> handler : handlers) {
  85. handler.onDataUpdate(data);
  86. }
  87. }
  88. }