Skip to content

Commit

Permalink
Add Either.map(...)
Browse files Browse the repository at this point in the history
Many case of usage of Either in LSP4E involve turning Either into a
value. That requires some boilerplate: declare variable, if/else.

This constructs can allow for instance replacing

	Either<TextEdit, InsertReplaceEdit> textEdit = item.getTextEdit();
	if (textEdit.isLeft())
		return offset ==
LSPEclipseUtils.toOffset(textEdit.getLeft().getRange().getStart(),
document);
	else {
		Position replace = textEdit.getRight().getReplace().getStart();
		Position insert = textEdit.getRight().getInsert().getStart();
		Position start = replace;
		if (replace.getLine() > insert.getLine() || replace.getCharacter() >
insert.getCharacter()) start = insert;
			return offset == LSPEclipseUtils.toOffset(start, document);
		}

by

	return offset == LSPEclipseUtils.toOffset(
		textEdit.map(
			edit -> edit.getRange().getStart(),
			insertReplaceEdit -> {
				Position replace = insertReplaceEdit.getReplace().getStart();
				Position insert = insertReplaceEdit.getInsert().getStart();
				Position start = replace;
				if (replace.getLine() > insert.getLine() || replace.getCharacter() >
insert.getCharacter()) start = insert;
			}), document);

which better captures the logic.
  • Loading branch information
mickaelistria committed Apr 14, 2021
1 parent 1470d74 commit 4a92b08
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collection;
import java.util.function.Function;

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

Expand All @@ -24,11 +25,11 @@
public class Either<L, R> {

public static <L, R> Either<L, R> forLeft(@NonNull L left) {
return new Either<L, R>(left, null);
return new Either<>(left, null);
}

public static <L, R> Either<L, R> forRight(@NonNull R right) {
return new Either<L, R>(null, right);
return new Either<>(null, right);
}

private final L left;
Expand Down Expand Up @@ -63,7 +64,17 @@ public boolean isLeft() {
public boolean isRight() {
return right != null;
}


public <T> T map(@NonNull Function<? super L, ? extends T> mapLeft, @NonNull Function <? super R, ? extends T> mapRight) {
if (isLeft()) {
return mapLeft.apply(getLeft());
}
if (isRight()) {
return mapRight.apply(getRight());
}
return null;
}

@Override
public boolean equals(Object obj) {
if (obj instanceof Either<?, ?>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
******************************************************************************/
package org.eclipse.lsp4j.jsonrpc.test.json;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.Collection;
import java.util.List;
import java.util.Objects;

import org.eclipse.lsp4j.jsonrpc.json.adapters.EitherTypeAdapter;
Expand All @@ -23,9 +29,6 @@
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;


public class EitherTest {

Expand Down Expand Up @@ -107,6 +110,19 @@ public void testEqualsFalseWithNonNull() {
assertFalse(either1.equals(either2));
}

@Test
public void testMap() {
Collection<Either<Collection<?>, String>> toTest = List.of(
Either.forLeft(List.of("f", "o", "o")),
Either.forRight("bar"));
for (Either<Collection<?>, String> either : toTest) {
Integer n = either.map(
collection -> Integer.valueOf(collection.size()),
string -> Integer.valueOf(string.length()));
assertEquals(Integer.valueOf(3), n);
}
}

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

0 comments on commit 4a92b08

Please sign in to comment.