Skip to content

Commit 47f5965

Browse files
authored
refactor: fixes various code analyser issues (#1118)
* 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
1 parent 641f04c commit 47f5965

File tree

134 files changed

+2398
-3896
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

134 files changed

+2398
-3896
lines changed

google-cloud-spanner/src/main/java/com/google/cloud/spanner/AbstractResultSet.java

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import com.google.cloud.spanner.spi.v1.SpannerRpc;
3333
import com.google.cloud.spanner.v1.stub.SpannerStubSettings;
3434
import com.google.common.annotations.VisibleForTesting;
35-
import com.google.common.base.Function;
3635
import com.google.common.collect.AbstractIterator;
3736
import com.google.common.collect.ImmutableMap;
3837
import com.google.common.collect.Lists;
@@ -225,7 +224,7 @@ protected com.google.protobuf.Value computeNext() {
225224
+ newValue.getKindCase());
226225
}
227226
if (kind == KindCase.STRING_VALUE) {
228-
merged = (String) merged + newValue.getStringValue();
227+
merged = merged + newValue.getStringValue();
229228
} else {
230229
concatLists(
231230
(List<com.google.protobuf.Value>) merged, newValue.getListValue().getValuesList());
@@ -319,7 +318,7 @@ private void concatLists(List<com.google.protobuf.Value> a, List<com.google.prot
319318
KindCase lastKind = last.getKindCase();
320319
KindCase firstKind = first.getKindCase();
321320
if (isMergeable(lastKind) && lastKind == firstKind) {
322-
com.google.protobuf.Value merged = null;
321+
com.google.protobuf.Value merged;
323322
if (lastKind == KindCase.STRING_VALUE) {
324323
String lastStr = last.getStringValue();
325324
String firstStr = first.getStringValue();
@@ -524,12 +523,7 @@ static Object decodeArrayValue(Type elementType, ListValue listValue) {
524523
// Use a view: element conversion is virtually free.
525524
return Lists.transform(
526525
listValue.getValuesList(),
527-
new Function<com.google.protobuf.Value, Boolean>() {
528-
@Override
529-
public Boolean apply(com.google.protobuf.Value input) {
530-
return input.getKindCase() == KindCase.NULL_VALUE ? null : input.getBoolValue();
531-
}
532-
});
526+
input -> input.getKindCase() == KindCase.NULL_VALUE ? null : input.getBoolValue());
533527
case INT64:
534528
// For int64/float64 types, use custom containers. These avoid wrapper object
535529
// creation for non-null arrays.
@@ -551,12 +545,7 @@ public Boolean apply(com.google.protobuf.Value input) {
551545
case STRING:
552546
return Lists.transform(
553547
listValue.getValuesList(),
554-
new Function<com.google.protobuf.Value, String>() {
555-
@Override
556-
public String apply(com.google.protobuf.Value input) {
557-
return input.getKindCase() == KindCase.NULL_VALUE ? null : input.getStringValue();
558-
}
559-
});
548+
input -> input.getKindCase() == KindCase.NULL_VALUE ? null : input.getStringValue());
560549
case BYTES:
561550
{
562551
// Materialize list: element conversion is expensive and should happen only once.
@@ -1012,12 +1001,9 @@ private static void backoffSleep(Context context, long backoffMillis) throws Spa
10121001
ImmutableMap.of("Delay", AttributeValue.longAttributeValue(backoffMillis)));
10131002
final CountDownLatch latch = new CountDownLatch(1);
10141003
final Context.CancellationListener listener =
1015-
new Context.CancellationListener() {
1016-
@Override
1017-
public void cancelled(Context context) {
1018-
// Wakeup on cancellation / DEADLINE_EXCEEDED.
1019-
latch.countDown();
1020-
}
1004+
ignored -> {
1005+
// Wakeup on cancellation / DEADLINE_EXCEEDED.
1006+
latch.countDown();
10211007
};
10221008

10231009
context.addListener(listener, DirectExecutor.INSTANCE);

google-cloud-spanner/src/main/java/com/google/cloud/spanner/AsyncResultSetImpl.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package com.google.cloud.spanner;
1818

19-
import com.google.api.core.ApiAsyncFunction;
2019
import com.google.api.core.ApiFuture;
2120
import com.google.api.core.ApiFutures;
2221
import com.google.api.core.ListenableFutureToApiFuture;
@@ -529,18 +528,10 @@ public <T> ApiFuture<List<T>> toListAsync(
529528
Preconditions.checkState(!closed, "This AsyncResultSet has been closed");
530529
Preconditions.checkState(
531530
this.state == State.INITIALIZED, "This AsyncResultSet has already been used.");
532-
final SettableApiFuture<List<T>> res = SettableApiFuture.<List<T>>create();
531+
final SettableApiFuture<List<T>> res = SettableApiFuture.create();
533532
CreateListCallback<T> callback = new CreateListCallback<>(res, transformer);
534533
ApiFuture<Void> finished = setCallback(executor, callback);
535-
return ApiFutures.transformAsync(
536-
finished,
537-
new ApiAsyncFunction<Void, List<T>>() {
538-
@Override
539-
public ApiFuture<List<T>> apply(Void input) throws Exception {
540-
return res;
541-
}
542-
},
543-
MoreExecutors.directExecutor());
534+
return ApiFutures.transformAsync(finished, ignored -> res, MoreExecutors.directExecutor());
544535
}
545536
}
546537

google-cloud-spanner/src/main/java/com/google/cloud/spanner/AsyncRunnerImpl.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

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

21-
import com.google.api.core.ApiFunction;
2221
import com.google.api.core.ApiFuture;
2322
import com.google.api.core.ApiFutures;
2423
import com.google.api.core.SettableApiFuture;
@@ -79,14 +78,7 @@ private void setCommitResponse() {
7978
public ApiFuture<Timestamp> getCommitTimestamp() {
8079
checkState(commitResponse != null, "runAsync() has not yet been called");
8180
return ApiFutures.transform(
82-
commitResponse,
83-
new ApiFunction<CommitResponse, Timestamp>() {
84-
@Override
85-
public Timestamp apply(CommitResponse input) {
86-
return input.getCommitTimestamp();
87-
}
88-
},
89-
MoreExecutors.directExecutor());
81+
commitResponse, CommitResponse::getCommitTimestamp, MoreExecutors.directExecutor());
9082
}
9183

9284
public ApiFuture<CommitResponse> getCommitResponse() {

google-cloud-spanner/src/main/java/com/google/cloud/spanner/AsyncTransactionManagerImpl.java

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
package com.google.cloud.spanner;
1818

19-
import com.google.api.core.ApiAsyncFunction;
20-
import com.google.api.core.ApiFunction;
2119
import com.google.api.core.ApiFuture;
2220
import com.google.api.core.ApiFutureCallback;
2321
import com.google.api.core.ApiFutures;
@@ -30,7 +28,6 @@
3028
import com.google.common.base.MoreObjects;
3129
import com.google.common.base.Preconditions;
3230
import com.google.common.util.concurrent.MoreExecutors;
33-
import com.google.protobuf.Empty;
3431
import io.opencensus.trace.Span;
3532
import io.opencensus.trace.Tracer;
3633
import io.opencensus.trace.Tracing;
@@ -73,7 +70,7 @@ public ApiFuture<Void> closeAsync() {
7370
if (txn != null) {
7471
txn.close();
7572
}
76-
return MoreObjects.firstNonNull(res, ApiFutures.<Void>immediateFuture(null));
73+
return MoreObjects.firstNonNull(res, ApiFutures.immediateFuture(null));
7774
}
7875

7976
@Override
@@ -154,14 +151,7 @@ public void onSuccess(CommitResponse result) {
154151
},
155152
MoreExecutors.directExecutor());
156153
return ApiFutures.transform(
157-
commitResponseFuture,
158-
new ApiFunction<CommitResponse, Timestamp>() {
159-
@Override
160-
public Timestamp apply(CommitResponse input) {
161-
return input.getCommitTimestamp();
162-
}
163-
},
164-
MoreExecutors.directExecutor());
154+
commitResponseFuture, CommitResponse::getCommitTimestamp, MoreExecutors.directExecutor());
165155
}
166156

167157
@Override
@@ -172,12 +162,7 @@ public ApiFuture<Void> rollbackAsync() {
172162
try {
173163
return ApiFutures.transformAsync(
174164
txn.rollbackAsync(),
175-
new ApiAsyncFunction<Empty, Void>() {
176-
@Override
177-
public ApiFuture<Void> apply(Empty input) throws Exception {
178-
return ApiFutures.immediateFuture(null);
179-
}
180-
},
165+
ignored -> ApiFutures.immediateFuture(null),
181166
MoreExecutors.directExecutor());
182167
} finally {
183168
txnState = TransactionState.ROLLED_BACK;

google-cloud-spanner/src/main/java/com/google/cloud/spanner/DatabaseAdminClientImpl.java

Lines changed: 31 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,9 @@
1616

1717
package com.google.cloud.spanner;
1818

19-
import com.google.api.core.ApiFunction;
2019
import com.google.api.gax.grpc.ProtoOperationTransformers;
2120
import com.google.api.gax.longrunning.OperationFuture;
2221
import com.google.api.gax.longrunning.OperationFutureImpl;
23-
import com.google.api.gax.longrunning.OperationSnapshot;
2422
import com.google.api.gax.paging.Page;
2523
import com.google.cloud.Policy;
2624
import com.google.cloud.Policy.DefaultMarshaller;
@@ -108,22 +106,15 @@ public OperationFuture<Database, RestoreDatabaseMetadata> restoreDatabase(Restor
108106
return new OperationFutureImpl<>(
109107
rawOperationFuture.getPollingFuture(),
110108
rawOperationFuture.getInitialFuture(),
111-
new ApiFunction<OperationSnapshot, Database>() {
112-
@Override
113-
public Database apply(OperationSnapshot snapshot) {
114-
return Database.fromProto(
109+
snapshot ->
110+
Database.fromProto(
115111
ProtoOperationTransformers.ResponseTransformer.create(
116112
com.google.spanner.admin.database.v1.Database.class)
117113
.apply(snapshot),
118-
DatabaseAdminClientImpl.this);
119-
}
120-
},
114+
DatabaseAdminClientImpl.this),
121115
ProtoOperationTransformers.MetadataTransformer.create(RestoreDatabaseMetadata.class),
122-
new ApiFunction<Exception, Database>() {
123-
@Override
124-
public Database apply(Exception e) {
125-
throw SpannerExceptionFactory.newSpannerException(e);
126-
}
116+
e -> {
117+
throw SpannerExceptionFactory.newSpannerException(e);
127118
});
128119
}
129120

@@ -154,30 +145,24 @@ public OperationFuture<Backup, CreateBackupMetadata> createBackup(Backup backupI
154145
return new OperationFutureImpl<>(
155146
rawOperationFuture.getPollingFuture(),
156147
rawOperationFuture.getInitialFuture(),
157-
new ApiFunction<OperationSnapshot, Backup>() {
158-
@Override
159-
public Backup apply(OperationSnapshot snapshot) {
160-
com.google.spanner.admin.database.v1.Backup proto =
161-
ProtoOperationTransformers.ResponseTransformer.create(
162-
com.google.spanner.admin.database.v1.Backup.class)
163-
.apply(snapshot);
164-
return Backup.fromProto(
165-
com.google.spanner.admin.database.v1.Backup.newBuilder(proto)
166-
.setName(proto.getName())
167-
.setExpireTime(proto.getExpireTime())
168-
.setVersionTime(proto.getVersionTime())
169-
.setState(proto.getState())
170-
.setEncryptionInfo(proto.getEncryptionInfo())
171-
.build(),
172-
DatabaseAdminClientImpl.this);
173-
}
148+
snapshot -> {
149+
com.google.spanner.admin.database.v1.Backup proto =
150+
ProtoOperationTransformers.ResponseTransformer.create(
151+
com.google.spanner.admin.database.v1.Backup.class)
152+
.apply(snapshot);
153+
return Backup.fromProto(
154+
com.google.spanner.admin.database.v1.Backup.newBuilder(proto)
155+
.setName(proto.getName())
156+
.setExpireTime(proto.getExpireTime())
157+
.setVersionTime(proto.getVersionTime())
158+
.setState(proto.getState())
159+
.setEncryptionInfo(proto.getEncryptionInfo())
160+
.build(),
161+
DatabaseAdminClientImpl.this);
174162
},
175163
ProtoOperationTransformers.MetadataTransformer.create(CreateBackupMetadata.class),
176-
new ApiFunction<Exception, Backup>() {
177-
@Override
178-
public Backup apply(Exception e) {
179-
throw SpannerExceptionFactory.newSpannerException(e);
180-
}
164+
e -> {
165+
throw SpannerExceptionFactory.newSpannerException(e);
181166
});
182167
}
183168

@@ -311,22 +296,15 @@ public OperationFuture<Database, CreateDatabaseMetadata> createDatabase(
311296
return new OperationFutureImpl<>(
312297
rawOperationFuture.getPollingFuture(),
313298
rawOperationFuture.getInitialFuture(),
314-
new ApiFunction<OperationSnapshot, Database>() {
315-
@Override
316-
public Database apply(OperationSnapshot snapshot) {
317-
return Database.fromProto(
299+
snapshot ->
300+
Database.fromProto(
318301
ProtoOperationTransformers.ResponseTransformer.create(
319302
com.google.spanner.admin.database.v1.Database.class)
320303
.apply(snapshot),
321-
DatabaseAdminClientImpl.this);
322-
}
323-
},
304+
DatabaseAdminClientImpl.this),
324305
ProtoOperationTransformers.MetadataTransformer.create(CreateDatabaseMetadata.class),
325-
new ApiFunction<Exception, Database>() {
326-
@Override
327-
public Database apply(Exception e) {
328-
throw SpannerExceptionFactory.newSpannerException(e);
329-
}
306+
e -> {
307+
throw SpannerExceptionFactory.newSpannerException(e);
330308
});
331309
}
332310

@@ -350,19 +328,13 @@ public OperationFuture<Void, UpdateDatabaseDdlMetadata> updateDatabaseDdl(
350328
return new OperationFutureImpl<>(
351329
rawOperationFuture.getPollingFuture(),
352330
rawOperationFuture.getInitialFuture(),
353-
new ApiFunction<OperationSnapshot, Void>() {
354-
@Override
355-
public Void apply(OperationSnapshot snapshot) {
356-
ProtoOperationTransformers.ResponseTransformer.create(Empty.class).apply(snapshot);
357-
return null;
358-
}
331+
snapshot -> {
332+
ProtoOperationTransformers.ResponseTransformer.create(Empty.class).apply(snapshot);
333+
return null;
359334
},
360335
ProtoOperationTransformers.MetadataTransformer.create(UpdateDatabaseDdlMetadata.class),
361-
new ApiFunction<Exception, Void>() {
362-
@Override
363-
public Void apply(Exception e) {
364-
throw SpannerExceptionFactory.newSpannerException(e);
365-
}
336+
e -> {
337+
throw SpannerExceptionFactory.newSpannerException(e);
366338
});
367339
}
368340

google-cloud-spanner/src/main/java/com/google/cloud/spanner/DatabaseClientImpl.java

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,7 @@ public CommitResponse writeWithOptions(
6464
throws SpannerException {
6565
Span span = tracer.spanBuilder(READ_WRITE_TRANSACTION).startSpan();
6666
try (Scope s = tracer.withSpan(span)) {
67-
return runWithSessionRetry(
68-
new Function<Session, CommitResponse>() {
69-
@Override
70-
public CommitResponse apply(Session session) {
71-
return session.writeWithOptions(mutations, options);
72-
}
73-
});
67+
return runWithSessionRetry(session -> session.writeWithOptions(mutations, options));
7468
} catch (RuntimeException e) {
7569
TraceUtil.setWithFailure(span, e);
7670
throw e;
@@ -91,12 +85,7 @@ public CommitResponse writeAtLeastOnceWithOptions(
9185
Span span = tracer.spanBuilder(READ_WRITE_TRANSACTION).startSpan();
9286
try (Scope s = tracer.withSpan(span)) {
9387
return runWithSessionRetry(
94-
new Function<Session, CommitResponse>() {
95-
@Override
96-
public CommitResponse apply(Session session) {
97-
return session.writeAtLeastOnceWithOptions(mutations, options);
98-
}
99-
});
88+
session -> session.writeAtLeastOnceWithOptions(mutations, options));
10089
} catch (RuntimeException e) {
10190
TraceUtil.setWithFailure(span, e);
10291
throw e;
@@ -221,13 +210,7 @@ public AsyncTransactionManager transactionManagerAsync(TransactionOption... opti
221210
public long executePartitionedUpdate(final Statement stmt, final UpdateOption... options) {
222211
Span span = tracer.spanBuilder(PARTITION_DML_TRANSACTION).startSpan();
223212
try (Scope s = tracer.withSpan(span)) {
224-
return runWithSessionRetry(
225-
new Function<Session, Long>() {
226-
@Override
227-
public Long apply(Session session) {
228-
return session.executePartitionedUpdate(stmt, options);
229-
}
230-
});
213+
return runWithSessionRetry(session -> session.executePartitionedUpdate(stmt, options));
231214
} catch (RuntimeException e) {
232215
TraceUtil.endSpanWithFailure(span, e);
233216
throw e;

0 commit comments

Comments
 (0)