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.

Try.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. package org.apache.archiva.common;
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. import java.util.function.Function;
  21. /**
  22. * This is a class that can be used for the Try monad
  23. *
  24. * The Try monad is able to collect exceptions during processing
  25. * of a stream.
  26. *
  27. *
  28. *
  29. */
  30. public abstract class Try<V> {
  31. private Try() {
  32. }
  33. public abstract Boolean isSuccess();
  34. public abstract Boolean isFailure();
  35. public abstract void throwException();
  36. /**
  37. * Returns the value if this is a success instance. Otherwise throws
  38. * a runtime exception with the stored throwable as cause.
  39. *
  40. * @return The value
  41. */
  42. public abstract V get();
  43. /**
  44. * Returns the throwable that is stored in the failure.
  45. *
  46. * @return The Throwable or null.
  47. */
  48. public abstract Throwable getError();
  49. /**
  50. * A mapping method for mapping the current instance to a new type.
  51. *
  52. * @param fn
  53. * @param <U>
  54. * @return
  55. */
  56. public <U> Try<U> map(Function<? super V, U> fn) {
  57. try {
  58. return Try.success(fn.apply(get()));
  59. } catch (Throwable e) {
  60. return Try.failure(e);
  61. }
  62. }
  63. /**
  64. * This is the bind method.
  65. * If this instance is success the function will be applied. If any error occurs
  66. * a failure instance will be returned.
  67. * If this instance is failure a new failure will be returned.
  68. *
  69. * @param fn
  70. * @param <U>
  71. * @return
  72. */
  73. public <U> Try<U> flatMap(Function<? super V, Try<U>> fn) {
  74. try {
  75. return fn.apply(get());
  76. } catch (Throwable t) {
  77. return Try.failure(t);
  78. }
  79. }
  80. public static <V> Try<V> failure(String message) {
  81. return new Failure<>(message);
  82. }
  83. public static <V> Try<V> failure(String message, Throwable e) {
  84. return new Failure<>(message, e);
  85. }
  86. /**
  87. * If you need type coercion, you should call this method as
  88. * Try.&lt;YOUR_TYPE&gt;failure(e)
  89. *
  90. *
  91. *
  92. * @param e The exception that is thrown
  93. * @param <V> The generic type this monad keeps
  94. * @return A new Try instance that represents a failure.
  95. */
  96. public static <V> Try<V> failure(Throwable e) {
  97. return new Failure<>(e);
  98. }
  99. /**
  100. * Returns a instance for the success case.
  101. *
  102. * @param value The value that should be stored.
  103. * @param <V> The return type
  104. * @return A new Try instance with the given value
  105. */
  106. public static <V> Try<V> success(V value) {
  107. return new Success<>(value);
  108. }
  109. private static class Failure<V> extends Try<V> {
  110. private Throwable exception;
  111. public Failure(String message) {
  112. super();
  113. this.exception = new IllegalStateException(message);
  114. }
  115. public Failure(String message, Throwable e) {
  116. super();
  117. this.exception = new IllegalStateException(message, e);
  118. }
  119. public Failure(Throwable e) {
  120. super();
  121. this.exception = new IllegalStateException(e);
  122. }
  123. @Override
  124. public Boolean isSuccess() {
  125. return false;
  126. }
  127. @Override
  128. public Boolean isFailure() {
  129. return true;
  130. }
  131. @Override
  132. public void throwException() {
  133. throw new RuntimeException(this.exception);
  134. }
  135. @Override
  136. public V get() {
  137. throw new RuntimeException(this.exception);
  138. }
  139. @Override
  140. public Throwable getError() {
  141. return exception;
  142. }
  143. }
  144. private static class Success<V> extends Try<V> {
  145. private V value;
  146. public Success(V value) {
  147. super();
  148. this.value = value;
  149. }
  150. @Override
  151. public Boolean isSuccess() {
  152. return true;
  153. }
  154. @Override
  155. public Boolean isFailure() {
  156. return false;
  157. }
  158. @Override
  159. public void throwException() {
  160. //log.error("Method throwException() called on a Success instance");
  161. }
  162. @Override
  163. public V get() {
  164. return value;
  165. }
  166. @Override
  167. public Throwable getError() {
  168. return null;
  169. }
  170. }
  171. // various method such as map an flatMap
  172. @Override
  173. public String toString() {
  174. return isSuccess() ? "true: "+get() : "false: "+ getError().getMessage();
  175. }
  176. }