Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

DataSource.java 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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.client.data;
  17. import java.util.function.Consumer;
  18. import com.vaadin.shared.Range;
  19. import com.vaadin.shared.Registration;
  20. /**
  21. * Source of data for widgets showing lazily loaded data based on indexable
  22. * items (e.g. rows) of a specified type. The data source is a lazy view into a
  23. * larger data set.
  24. *
  25. * @since 7.4
  26. * @author Vaadin Ltd
  27. * @param <T>
  28. * the row type
  29. */
  30. public interface DataSource<T> {
  31. /**
  32. * A handle that contains information on whether a row should be
  33. * {@link #pin() pinned} or {@link #unpin() unpinned}, and also always the
  34. * most recent representation for that particular row.
  35. *
  36. * @param <T>
  37. * the row type
  38. */
  39. public abstract class RowHandle<T> {
  40. /**
  41. * Gets the most recent representation for the row this handle
  42. * represents.
  43. *
  44. * @return the most recent representation for the row this handle
  45. * represents
  46. * @throws IllegalStateException
  47. * if this row handle isn't currently pinned
  48. * @see #pin()
  49. */
  50. public abstract T getRow() throws IllegalStateException;
  51. /**
  52. * Marks this row as pinned.
  53. * <p>
  54. * <em>Note:</em> Pinning a row multiple times requires an equal amount
  55. * of unpins to free the row from the "pinned" status.
  56. * <p>
  57. * <em>Technical Note:</em> Pinning a row makes sure that the row object
  58. * for a particular set of data is always kept as up to date as the data
  59. * source is able to. Since the DataSource might create a new instance
  60. * of an object, object references aren't necessarily kept up-to-date.
  61. * This is a technical work-around for that.
  62. *
  63. * @see #unpin()
  64. */
  65. public abstract void pin();
  66. /**
  67. * Marks this row as unpinned.
  68. * <p>
  69. * <em>Note:</em> Pinning a row multiple times requires an equal amount
  70. * of unpins to free the row from the "pinned" status.
  71. * <p>
  72. * <em>Technical Note:</em> Pinning a row makes sure that the row object
  73. * for a particular set of data is always kept as up to date as the data
  74. * source is able to. Since the DataSource might create a new instance
  75. * of an object, object references aren't necessarily kept up-to-date.
  76. * This is a technical work-around for that.
  77. *
  78. * @throws IllegalStateException
  79. * if this row handle has not been pinned before
  80. * @see #pin()
  81. */
  82. public abstract void unpin() throws IllegalStateException;
  83. /**
  84. * Informs the DataSource that the row data represented by this
  85. * RowHandle has been updated. DataChangeHandler for the DataSource
  86. * should be informed that parts of data have been updated.
  87. *
  88. * @see DataChangeHandler#dataUpdated(int, int)
  89. */
  90. public abstract void updateRow();
  91. /**
  92. * An explicit override for {@link Object#equals(Object)}. This method
  93. * should be functionally equivalent to a properly implemented equals
  94. * method.
  95. * <p>
  96. * Having a properly implemented equals method is imperative for
  97. * RowHandle to function. Because Java has no mechanism to force an
  98. * override of an existing method, we're defining a new method for that
  99. * instead.
  100. *
  101. * @param obj
  102. * the reference object with which to compare
  103. * @return {@code true} if this object is the same as the obj argument;
  104. * {@code false} otherwise.
  105. */
  106. protected abstract boolean equalsExplicit(Object obj);
  107. /**
  108. * An explicit override for {@link Object#hashCode()}. This method
  109. * should be functionally equivalent to a properly implemented hashCode
  110. * method.
  111. * <p>
  112. * Having a properly implemented hashCode method is imperative for
  113. * RowHandle to function. Because Java has no mechanism to force an
  114. * override of an existing method, we're defining a new method for that
  115. * instead.
  116. *
  117. * @return a hash code value for this object
  118. */
  119. protected abstract int hashCodeExplicit();
  120. @Override
  121. public int hashCode() {
  122. return hashCodeExplicit();
  123. }
  124. @Override
  125. public boolean equals(Object obj) {
  126. return equalsExplicit(obj);
  127. }
  128. }
  129. /**
  130. * Informs the data source that data for the given range is needed. A data
  131. * source only has one active region at a time, so calling this method
  132. * discards the previously set range.
  133. * <p>
  134. * This method triggers lazy loading of data if necessary. The change
  135. * handler registered using {@link #addDataChangeHandler(DataChangeHandler)}
  136. * is informed when new data has been loaded.
  137. * <p>
  138. * After any possible lazy loading and updates are done, the change handler
  139. * is informed that new data is available.
  140. *
  141. * @param firstRowIndex
  142. * the index of the first needed row
  143. * @param numberOfRows
  144. * the number of needed rows
  145. */
  146. public void ensureAvailability(int firstRowIndex, int numberOfRows);
  147. /**
  148. * Retrieves the data for the row at the given index. If the row data is not
  149. * available, returns <code>null</code>.
  150. * <p>
  151. * This method does not trigger loading of unavailable data.
  152. * {@link #ensureAvailability(int, int)} should be used to signal what data
  153. * will be needed.
  154. *
  155. * @param rowIndex
  156. * the index of the row to retrieve data for
  157. * @return data for the row; or <code>null</code> if no data is available
  158. */
  159. public T getRow(int rowIndex);
  160. /**
  161. * Returns the number of rows in the data source.
  162. *
  163. * @return the current size of the data source
  164. */
  165. public int size();
  166. /**
  167. * Sets a data change handler to inform when data is updated, added or
  168. * removed.
  169. *
  170. * @param dataChangeHandler
  171. * the data change handler
  172. *
  173. * @return registration for removing the handler
  174. */
  175. public Registration addDataChangeHandler(
  176. DataChangeHandler dataChangeHandler);
  177. /**
  178. * Sets a simple data change handler for a widget without lazy loading.
  179. * Refresh method should reset all the data in the widget.
  180. *
  181. * @param refreshMethod
  182. * a method to refresh all data in the widget
  183. *
  184. * @return registration for removing the handler
  185. */
  186. public default Registration addDataChangeHandler(
  187. Consumer<Range> refreshMethod) {
  188. return addDataChangeHandler(
  189. new SimpleDataChangeHandler(this, refreshMethod));
  190. }
  191. /**
  192. * Gets a {@link RowHandle} of a row object in the cache.
  193. *
  194. * @param row
  195. * the row object for which to retrieve a row handle
  196. * @return a non-<code>null</code> row handle of the given row object
  197. * @throw IllegalStateException if this data source cannot be sure whether
  198. * or not the given row exists. <em>In practice</em> this usually
  199. * means that the row is not currently in this data source's cache.
  200. */
  201. public RowHandle<T> getHandle(T row);
  202. /**
  203. * Checks whether this data source is currently waiting for more rows to
  204. * become available.
  205. *
  206. * @return <code>true</code> if waiting for data; otherwise
  207. * <code>false</code>
  208. * @since 7.7.2
  209. */
  210. public boolean isWaitingForData();
  211. }