Skip to content

Commit

Permalink
[java] Changing order of Either parts, right part is for right value
Browse files Browse the repository at this point in the history
  • Loading branch information
barancev committed Feb 16, 2021
1 parent 99c31dd commit 0065abd
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
14 changes: 7 additions & 7 deletions java/client/src/org/openqa/selenium/devtools/Connection.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public class Connection implements Closeable {
});
private static final AtomicLong NEXT_ID = new AtomicLong(1L);
private final WebSocket socket;
private final Map<Long, Consumer<Either<JsonInput, Throwable>>> methodCallbacks = new LinkedHashMap<>();
private final Map<Long, Consumer<Either<Throwable, JsonInput>>> methodCallbacks = new LinkedHashMap<>();
private final Multimap<Event<?>, Consumer<?>> eventCallbacks = HashMultimap.create();

public Connection(HttpClient client, String url) {
Expand Down Expand Up @@ -104,16 +104,16 @@ public <X> CompletableFuture<X> send(SessionID sessionId, Command<X> command) {
CompletableFuture<X> result = new CompletableFuture<>();
if (command.getSendsResponse()) {
methodCallbacks.put(id, NamedConsumer.of(command.getMethod(), inputOrException -> {
if (inputOrException.isLeft()) {
if (inputOrException.isRight()) {
try {
X value = command.getMapper().apply(inputOrException.left());
X value = command.getMapper().apply(inputOrException.right());
result.complete(value);
} catch (Throwable e) {
LOG.log(Level.WARNING, String.format("Unable to map result for %s", command.getMethod()), e);
result.completeExceptionally(e);
}
} else {
result.completeExceptionally(inputOrException.right());
result.completeExceptionally(inputOrException.left());
}
}));
}
Expand Down Expand Up @@ -204,7 +204,7 @@ private void handle(CharSequence data) {
Map<String, Object> raw = JSON.toType(asString, MAP_TYPE);
if (raw.get("id") instanceof Number
&& (raw.get("result") != null || raw.get("error") != null)) {
Consumer<Either<JsonInput, Throwable>> consumer = methodCallbacks.remove(((Number) raw.get("id")).longValue());
Consumer<Either<Throwable, JsonInput>> consumer = methodCallbacks.remove(((Number) raw.get("id")).longValue());
if (consumer == null) {
return;
}
Expand All @@ -215,11 +215,11 @@ private void handle(CharSequence data) {
while (input.hasNext()) {
switch (input.nextName()) {
case "result":
consumer.accept(Either.left(input));
consumer.accept(Either.right(input));
break;

case "error":
consumer.accept(Either.right(new WebDriverException(asString)));
consumer.accept(Either.left(new WebDriverException(asString)));
input.skipValue();
break;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ public Result createSession(HttpHandler client, Command command) throws IOExcept

try (InputStream rawIn = os.asByteSource().openBufferedStream();
BufferedInputStream contentStream = new BufferedInputStream(rawIn)) {
Either<Result, String> result = createSession(client, contentStream, counter.getCount());
Either<String, Result> result = createSession(client, contentStream, counter.getCount());

if (result.isLeft()) {
Result toReturn = result.left();
if (result.isRight()) {
Result toReturn = result.right();
LOG.info(String.format("Detected dialect: %s", toReturn.dialect));
return toReturn;
} else {
Expand All @@ -89,7 +89,7 @@ public Result createSession(HttpHandler client, Command command) throws IOExcept
}
}

Either<Result, String> createSession(HttpHandler client, InputStream newSessionBlob, long size) {
Either<String, Result> createSession(HttpHandler client, InputStream newSessionBlob, long size) {
// Create the http request and send it
HttpRequest request = new HttpRequest(HttpMethod.POST, "/session");

Expand Down Expand Up @@ -119,7 +119,7 @@ Either<Result, String> createSession(HttpHandler client, InputStream newSessionB
blob);

if (initialResponse.getStatusCode() != 200) {
return Either.right(blob.get("message").toString());
return Either.left(blob.get("message").toString());
}

return Stream.of(
Expand All @@ -128,8 +128,8 @@ Either<Result, String> createSession(HttpHandler client, InputStream newSessionB
.map(func -> func.apply(initialResponse))
.filter(Objects::nonNull)
.findFirst()
.<Either<Result, String>>map(Either::left)
.orElseGet(() -> Either.right("Handshake response does not match any supported protocol"));
.<Either<String, Result>>map(Either::right)
.orElseGet(() -> Either.left("Handshake response does not match any supported protocol"));
}

public static class Result {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,17 +359,17 @@ private WebDriver getRemoteDriver() {
.andThen(new ErrorFilter()));

byte[] payload = getPayloadUtf8Bytes();
Either<ProtocolHandshake.Result, String> result = new ProtocolHandshake().createSession(
Either<String, ProtocolHandshake.Result> result = new ProtocolHandshake().createSession(
handler,
new ByteArrayInputStream(payload),
payload.length);

if (result.isLeft()) {
CommandExecutor executor = result.mapLeft(res -> createExecutor(handler, res));
if (result.isRight()) {
CommandExecutor executor = result.map(res -> createExecutor(handler, res));
return new RemoteWebDriver(executor, new ImmutableCapabilities());
} else {
throw new SessionNotCreatedException(
String.format("Unable to create new remote session. Reason: %s", result.right()));
String.format("Unable to create new remote session. Reason: %s", result.left()));
}
}

Expand Down

0 comments on commit 0065abd

Please sign in to comment.