Skip to content

Commit

Permalink
refactor: fixes various code analyser issues (#1118)
Browse files Browse the repository at this point in the history
* chore: removes final modifier from methods

Removes final modifier from private and static methods.

* chore: adds diamond operator to generic class call

* chore: simplifies boolean expression

* chore: removes redundant throws clauses

* chore: marks ignored exceptions with comments

* chore: anonymous types replaced with lambda calls

* chore: uses comparator combinator

* chore: lambda replaced with method reference

* chore: makes inner class static

* chore: replaces Arrays.asList with emptyList

* chore: removes redundant initializers

* chore: removes redundant toString calls

* chore: explicit type arguments can be inferred

* chore: removes redundant type casts

* chore: replaces StringBuilder with String

* chore: removes redundant suppressions

* chore: list method replaced with value

* chore: uses comparator combinator

* chore: uses singletonList instead of asList

* chore: removes redundant type casts / parameters

* chore: addresses pr comments

* chore: removes unnecessary type cast
  • Loading branch information
thiagotnunes committed May 3, 2021
1 parent 641f04c commit 47f5965
Show file tree
Hide file tree
Showing 134 changed files with 2,398 additions and 3,896 deletions.
Expand Up @@ -32,7 +32,6 @@
import com.google.cloud.spanner.spi.v1.SpannerRpc;
import com.google.cloud.spanner.v1.stub.SpannerStubSettings;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Function;
import com.google.common.collect.AbstractIterator;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
Expand Down Expand Up @@ -225,7 +224,7 @@ protected com.google.protobuf.Value computeNext() {
+ newValue.getKindCase());
}
if (kind == KindCase.STRING_VALUE) {
merged = (String) merged + newValue.getStringValue();
merged = merged + newValue.getStringValue();
} else {
concatLists(
(List<com.google.protobuf.Value>) merged, newValue.getListValue().getValuesList());
Expand Down Expand Up @@ -319,7 +318,7 @@ private void concatLists(List<com.google.protobuf.Value> a, List<com.google.prot
KindCase lastKind = last.getKindCase();
KindCase firstKind = first.getKindCase();
if (isMergeable(lastKind) && lastKind == firstKind) {
com.google.protobuf.Value merged = null;
com.google.protobuf.Value merged;
if (lastKind == KindCase.STRING_VALUE) {
String lastStr = last.getStringValue();
String firstStr = first.getStringValue();
Expand Down Expand Up @@ -524,12 +523,7 @@ static Object decodeArrayValue(Type elementType, ListValue listValue) {
// Use a view: element conversion is virtually free.
return Lists.transform(
listValue.getValuesList(),
new Function<com.google.protobuf.Value, Boolean>() {
@Override
public Boolean apply(com.google.protobuf.Value input) {
return input.getKindCase() == KindCase.NULL_VALUE ? null : input.getBoolValue();
}
});
input -> input.getKindCase() == KindCase.NULL_VALUE ? null : input.getBoolValue());
case INT64:
// For int64/float64 types, use custom containers. These avoid wrapper object
// creation for non-null arrays.
Expand All @@ -551,12 +545,7 @@ public Boolean apply(com.google.protobuf.Value input) {
case STRING:
return Lists.transform(
listValue.getValuesList(),
new Function<com.google.protobuf.Value, String>() {
@Override
public String apply(com.google.protobuf.Value input) {
return input.getKindCase() == KindCase.NULL_VALUE ? null : input.getStringValue();
}
});
input -> input.getKindCase() == KindCase.NULL_VALUE ? null : input.getStringValue());
case BYTES:
{
// Materialize list: element conversion is expensive and should happen only once.
Expand Down Expand Up @@ -1012,12 +1001,9 @@ private static void backoffSleep(Context context, long backoffMillis) throws Spa
ImmutableMap.of("Delay", AttributeValue.longAttributeValue(backoffMillis)));
final CountDownLatch latch = new CountDownLatch(1);
final Context.CancellationListener listener =
new Context.CancellationListener() {
@Override
public void cancelled(Context context) {
// Wakeup on cancellation / DEADLINE_EXCEEDED.
latch.countDown();
}
ignored -> {
// Wakeup on cancellation / DEADLINE_EXCEEDED.
latch.countDown();
};

context.addListener(listener, DirectExecutor.INSTANCE);
Expand Down
Expand Up @@ -16,7 +16,6 @@

package com.google.cloud.spanner;

import com.google.api.core.ApiAsyncFunction;
import com.google.api.core.ApiFuture;
import com.google.api.core.ApiFutures;
import com.google.api.core.ListenableFutureToApiFuture;
Expand Down Expand Up @@ -529,18 +528,10 @@ public <T> ApiFuture<List<T>> toListAsync(
Preconditions.checkState(!closed, "This AsyncResultSet has been closed");
Preconditions.checkState(
this.state == State.INITIALIZED, "This AsyncResultSet has already been used.");
final SettableApiFuture<List<T>> res = SettableApiFuture.<List<T>>create();
final SettableApiFuture<List<T>> res = SettableApiFuture.create();
CreateListCallback<T> callback = new CreateListCallback<>(res, transformer);
ApiFuture<Void> finished = setCallback(executor, callback);
return ApiFutures.transformAsync(
finished,
new ApiAsyncFunction<Void, List<T>>() {
@Override
public ApiFuture<List<T>> apply(Void input) throws Exception {
return res;
}
},
MoreExecutors.directExecutor());
return ApiFutures.transformAsync(finished, ignored -> res, MoreExecutors.directExecutor());
}
}

Expand Down
Expand Up @@ -18,7 +18,6 @@

import static com.google.common.base.Preconditions.checkState;

import com.google.api.core.ApiFunction;
import com.google.api.core.ApiFuture;
import com.google.api.core.ApiFutures;
import com.google.api.core.SettableApiFuture;
Expand Down Expand Up @@ -79,14 +78,7 @@ private void setCommitResponse() {
public ApiFuture<Timestamp> getCommitTimestamp() {
checkState(commitResponse != null, "runAsync() has not yet been called");
return ApiFutures.transform(
commitResponse,
new ApiFunction<CommitResponse, Timestamp>() {
@Override
public Timestamp apply(CommitResponse input) {
return input.getCommitTimestamp();
}
},
MoreExecutors.directExecutor());
commitResponse, CommitResponse::getCommitTimestamp, MoreExecutors.directExecutor());
}

public ApiFuture<CommitResponse> getCommitResponse() {
Expand Down
Expand Up @@ -16,8 +16,6 @@

package com.google.cloud.spanner;

import com.google.api.core.ApiAsyncFunction;
import com.google.api.core.ApiFunction;
import com.google.api.core.ApiFuture;
import com.google.api.core.ApiFutureCallback;
import com.google.api.core.ApiFutures;
Expand All @@ -30,7 +28,6 @@
import com.google.common.base.MoreObjects;
import com.google.common.base.Preconditions;
import com.google.common.util.concurrent.MoreExecutors;
import com.google.protobuf.Empty;
import io.opencensus.trace.Span;
import io.opencensus.trace.Tracer;
import io.opencensus.trace.Tracing;
Expand Down Expand Up @@ -73,7 +70,7 @@ public ApiFuture<Void> closeAsync() {
if (txn != null) {
txn.close();
}
return MoreObjects.firstNonNull(res, ApiFutures.<Void>immediateFuture(null));
return MoreObjects.firstNonNull(res, ApiFutures.immediateFuture(null));
}

@Override
Expand Down Expand Up @@ -154,14 +151,7 @@ public void onSuccess(CommitResponse result) {
},
MoreExecutors.directExecutor());
return ApiFutures.transform(
commitResponseFuture,
new ApiFunction<CommitResponse, Timestamp>() {
@Override
public Timestamp apply(CommitResponse input) {
return input.getCommitTimestamp();
}
},
MoreExecutors.directExecutor());
commitResponseFuture, CommitResponse::getCommitTimestamp, MoreExecutors.directExecutor());
}

@Override
Expand All @@ -172,12 +162,7 @@ public ApiFuture<Void> rollbackAsync() {
try {
return ApiFutures.transformAsync(
txn.rollbackAsync(),
new ApiAsyncFunction<Empty, Void>() {
@Override
public ApiFuture<Void> apply(Empty input) throws Exception {
return ApiFutures.immediateFuture(null);
}
},
ignored -> ApiFutures.immediateFuture(null),
MoreExecutors.directExecutor());
} finally {
txnState = TransactionState.ROLLED_BACK;
Expand Down
Expand Up @@ -16,11 +16,9 @@

package com.google.cloud.spanner;

import com.google.api.core.ApiFunction;
import com.google.api.gax.grpc.ProtoOperationTransformers;
import com.google.api.gax.longrunning.OperationFuture;
import com.google.api.gax.longrunning.OperationFutureImpl;
import com.google.api.gax.longrunning.OperationSnapshot;
import com.google.api.gax.paging.Page;
import com.google.cloud.Policy;
import com.google.cloud.Policy.DefaultMarshaller;
Expand Down Expand Up @@ -108,22 +106,15 @@ public OperationFuture<Database, RestoreDatabaseMetadata> restoreDatabase(Restor
return new OperationFutureImpl<>(
rawOperationFuture.getPollingFuture(),
rawOperationFuture.getInitialFuture(),
new ApiFunction<OperationSnapshot, Database>() {
@Override
public Database apply(OperationSnapshot snapshot) {
return Database.fromProto(
snapshot ->
Database.fromProto(
ProtoOperationTransformers.ResponseTransformer.create(
com.google.spanner.admin.database.v1.Database.class)
.apply(snapshot),
DatabaseAdminClientImpl.this);
}
},
DatabaseAdminClientImpl.this),
ProtoOperationTransformers.MetadataTransformer.create(RestoreDatabaseMetadata.class),
new ApiFunction<Exception, Database>() {
@Override
public Database apply(Exception e) {
throw SpannerExceptionFactory.newSpannerException(e);
}
e -> {
throw SpannerExceptionFactory.newSpannerException(e);
});
}

Expand Down Expand Up @@ -154,30 +145,24 @@ public OperationFuture<Backup, CreateBackupMetadata> createBackup(Backup backupI
return new OperationFutureImpl<>(
rawOperationFuture.getPollingFuture(),
rawOperationFuture.getInitialFuture(),
new ApiFunction<OperationSnapshot, Backup>() {
@Override
public Backup apply(OperationSnapshot snapshot) {
com.google.spanner.admin.database.v1.Backup proto =
ProtoOperationTransformers.ResponseTransformer.create(
com.google.spanner.admin.database.v1.Backup.class)
.apply(snapshot);
return Backup.fromProto(
com.google.spanner.admin.database.v1.Backup.newBuilder(proto)
.setName(proto.getName())
.setExpireTime(proto.getExpireTime())
.setVersionTime(proto.getVersionTime())
.setState(proto.getState())
.setEncryptionInfo(proto.getEncryptionInfo())
.build(),
DatabaseAdminClientImpl.this);
}
snapshot -> {
com.google.spanner.admin.database.v1.Backup proto =
ProtoOperationTransformers.ResponseTransformer.create(
com.google.spanner.admin.database.v1.Backup.class)
.apply(snapshot);
return Backup.fromProto(
com.google.spanner.admin.database.v1.Backup.newBuilder(proto)
.setName(proto.getName())
.setExpireTime(proto.getExpireTime())
.setVersionTime(proto.getVersionTime())
.setState(proto.getState())
.setEncryptionInfo(proto.getEncryptionInfo())
.build(),
DatabaseAdminClientImpl.this);
},
ProtoOperationTransformers.MetadataTransformer.create(CreateBackupMetadata.class),
new ApiFunction<Exception, Backup>() {
@Override
public Backup apply(Exception e) {
throw SpannerExceptionFactory.newSpannerException(e);
}
e -> {
throw SpannerExceptionFactory.newSpannerException(e);
});
}

Expand Down Expand Up @@ -311,22 +296,15 @@ public OperationFuture<Database, CreateDatabaseMetadata> createDatabase(
return new OperationFutureImpl<>(
rawOperationFuture.getPollingFuture(),
rawOperationFuture.getInitialFuture(),
new ApiFunction<OperationSnapshot, Database>() {
@Override
public Database apply(OperationSnapshot snapshot) {
return Database.fromProto(
snapshot ->
Database.fromProto(
ProtoOperationTransformers.ResponseTransformer.create(
com.google.spanner.admin.database.v1.Database.class)
.apply(snapshot),
DatabaseAdminClientImpl.this);
}
},
DatabaseAdminClientImpl.this),
ProtoOperationTransformers.MetadataTransformer.create(CreateDatabaseMetadata.class),
new ApiFunction<Exception, Database>() {
@Override
public Database apply(Exception e) {
throw SpannerExceptionFactory.newSpannerException(e);
}
e -> {
throw SpannerExceptionFactory.newSpannerException(e);
});
}

Expand All @@ -350,19 +328,13 @@ public OperationFuture<Void, UpdateDatabaseDdlMetadata> updateDatabaseDdl(
return new OperationFutureImpl<>(
rawOperationFuture.getPollingFuture(),
rawOperationFuture.getInitialFuture(),
new ApiFunction<OperationSnapshot, Void>() {
@Override
public Void apply(OperationSnapshot snapshot) {
ProtoOperationTransformers.ResponseTransformer.create(Empty.class).apply(snapshot);
return null;
}
snapshot -> {
ProtoOperationTransformers.ResponseTransformer.create(Empty.class).apply(snapshot);
return null;
},
ProtoOperationTransformers.MetadataTransformer.create(UpdateDatabaseDdlMetadata.class),
new ApiFunction<Exception, Void>() {
@Override
public Void apply(Exception e) {
throw SpannerExceptionFactory.newSpannerException(e);
}
e -> {
throw SpannerExceptionFactory.newSpannerException(e);
});
}

Expand Down
Expand Up @@ -64,13 +64,7 @@ public CommitResponse writeWithOptions(
throws SpannerException {
Span span = tracer.spanBuilder(READ_WRITE_TRANSACTION).startSpan();
try (Scope s = tracer.withSpan(span)) {
return runWithSessionRetry(
new Function<Session, CommitResponse>() {
@Override
public CommitResponse apply(Session session) {
return session.writeWithOptions(mutations, options);
}
});
return runWithSessionRetry(session -> session.writeWithOptions(mutations, options));
} catch (RuntimeException e) {
TraceUtil.setWithFailure(span, e);
throw e;
Expand All @@ -91,12 +85,7 @@ public CommitResponse writeAtLeastOnceWithOptions(
Span span = tracer.spanBuilder(READ_WRITE_TRANSACTION).startSpan();
try (Scope s = tracer.withSpan(span)) {
return runWithSessionRetry(
new Function<Session, CommitResponse>() {
@Override
public CommitResponse apply(Session session) {
return session.writeAtLeastOnceWithOptions(mutations, options);
}
});
session -> session.writeAtLeastOnceWithOptions(mutations, options));
} catch (RuntimeException e) {
TraceUtil.setWithFailure(span, e);
throw e;
Expand Down Expand Up @@ -221,13 +210,7 @@ public AsyncTransactionManager transactionManagerAsync(TransactionOption... opti
public long executePartitionedUpdate(final Statement stmt, final UpdateOption... options) {
Span span = tracer.spanBuilder(PARTITION_DML_TRANSACTION).startSpan();
try (Scope s = tracer.withSpan(span)) {
return runWithSessionRetry(
new Function<Session, Long>() {
@Override
public Long apply(Session session) {
return session.executePartitionedUpdate(stmt, options);
}
});
return runWithSessionRetry(session -> session.executePartitionedUpdate(stmt, options));
} catch (RuntimeException e) {
TraceUtil.endSpanWithFailure(span, e);
throw e;
Expand Down

0 comments on commit 47f5965

Please sign in to comment.