Skip to content

Commit

Permalink
refact: use var keyword for direct casts and assignment of new objects
Browse files Browse the repository at this point in the history
  • Loading branch information
sebthom committed Mar 1, 2024
1 parent 65e57de commit a382d99
Show file tree
Hide file tree
Showing 52 changed files with 587 additions and 616 deletions.
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
/******************************************************************************
* Copyright (c) 2022 itemis AG and others.
*
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0,
* or the Eclipse Distribution License v. 1.0 which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
*
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
******************************************************************************/
package org.eclipse.lsp4j.debug.adapters;

import java.util.function.Predicate;

import org.eclipse.lsp4j.debug.AttachRequestArguments;
import org.eclipse.lsp4j.debug.LaunchRequestArguments;
import org.eclipse.lsp4j.jsonrpc.json.adapters.EitherTypeAdapter;
import org.eclipse.lsp4j.jsonrpc.json.adapters.EitherTypeAdapter.PropertyChecker;
import org.eclipse.lsp4j.jsonrpc.messages.Either;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.TypeAdapter;
import com.google.gson.TypeAdapterFactory;
import com.google.gson.reflect.TypeToken;
Expand All @@ -33,10 +30,9 @@ public class RestartArgumentsArgumentsTypeAdapter implements TypeAdapterFactory
@SuppressWarnings("unchecked")
@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
Predicate<JsonElement> leftChecker = new PropertyChecker("noDebug");
Predicate<JsonElement> rightChecker = leftChecker.negate();
TypeAdapter<Either<LaunchRequestArguments, AttachRequestArguments>> elementTypeAdapter = new EitherTypeAdapter<>(
gson, ELEMENT_TYPE, leftChecker, rightChecker);
final var leftChecker = new PropertyChecker("noDebug");
final var rightChecker = leftChecker.negate();
final var elementTypeAdapter = new EitherTypeAdapter<>(gson, ELEMENT_TYPE, leftChecker, rightChecker);
return (TypeAdapter<T>) elementTypeAdapter;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/******************************************************************************
* Copyright (c) 2017 Kichwa Coders Ltd. and others.
*
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0,
* or the Eclipse Distribution License v. 1.0 which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
*
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
******************************************************************************/
package org.eclipse.lsp4j.jsonrpc.debug.adapters;
Expand Down Expand Up @@ -329,7 +329,7 @@ protected Object parseBody(Object rawBody, String messageType, int request_seq,
if (!(rawBody instanceof JsonElement)) {
return rawBody;
}
JsonElement rawJsonParams = (JsonElement) rawBody;
final var rawJsonParams = (JsonElement) rawBody;
return fromJson(rawJsonParams, Object.class);
}
return rawBody;
Expand Down Expand Up @@ -388,7 +388,7 @@ private Message createMessage(String messageType, int seq, int request_seq, Stri
Type returnType = jsonRpcMethod.getReturnType();
if (jsonRpcMethod.getReturnTypeAdapterFactory() != null)
typeAdapter = jsonRpcMethod.getReturnTypeAdapterFactory().create(gson, TypeToken.get(returnType));
JsonElement jsonElement = (JsonElement) body;
final var jsonElement = (JsonElement) body;
if (typeAdapter != null)
body = typeAdapter.fromJsonTree(jsonElement);
else
Expand All @@ -410,7 +410,7 @@ private Message createMessage(String messageType, int seq, int request_seq, Stri
public void write(JsonWriter out, Message message) throws IOException {
out.beginObject();
if (message instanceof DebugRequestMessage) {
DebugRequestMessage requestMessage = (DebugRequestMessage) message;
final var requestMessage = (DebugRequestMessage) message;
out.name("type");
out.value("request");
out.name("seq");
Expand All @@ -419,11 +419,11 @@ public void write(JsonWriter out, Message message) throws IOException {
out.value(requestMessage.getMethod());
Object params = requestMessage.getParams();
if (params != null) {
out.name("arguments");
out.name("arguments");
gson.toJson(params, params.getClass(), out);
}
} else if (message instanceof DebugResponseMessage) {
DebugResponseMessage responseMessage = (DebugResponseMessage) message;
final var responseMessage = (DebugResponseMessage) message;
out.name("type");
out.value("response");
out.name("seq");
Expand Down Expand Up @@ -458,7 +458,7 @@ public void write(JsonWriter out, Message message) throws IOException {
}
}
} else if (message instanceof DebugNotificationMessage) {
DebugNotificationMessage notificationMessage = (DebugNotificationMessage) message;
final var notificationMessage = (DebugNotificationMessage) message;
out.name("type");
out.value("event");
out.name("seq");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/******************************************************************************
* Copyright (c) 2017 Kichwa Coders Ltd. and others.
*
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0,
* or the Eclipse Distribution License v. 1.0 which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
*
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
******************************************************************************/
package org.eclipse.lsp4j.jsonrpc.debug.messages;
Expand Down Expand Up @@ -38,7 +38,7 @@ public String getId() {
return id.getRight().toString();
return null;
}

@NonNull
public Either<String, Number> getRawId() {
return id;
Expand All @@ -47,11 +47,11 @@ public Either<String, Number> getRawId() {
public void setId(String id) {
this.id = Either.forLeft(id);
}

public void setId(int id) {
this.id = Either.forRight(id);
}

public void setRawId(@NonNull Either<String, Number> id) {
this.id = id;
}
Expand All @@ -66,7 +66,7 @@ public boolean equals(final Object obj) {
return false;
if (!super.equals(obj))
return false;
DebugNotificationMessage other = (DebugNotificationMessage) obj;
final var other = (DebugNotificationMessage) obj;
if (this.id == null) {
if (other.id != null)
return false;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/******************************************************************************
* Copyright (c) 2017 Kichwa Coders Ltd. and others.
*
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0,
* or the Eclipse Distribution License v. 1.0 which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
*
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
******************************************************************************/
package org.eclipse.lsp4j.jsonrpc.debug.messages;
Expand Down Expand Up @@ -40,7 +40,7 @@ public String getResponseId() {
return responseId.getRight().toString();
return null;
}

@NonNull
public Either<String, Number> getRawResponseId() {
return responseId;
Expand All @@ -49,11 +49,11 @@ public Either<String, Number> getRawResponseId() {
public void setResponseId(String id) {
this.responseId = Either.forLeft(id);
}

public void setResponseId(int id) {
this.responseId = Either.forRight(id);
}

public void setRawResponseId(@NonNull Either<String, Number> id) {
this.responseId = id;
}
Expand Down Expand Up @@ -83,7 +83,7 @@ public boolean equals(final Object obj) {
return false;
if (!super.equals(obj))
return false;
DebugResponseMessage other = (DebugResponseMessage) obj;
final var other = (DebugResponseMessage) obj;
if (this.responseId == null) {
if (other.responseId != null)
return false;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/******************************************************************************
* Copyright (c) 2016-2017 TypeFox and others.
*
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0,
* or the Eclipse Distribution License v. 1.0 which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
*
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
******************************************************************************/
package org.eclipse.lsp4j.jsonrpc.debug.test;
Expand Down Expand Up @@ -51,7 +51,7 @@ public void notify(String method, Object parameter) {

@Override
public CompletableFuture<Object> request(String method, Object parameter) {
CompletableFuture<Object> completableFuture = new CompletableFuture<Object>();
final var completableFuture = new CompletableFuture<>();
requests.put(new RequestMessage() {{
setMethod(method);
setParams(parameter);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/******************************************************************************
* Copyright (c) 2016 TypeFox and others.
*
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0,
* or the Eclipse Distribution License v. 1.0 which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
*
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
******************************************************************************/
package org.eclipse.lsp4j.jsonrpc;
Expand All @@ -18,52 +18,52 @@

public final class CompletableFutures {
private CompletableFutures() {}

/**
* A utility method to create a {@link CompletableFuture} with cancellation support.
*
*
* @param code a function that accepts a {@link CancelChecker} and returns the to be computed value
* @return a future
*/
public static <R> CompletableFuture<R> computeAsync(Function<CancelChecker, R> code) {
CompletableFuture<CancelChecker> start = new CompletableFuture<>();
final var start = new CompletableFuture<CancelChecker>();
CompletableFuture<R> result = start.thenApplyAsync(code);
start.complete(new FutureCancelChecker(result));
return result;
}

/**
* A utility method to create a {@link CompletableFuture} with cancellation support.
*
*
* @param code a function that accepts a {@link CancelChecker} and returns the to be computed value
* @return a future
*/
public static <R> CompletableFuture<R> computeAsync(Executor executor, Function<CancelChecker, R> code) {
CompletableFuture<CancelChecker> start = new CompletableFuture<>();
final var start = new CompletableFuture<CancelChecker>();
CompletableFuture<R> result = start.thenApplyAsync(code, executor);
start.complete(new FutureCancelChecker(result));
return result;
}

public static class FutureCancelChecker implements CancelChecker {

private final CompletableFuture<?> future;

public FutureCancelChecker(CompletableFuture<?> future) {
this.future = future;
}

@Override
public void checkCanceled() {
if (future.isCancelled())
if (future.isCancelled())
throw new CancellationException();
}

@Override
public boolean isCanceled() {
return future.isCancelled();
}

}

}
Loading

0 comments on commit a382d99

Please sign in to comment.