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.

Result.java 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * Copyright 2000-2018 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 java.util.Objects;
  19. import java.util.Optional;
  20. import com.vaadin.server.SerializableConsumer;
  21. import com.vaadin.server.SerializableFunction;
  22. import com.vaadin.server.SerializableSupplier;
  23. /**
  24. * Represents the result of an operation that might fail, such as type
  25. * conversion. A result may contain either a value, signifying a successful
  26. * operation, or an error message in case of a failure.
  27. * <p>
  28. * Result instances are created using the factory methods {@link #ok(R)} and
  29. * {@link #error(String)}, denoting success and failure respectively.
  30. * <p>
  31. * Unless otherwise specified, {@code Result} method arguments cannot be null.
  32. *
  33. * @param <R>
  34. * the result value type
  35. *
  36. * @since 8.0
  37. */
  38. public interface Result<R> extends Serializable {
  39. /**
  40. * Returns a successful result wrapping the given value.
  41. *
  42. * @param <R>
  43. * the result value type
  44. * @param value
  45. * the result value, can be null
  46. * @return a successful result
  47. */
  48. public static <R> Result<R> ok(R value) {
  49. return new SimpleResult<>(value, null);
  50. }
  51. /**
  52. * Returns a failure result wrapping the given error message.
  53. *
  54. * @param <R>
  55. * the result value type
  56. * @param message
  57. * the error message
  58. * @return a failure result
  59. */
  60. public static <R> Result<R> error(String message) {
  61. Objects.requireNonNull(message, "message cannot be null");
  62. return new SimpleResult<>(null, message);
  63. }
  64. /**
  65. * Returns a Result representing the result of invoking the given supplier.
  66. * If the supplier returns a value, returns a {@code Result.ok} of the
  67. * value; if an exception is thrown, returns the message in a
  68. * {@code Result.error}.
  69. *
  70. * @param <R>
  71. * the result value type
  72. * @param supplier
  73. * the supplier to run
  74. * @param onError
  75. * the function to provide the error message
  76. * @return the result of invoking the supplier
  77. */
  78. public static <R> Result<R> of(SerializableSupplier<R> supplier,
  79. SerializableFunction<Exception, String> onError) {
  80. Objects.requireNonNull(supplier, "supplier cannot be null");
  81. Objects.requireNonNull(onError, "onError cannot be null");
  82. try {
  83. return ok(supplier.get());
  84. } catch (Exception e) {
  85. return error(onError.apply(e));
  86. }
  87. }
  88. /**
  89. * If this Result has a value, returns a Result of applying the given
  90. * function to the value. Otherwise, returns a Result bearing the same error
  91. * as this one. Note that any exceptions thrown by the mapping function are
  92. * not wrapped but allowed to propagate.
  93. *
  94. * @param <S>
  95. * the type of the mapped value
  96. * @param mapper
  97. * the mapping function
  98. * @return the mapped result
  99. */
  100. public default <S> Result<S> map(SerializableFunction<R, S> mapper) {
  101. return flatMap(value -> ok(mapper.apply(value)));
  102. }
  103. /**
  104. * If this Result has a value, applies the given Result-returning function
  105. * to the value. Otherwise, returns a Result bearing the same error as this
  106. * one. Note that any exceptions thrown by the mapping function are not
  107. * wrapped but allowed to propagate.
  108. *
  109. * @param <S>
  110. * the type of the mapped value
  111. * @param mapper
  112. * the mapping function
  113. * @return the mapped result
  114. */
  115. public <S> Result<S> flatMap(SerializableFunction<R, Result<S>> mapper);
  116. /**
  117. * Invokes either the first callback or the second one, depending on whether
  118. * this Result denotes a success or a failure, respectively.
  119. *
  120. * @param ifOk
  121. * the function to call if success
  122. * @param ifError
  123. * the function to call if failure
  124. */
  125. public void handle(SerializableConsumer<R> ifOk,
  126. SerializableConsumer<String> ifError);
  127. /**
  128. * Applies the {@code consumer} if result is not an error.
  129. *
  130. * @param consumer
  131. * consumer to apply in case it's not an error
  132. */
  133. public default void ifOk(SerializableConsumer<R> consumer) {
  134. handle(consumer, error -> {
  135. });
  136. }
  137. /**
  138. * Applies the {@code consumer} if result is an error.
  139. *
  140. * @param consumer
  141. * consumer to apply in case it's an error
  142. */
  143. public default void ifError(SerializableConsumer<String> consumer) {
  144. handle(value -> {
  145. }, consumer);
  146. }
  147. /**
  148. * Checks if the result denotes an error.
  149. *
  150. * @return <code>true</code> if the result denotes an error,
  151. * <code>false</code> otherwise
  152. */
  153. public boolean isError();
  154. /**
  155. * Returns an Optional of the result message, or an empty Optional if none.
  156. *
  157. * @return the optional message
  158. */
  159. public Optional<String> getMessage();
  160. /**
  161. * Return the value, if the result denotes success, otherwise throw an
  162. * exception to be created by the provided supplier.
  163. *
  164. * @param <X>
  165. * Type of the exception to be thrown
  166. * @param exceptionProvider
  167. * The provider which will return the exception to be thrown
  168. * based on the given error message
  169. * @return the value
  170. * @throws X
  171. * if this result denotes an error
  172. */
  173. public <X extends Throwable> R getOrThrow(
  174. SerializableFunction<String, ? extends X> exceptionProvider)
  175. throws X;
  176. }