aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/pacien/lemonad/attempt/Attempt.java
blob: fc437add00cd49e2611284a94bfc37b6b0b0cbeb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
 * lemonad - Some functional sweetness for Java
 * Copyright (C) 2019  Pacien TRAN-GIRARD
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package org.pacien.lemonad.attempt;

import java.util.function.Consumer;
import java.util.function.Function;

import lombok.NonNull;

/**
 * Wraps either a value from a success or an error from a failure.
 *
 * @param <R> the potential wrapped result type.
 * @param <E> the potential error type.
 * @author pacien
 */
public interface Attempt<R, E> {
  /**
   * @return whether the {@link Attempt} is successful.
   */
  boolean isSuccess();

  /**
   * @return whether the {@link Attempt} is failed.
   */
  boolean isFailure();

  /**
   * @return the result if this {@link Attempt} is a success.
   * @throws java.util.NoSuchElementException if this {@link Attempt} is a failure.
   */
  R getResult();

  /**
   * @return the error if this {@link Attempt} is a failure.
   * @throws java.util.NoSuchElementException if this {@link Attempt} is a success.
   */
  E getError();

  /**
   * @param resultConsumer a {@link Consumer} of result called if the {@link Attempt} is a success.
   * @return the current {@link Attempt}
   */
  default Attempt<R, E> ifSuccess(@NonNull Consumer<? super R> resultConsumer) {
    if (isSuccess()) resultConsumer.accept(getResult());
    return this;
  }

  /**
   * @param errorConsumer a {@link Consumer} of error called if the {@link Attempt} is a failure.
   * @return the current {@link Attempt}
   */
  default Attempt<R, E> ifFailure(@NonNull Consumer<? super E> errorConsumer) {
    if (isFailure()) errorConsumer.accept(getError());
    return this;
  }

  /**
   * @param mapper a function producing an {@link Attempt}, called with the current result if this {@link Attempt} is a success.
   * @return this {@link Attempt} if it is a failure, or the produced one otherwise.
   */
  default <RR> Attempt<RR, E> mapResult(@NonNull Function<? super R, ? extends Attempt<? extends RR, ? extends E>> mapper) {
    //noinspection unchecked
    return (Attempt<RR, E>) (isSuccess() ? mapper.apply(getResult()) : this);
  }

  /**
   * @param mapper a function producing an {@link Attempt}, called with the current error if this {@link Attempt} is a failure.
   * @return this {@link Attempt} if it is a success, or the alternative {@link Attempt} retrieved from the supplier otherwise.
   */
  default <EE> Attempt<R, EE> mapFailure(@NonNull Function<? super E, ? extends Attempt<? extends R, ? extends EE>> mapper) {
    //noinspection unchecked
    return (Attempt<R, EE>) (isFailure() ? mapper.apply(getError()) : this);
  }

  /**
   * @param mapper a function transforming an {@link Attempt}.
   * @return the transformed {@link Attempt}
   */
  default <RR, EE> Attempt<RR, EE> flatMap(@NonNull Function<? super Attempt<? super R, ? super E>, ? extends Attempt<? extends RR, ? extends EE>> mapper) {
    //noinspection unchecked
    return (Attempt<RR, EE>) mapper.apply(this);
  }

  /**
   * @param result the result of the {@link Attempt}.
   * @return a successful {@link Attempt} wrapping the supplied result.
   */
  static <R, E> Attempt<R, E> success(R result) {
    return new Success<>(result);
  }

  /**
   * @param error the cause of the failure of the {@link Attempt}.
   * @return a failed {@link Attempt} with the supplied error.
   */
  static <R, E> Attempt<R, E> failure(E error) {
    return new Failure<>(error);
  }

  /**
   * @param supplier a {@code Supplier} that may throw an {@link Throwable}.
   * @return an {@link Attempt} wrapping either the result of the execution of the supplier or any thrown {@link Throwable}.
   */
  static <R, E extends Throwable> Attempt<R, E> attempt(@NonNull ThrowingSupplier<? extends R, ? extends E> supplier) {
    try {
      return success(supplier.get());
    } catch (Throwable throwable) {
      //noinspection unchecked
      return (Attempt<R, E>) failure(throwable);
    }
  }
}