aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/pacien/lemonad/attempt/Attempt.java
blob: 574b6ed4a145008ce0bd431490de828822a65253 (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
 * 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 transformer 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> transformResult(@NonNull Function<? super R, ? extends Attempt<? extends RR, ? extends E>> transformer) {
    //noinspection unchecked
    return (Attempt<RR, E>) (isSuccess() ? transformer.apply(getResult()) : this);
  }

  /**
   * @param transformer  a function producing an {@link Attempt}, called with the current result if this {@link Attempt} is a success.
   * @param errorAdapter a function adapting any intermediate error returned by the {@code transformer} function.
   * @return this {@link Attempt} if it is a failure, or the produced one otherwise.
   */
  default <RR, IE> Attempt<RR, E> transformResult(@NonNull Function<? super R, ? extends Attempt<? extends RR, ? extends IE>> transformer,
                                                  @NonNull Function<? super IE, ? extends E> errorAdapter) {
    return transformResult(transformer.andThen(attempt -> attempt.recoverError(errorAdapter.andThen(Attempt::failure))));
  }

  /**
   * @param mapper a function mapping used to map any result if this {@link Attempt} is a success.
   * @return this {@link Attempt} if it is a failure, or the mutated one otherwise.
   */
  default <RR> Attempt<RR, E> mapResult(@NonNull Function<? super R, ? extends RR> mapper) {
    return transformResult(mapper.andThen(Attempt::success));
  }

  /**
   * @param recoverer 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> recoverError(@NonNull Function<? super E, ? extends Attempt<? extends R, ? extends EE>> recoverer) {
    //noinspection unchecked
    return (Attempt<R, EE>) (isFailure() ? recoverer.apply(getError()) : this);
  }

  /**
   * @param recoverer     a function producing an {@link Attempt}, called with the current error if this {@link Attempt} is a failure.
   * @param resultAdapter a function adapting any intermediate result returned by the {@code recoverer} function.
   * @return this {@link Attempt} if it is a success, or the alternative {@link Attempt} retrieved from the supplier otherwise.
   */
  default <IR, EE> Attempt<R, EE> recoverError(@NonNull Function<? super E, ? extends Attempt<? extends IR, ? extends EE>> recoverer,
                                               @NonNull Function<? super IR, ? extends R> resultAdapter) {
    return recoverError(recoverer.andThen(attempt -> attempt.transformResult(resultAdapter.andThen(Attempt::success))));
  }

  /**
   * @param mapper a function mapping used to map any result if this {@link Attempt} is a failure.
   * @return this {@link Attempt} if it is a success, or the mutated one otherwise.
   */
  default <EE> Attempt<R, EE> mapError(@NonNull Function<? super E, ? extends EE> mapper) {
    return recoverError(mapper.andThen(Attempt::failure));
  }

  /**
   * @param resultTransformer a function producing an {@link Attempt}, called with the current result if this {@link Attempt} is a success.
   * @param errorTransformer  a function producing an {@link Attempt}, called with the current error if this {@link Attempt} is a failure.
   * @return the transformed {@link Attempt}.
   */
  default <RR, EE> Attempt<RR, EE> transform(@NonNull Function<? super R, ? extends Attempt<? extends RR, ? extends EE>> resultTransformer,
                                             @NonNull Function<? super E, ? extends Attempt<? extends RR, ? extends EE>> errorTransformer) {
    //noinspection unchecked
    return (Attempt<RR, EE>) (isSuccess() ? resultTransformer.apply(getResult()) : errorTransformer.apply(getError()));
  }

  /**
   * @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);
    }
  }
}