File tree Expand file tree Collapse file tree 2 files changed +24
-0
lines changed
main/java/com/hubspot/algebra
test/java/com/hubspot/algebra Expand file tree Collapse file tree 2 files changed +24
-0
lines changed Original file line number Diff line number Diff line change @@ -44,6 +44,11 @@ public void ifErr(Consumer<? super ERROR_TYPE> consumer) {
4444 Results .getErr (this ).ifPresent (consumer );
4545 }
4646
47+ public void consume (Consumer <? super ERROR_TYPE > errConsumer , Consumer <? super SUCCESS_TYPE > okConsumer ) {
48+ ifOk (okConsumer );
49+ ifErr (errConsumer );
50+ }
51+
4752 public <NEW_ERROR_TYPE > Result <SUCCESS_TYPE , NEW_ERROR_TYPE > mapErr (Function <ERROR_TYPE , NEW_ERROR_TYPE > mapper ) {
4853 return Results .<SUCCESS_TYPE , ERROR_TYPE , NEW_ERROR_TYPE >modErr (mapper ).apply (this );
4954 }
Original file line number Diff line number Diff line change 44import static org .assertj .core .api .Assertions .assertThatExceptionOfType ;
55
66import java .util .ArrayList ;
7+ import java .util .Collections ;
78import java .util .List ;
89import java .util .function .Consumer ;
910import java .util .function .Function ;
@@ -172,4 +173,22 @@ public void itAllowsConsumerOfErrSuperType() {
172173 result .ifErr (consumer );
173174 assertThat (!check .isEmpty ());
174175 }
176+
177+ @ Test
178+ public void itConsumesOks () throws Exception {
179+ List <String > okResults = new ArrayList <>();
180+ List <SampleError > errorResults = Collections .emptyList ();
181+ OK_RESULT .consume (errorResults ::add , okResults ::add );
182+ assertThat (okResults ).contains (OK_RESULT .unwrapOrElseThrow ());
183+ assertThat (errorResults ).isEmpty ();
184+ }
185+
186+ @ Test
187+ public void itConsumesErrors () throws Exception {
188+ List <String > okResults = Collections .emptyList ();
189+ List <SampleError > errorResults = new ArrayList <>();
190+ ERR_RESULT .consume (errorResults ::add , okResults ::add );
191+ assertThat (okResults ).isEmpty ();
192+ assertThat (errorResults ).contains (ERR_RESULT .unwrapErrOrElseThrow ());
193+ }
175194}
You can’t perform that action at this time.
0 commit comments