• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

yuu-nkjm / sorm4j / #542

14 Mar 2025 07:20AM UTC coverage: 94.877% (-0.05%) from 94.924%
#542

push

yuu-nkjm
Modify Try util

837 of 942 branches covered (88.85%)

Branch coverage included in aggregate %.

46 of 51 new or added lines in 12 files covered. (90.2%)

12 existing lines in 3 files now uncovered.

4274 of 4445 relevant lines covered (96.15%)

0.96 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.5
/sorm4j/src/main/java/org/nkjmlab/sorm4j/util/function/exception/Try.java
1
package org.nkjmlab.sorm4j.util.function.exception;
2

3
import java.util.function.Consumer;
4
import java.util.function.Function;
5

6
/**
7
 * Utility class for handling exceptions in functional operations in a "Try-style" manner. Provides
8
 * static methods to handle checked exceptions in a functional way.
9
 */
10
public final class Try {
11

12
  /**
13
   * Executes the given supplier and returns its result. If an exception occurs, returns the
14
   * provided alternative value.
15
   *
16
   * @param <T> the type of the result
17
   * @param trySupplier the supplier that may throw an exception
18
   * @param other the value to return if an exception occurs
19
   * @return the result of {@code trySupplier}, or {@code other} if an exception occurs
20
   */
21
  public static <T> T getOrElse(TrySupplier<T> trySupplier, T other) {
22
    return TrySupplier.toSupplier(trySupplier, e -> other).get();
1✔
23
  }
24

25
  /**
26
   * Executes the given supplier and returns its result. If an exception occurs, applies the given
27
   * function to generate an alternative value.
28
   *
29
   * @param <T> the type of the result
30
   * @param trySupplier the supplier that may throw an exception
31
   * @param otherSupplier the function that provides an alternative value in case of an exception
32
   * @return the result of {@code trySupplier}, or the result of {@code otherSupplier.apply(e)} if
33
   *     an exception occurs
34
   */
35
  public static <T> T getOrElseGet(
36
      TrySupplier<T> trySupplier, Function<Exception, T> otherSupplier) {
37
    return TrySupplier.toSupplier(trySupplier, e -> otherSupplier.apply(e)).get();
1✔
38
  }
39

40
  /**
41
   * Executes the given supplier and returns its result. If an exception occurs, returns {@code
42
   * null}.
43
   *
44
   * @param <T> the type of the result
45
   * @param trySupplier the supplier that may throw an exception
46
   * @return the result of {@code trySupplier}, or {@code null} if an exception occurs
47
   */
48
  public static <T> T getOrElseNull(TrySupplier<T> trySupplier) {
49
    return TrySupplier.toSupplier(trySupplier, e -> null).get();
1✔
50
  }
51

52
  /**
53
   * Executes the given supplier and returns its result. If an exception occurs, throws an exception
54
   * generated by the provided function.
55
   *
56
   * @param <T> the type of the result
57
   * @param <X> the type of the exception to be thrown
58
   * @param trySupplier the supplier that may throw an exception
59
   * @param exceptionFunction the function that generates the exception to be thrown
60
   * @return the result of {@code trySupplier}
61
   * @throws X if an exception occurs during execution
62
   */
63
  public static <T, X extends RuntimeException> T getOrElseThrow(
64
      TrySupplier<T> trySupplier, Function<Exception, ? extends X> exceptionFunction) throws X {
65
    return TrySupplier.toSupplier(
1✔
66
            trySupplier,
67
            e -> {
68
              throw exceptionFunction.apply(e);
1✔
69
            })
70
        .get();
1✔
71
  }
72

73
  /**
74
   * Executes the given supplier and returns its result. If an exception occurs, rethrows it as an
75
   * unchecked exception.
76
   *
77
   * @param <T> the type of the result
78
   * @param trySupplier the supplier that may throw an exception
79
   * @return the result of {@code trySupplier}
80
   */
81
  public static <T> T getOrElseThrow(TrySupplier<T> trySupplier) {
82
    return getOrElseThrow(trySupplier, e -> Try.rethrow(e));
1✔
83
  }
84

85
  /**
86
   * Rethrows the given throwable as a runtime exception.
87
   *
88
   * @param <T> the type of the throwable
89
   * @param throwable the throwable to be rethrown
90
   * @return never returns (this method always throws an exception)
91
   * @throws T always throws the given throwable
92
   */
93
  @SuppressWarnings("unchecked")
94
  public static <T extends Throwable> RuntimeException rethrow(Throwable throwable) throws T {
95
    throw (T) throwable;
1✔
96
  }
97

98
  /**
99
   * Executes the given runnable. If an exception occurs, applies the given handler function.
100
   *
101
   * @param tryRunnable the runnable that may throw an exception
102
   * @param exceptionHandler the consumer that handles the exception
103
   */
104
  public static void runOrHandle(TryRunnable tryRunnable, Consumer<Exception> exceptionHandler) {
105
    TryRunnable.toRunnable(tryRunnable, exceptionHandler).run();
1✔
106
  }
1✔
107

108
  /**
109
   * Executes the given runnable. If an exception occurs, throws an exception generated by the
110
   * provided function.
111
   *
112
   * @param <X> the type of the exception to be thrown
113
   * @param tryRunnable the runnable that may throw an exception
114
   * @param exceptionFunction the function that generates the exception to be thrown
115
   * @throws X if an exception occurs during execution
116
   */
117
  public static <T, X extends RuntimeException> void runOrThrow(
118
      TryRunnable tryRunnable, Function<Exception, ? extends X> exceptionFunction) throws X {
119
    TryRunnable.toRunnable(
1✔
120
            tryRunnable,
121
            e -> {
122
              throw exceptionFunction.apply(e);
1✔
123
            })
124
        .run();
1✔
125
  }
1✔
126

127
  /**
128
   * Executes the given runnable. If an exception occurs, rethrows it as an unchecked exception.
129
   *
130
   * @param tryRunnable the runnable that may throw an exception
131
   */
132
  public static <T> void runOrThrow(TryRunnable tryRunnable) {
NEW
133
    runOrThrow(tryRunnable, e -> Try.rethrow(e));
×
UNCOV
134
  }
×
135

136
  /** Private constructor to prevent instantiation. */
137
  private Try() {}
138
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc