View Javadoc
1   /**************************************************************************
2    *
3    * Copyright (c) 2019-2020 Yawg project contributors.
4    *
5    **************************************************************************/
6   
7   package com.varmateo.yawg.util;
8   
9   import io.vavr.control.Try;
10  
11  import com.varmateo.yawg.api.Result;
12  import com.varmateo.yawg.api.YawgException;
13  
14  
15  /**
16   *
17   */
18  public final class Results {
19  
20      private Results() {
21          // Nothing to do.
22      }
23  
24  
25      /**
26       *
27       */
28      public static <T> Result<T> fromTry(final Try<T> tryResult) {
29  
30          return tryResult
31                  .map(Result::success)
32                  .recover(YawgException.class, Result::failure)
33                  .get();
34      }
35  
36  
37      /**
38       *
39       */
40      public static <T> Try<T> toTry(final Result<T> value) {
41  
42          final Try<T> result;
43  
44          if (value.isSuccess()) {
45              result = Try.success(value.get());
46          } else {
47              result = Try.failure(value.failureCause());
48          }
49  
50          return result;
51      }
52  }