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.

Buffered.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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.data;
  17. import java.io.Serializable;
  18. import com.vaadin.data.Validator.InvalidValueException;
  19. /**
  20. * <p>
  21. * Defines the interface to commit and discard changes to an object, supporting
  22. * buffering.
  23. *
  24. * <p>
  25. * In <i>buffered</i> mode the initial value is read from the data source and
  26. * then buffered. Any subsequential writes or reads will be done on the buffered
  27. * value. Calling {@link #commit()} will write the buffered value to the data
  28. * source while calling {@link #discard()} while discard the buffered value and
  29. * re-read the value from the data source.
  30. *
  31. * <p>
  32. * In <i>non-buffered</i> mode the value is always read directly from the data
  33. * source. Any write is done directly to the data source with no buffering in
  34. * between. Reads are also done directly from the data source. Calling
  35. * {@link #commit()} or {@link #discard()} in this mode is efficiently a no-op.
  36. *
  37. * @author Vaadin Ltd.
  38. * @since 3.0
  39. */
  40. public interface Buffered extends Serializable {
  41. /**
  42. * Updates all changes since the previous commit to the data source. The
  43. * value stored in the object will always be updated into the data source
  44. * when <code>commit</code> is called.
  45. *
  46. * @throws SourceException
  47. * if the operation fails because of an exception is thrown by
  48. * the data source. The cause is included in the exception.
  49. * @throws InvalidValueException
  50. * if the operation fails because validation is enabled and the
  51. * values do not validate
  52. */
  53. public void commit() throws SourceException, InvalidValueException;
  54. /**
  55. * Discards all changes since last commit. The object updates its value from
  56. * the data source.
  57. *
  58. * @throws SourceException
  59. * if the operation fails because of an exception is thrown by
  60. * the data source. The cause is included in the exception.
  61. */
  62. public void discard() throws SourceException;
  63. /**
  64. * Sets the buffered mode to the specified status.
  65. * <p>
  66. * When in buffered mode, an internal buffer will be used to store changes
  67. * until {@link #commit()} is called. Calling {@link #discard()} will revert
  68. * the internal buffer to the value of the data source.
  69. * <p>
  70. * When in non-buffered mode both read and write operations will be done
  71. * directly on the data source. In this mode the {@link #commit()} and
  72. * {@link #discard()} methods serve no purpose.
  73. *
  74. * @param buffered
  75. * true if buffered mode should be turned on, false otherwise
  76. * @since 7.0
  77. */
  78. public void setBuffered(boolean buffered);
  79. /**
  80. * Checks the buffered mode
  81. *
  82. * @return true if buffered mode is on, false otherwise
  83. * @since 7.0
  84. */
  85. public boolean isBuffered();
  86. /**
  87. * Tests if the value stored in the object has been modified since it was
  88. * last updated from the data source.
  89. *
  90. * @return <code>true</code> if the value in the object has been modified
  91. * since the last data source update, <code>false</code> if not.
  92. */
  93. public boolean isModified();
  94. /**
  95. * An exception that signals that one or more exceptions occurred while a
  96. * buffered object tried to access its data source or if there is a problem
  97. * in processing a data source.
  98. *
  99. * @author Vaadin Ltd.
  100. * @since 3.0
  101. */
  102. @SuppressWarnings("serial")
  103. public class SourceException extends RuntimeException implements
  104. Serializable {
  105. /** Source class implementing the buffered interface */
  106. private final Buffered source;
  107. /** Original cause of the source exception */
  108. private Throwable[] causes = {};
  109. /**
  110. * Creates a source exception that does not include a cause.
  111. *
  112. * @param source
  113. * the source object implementing the Buffered interface.
  114. */
  115. public SourceException(Buffered source) {
  116. this.source = source;
  117. }
  118. /**
  119. * Creates a source exception from multiple causes.
  120. *
  121. * @param source
  122. * the source object implementing the Buffered interface.
  123. * @param causes
  124. * the original causes for this exception.
  125. */
  126. public SourceException(Buffered source, Throwable... causes) {
  127. this.source = source;
  128. this.causes = causes;
  129. }
  130. /**
  131. * Gets the cause of the exception.
  132. *
  133. * @return The (first) cause for the exception, null if no cause.
  134. */
  135. @Override
  136. public final Throwable getCause() {
  137. if (causes.length == 0) {
  138. return null;
  139. }
  140. return causes[0];
  141. }
  142. /**
  143. * Gets all the causes for this exception.
  144. *
  145. * @return throwables that caused this exception
  146. */
  147. public final Throwable[] getCauses() {
  148. return causes;
  149. }
  150. /**
  151. * Gets a source of the exception.
  152. *
  153. * @return the Buffered object which generated this exception.
  154. */
  155. public Buffered getSource() {
  156. return source;
  157. }
  158. }
  159. }