Skip to content

Commit

Permalink
Add three-argument overload for map to Either3
Browse files Browse the repository at this point in the history
This is a follow-up to eclipse-lsp4j#546
  • Loading branch information
pisv committed Apr 21, 2021
1 parent 94fd735 commit 530713a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ public boolean isRight() {
return right != null;
}

public <T> T map(@NonNull Function<? super L, ? extends T> mapLeft, @NonNull Function <? super R, ? extends T> mapRight) {
public <T> T map(
@NonNull Function<? super L, ? extends T> mapLeft,
@NonNull Function<? super R, ? extends T> mapRight) {
if (isLeft()) {
return mapLeft.apply(getLeft());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
******************************************************************************/
package org.eclipse.lsp4j.jsonrpc.messages;

import java.util.function.Function;

import org.eclipse.lsp4j.jsonrpc.validation.NonNull;

/**
Expand Down Expand Up @@ -81,6 +83,22 @@ public boolean isThird() {
return isRight() && getRight().isRight();
}

public <T> T map(
@NonNull Function<? super T1, ? extends T> mapFirst,
@NonNull Function<? super T2, ? extends T> mapSecond,
@NonNull Function<? super T3, ? extends T> mapThird) {
if (isFirst()) {
return mapFirst.apply(getFirst());
}
if (isSecond()) {
return mapSecond.apply(getSecond());
}
if (isThird()) {
return mapThird.apply(getThird());
}
return null;
}

@Override
public String toString() {
StringBuilder builder = new StringBuilder("Either3 [").append(System.lineSeparator());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import static org.junit.Assert.assertTrue;

import java.util.Objects;
import java.util.function.Function;

import org.eclipse.lsp4j.jsonrpc.json.adapters.EitherTypeAdapter;
import org.eclipse.lsp4j.jsonrpc.messages.Either;
Expand Down Expand Up @@ -110,16 +111,33 @@ public void testEqualsFalseWithNonNull() {

@Test
public void testMap() {
Either<byte[], String> either =Either.forLeft(new byte[] { 'f', 'o', 'o'});
Either<char[], String> either = Either.forLeft(new char[] { 'f', 'o', 'o' });
assertEquals("foo", either.map(
String::new,
String::toUpperCase));
either = Either.forRight("barlol");
assertEquals("BARLOL", either.map(
either = Either.forRight("bar");
assertEquals("BAR", either.map(
String::new,
String::toUpperCase));
}

@Test
public void testMapEither3() {
Either3<String, Integer, Boolean> either3;
Function<String, String> mapFirst = s -> s.toUpperCase();
Function<Integer, String> mapSecond = x -> x.toString();
Function<Boolean, String> mapThird = b -> b.toString();

either3 = Either3.forFirst("abc");
assertEquals("ABC", either3.map(mapFirst, mapSecond, mapThird));

either3 = Either3.forSecond(123);
assertEquals("123", either3.map(mapFirst, mapSecond, mapThird));

either3 = Either3.forThird(Boolean.TRUE);
assertEquals("true", either3.map(mapFirst, mapSecond, mapThird));
}

protected static class MyObjectA {
public Either<String, Integer> myProperty;
public String otherProperty;
Expand Down

0 comments on commit 530713a

Please sign in to comment.