Skip to content

Commit 6e5d54f

Browse files
authored
refactor: cleans up code / uses java 8 features (#1072)
* chore: refactor c style array declaration Moves from c style array declaration to java style. E.g. int array[] to int[] array. * chore: refactor to use ut8 standard charset Instead of using the literal String UTF-8 * chore: removes redundant string conversions * chore: refactor enums Removes redundant private modifier in enum constructors. Removes redundant static modifier in inner class enums. * chore: refactor interfaces Removes redundant modifiers from interfaces. * chore: removes unnecessary semicolons * chore: remove redundant local variable declaration * chore: removes redundant throws clause * chore: removes redundant call to close method * chore: optimise imports * chore: removes dangling javadoc comment * chore: removes self-references in javadoc * chore: refactor long literals From lower case l to upper case L * chore: removes octal integers from tests * chore: simplify arithmetic expressions * chore: fixes malformed string.format * chore: removes ignored method call results * chore: compare strings with equals instead of == * chore: simplifies test assertions * chore: removes redundant calls to string.format * chore: explicit type argument replaced by <> * chore: uses try with resources * chore: replaces if block by switch statement * chore: uses enhanced for loop * chore: removes unnecessary boxing * chore: removes unnecessary unboxing * chore: refactors to use map.computeIfAbsent * chore: addresses PR comments * chore: fixes compilation errors
1 parent 03312dc commit 6e5d54f

File tree

80 files changed

+316
-358
lines changed

Some content is hidden

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

80 files changed

+316
-358
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -794,8 +794,7 @@ CloseableIterator<PartialResultSet> startStream(@Nullable ByteString resumeToken
794794
return stream;
795795
}
796796
};
797-
GrpcResultSet resultSet = new GrpcResultSet(stream, this);
798-
return resultSet;
797+
return new GrpcResultSet(stream, this);
799798
}
800799

801800
private Struct consumeSingleRow(ResultSet resultSet) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ protected com.google.protobuf.Value computeNext() {
208208
Object merged =
209209
kind == KindCase.STRING_VALUE
210210
? value.getStringValue()
211-
: new ArrayList<com.google.protobuf.Value>(value.getListValue().getValuesList());
211+
: new ArrayList<>(value.getListValue().getValuesList());
212212
while (current.getChunkedValue() && pos == current.getValuesCount()) {
213213
if (!ensureReady(StreamValue.RESULT)) {
214214
throw newSpannerException(

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,8 @@ enum CursorState {
3939
* Non-blocking call that attempts to step the cursor to the next position in the stream. The
4040
* cursor may be inspected only if the cursor returns {@code CursorState.OK}.
4141
*
42-
* <p>A caller will typically call {@link #tryNext()} in a loop inside the ReadyCallback,
43-
* consuming all results available. For more information see {@link #setCallback(Executor,
44-
* ReadyCallback)}.
42+
* <p>A caller will typically call tryNext in a loop inside the ReadyCallback, consuming all
43+
* results available. For more information see {@link #setCallback(Executor, ReadyCallback)}.
4544
*
4645
* <p>Currently this method may only be called if a ReadyCallback has been registered. This is for
4746
* safety purposes only, and may be relaxed in future.
@@ -146,8 +145,8 @@ interface ReadyCallback {
146145
* <ul>
147146
* <li>Semi-async: make {@code upstream.emit()} a blocking call. This will block the callback
148147
* thread until progress is possible. When coding in this way the threads in the Executor
149-
* provided to {@link #setCallback(Executor, ReadyCallback)} must be blockable without
150-
* causing harm to progress in your system.
148+
* provided to setCallback must be blockable without causing harm to progress in your
149+
* system.
151150
* <li>Full-async: call {@code cursor.pause()} and return from the callback with data still in
152151
* the Cursor. Once in this state cursor waits until resume() is called before calling
153152
* callback again.

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

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,11 @@ private enum State {
6262
/** Does this state mean that the result set should permanently stop producing rows. */
6363
private final boolean shouldStop;
6464

65-
private State() {
65+
State() {
6666
shouldStop = false;
6767
}
6868

69-
private State(boolean shouldStop) {
69+
State(boolean shouldStop) {
7070
this.shouldStop = shouldStop;
7171
}
7272
}
@@ -116,32 +116,31 @@ private State(boolean shouldStop) {
116116
private State state = State.INITIALIZED;
117117

118118
/**
119-
* {@link #finished} indicates whether all the results from the underlying result set have been
120-
* read.
119+
* This variable indicates whether all the results from the underlying result set have been read.
121120
*/
122121
private volatile boolean finished;
123122

124123
private volatile ApiFuture<Void> result;
125124

126125
/**
127-
* {@link #cursorReturnedDoneOrException} indicates whether {@link #tryNext()} has returned {@link
128-
* CursorState#DONE} or a {@link SpannerException}.
126+
* This variable indicates whether {@link #tryNext()} has returned {@link CursorState#DONE} or a
127+
* {@link SpannerException}.
129128
*/
130129
private volatile boolean cursorReturnedDoneOrException;
131130

132131
/**
133-
* {@link #pausedLatch} is used to pause the producer when the {@link AsyncResultSet} is paused.
134-
* The production of rows that are put into the buffer is only paused once the buffer is full.
132+
* This variable is used to pause the producer when the {@link AsyncResultSet} is paused. The
133+
* production of rows that are put into the buffer is only paused once the buffer is full.
135134
*/
136135
private volatile CountDownLatch pausedLatch = new CountDownLatch(1);
137136
/**
138-
* {@link #bufferConsumptionLatch} is used to pause the producer when the buffer is full and the
139-
* consumer needs some time to catch up.
137+
* This variable is used to pause the producer when the buffer is full and the consumer needs some
138+
* time to catch up.
140139
*/
141140
private volatile CountDownLatch bufferConsumptionLatch = new CountDownLatch(0);
142141
/**
143-
* {@link #consumingLatch} is used to pause the producer when all rows have been put into the
144-
* buffer, but the consumer (the callback) has not yet received and processed all rows.
142+
* This variable is used to pause the producer when all rows have been put into the buffer, but
143+
* the consumer (the callback) has not yet received and processed all rows.
145144
*/
146145
private volatile CountDownLatch consumingLatch = new CountDownLatch(0);
147146

@@ -531,7 +530,7 @@ public <T> ApiFuture<List<T>> toListAsync(
531530
Preconditions.checkState(
532531
this.state == State.INITIALIZED, "This AsyncResultSet has already been used.");
533532
final SettableApiFuture<List<T>> res = SettableApiFuture.<List<T>>create();
534-
CreateListCallback<T> callback = new CreateListCallback<T>(res, transformer);
533+
CreateListCallback<T> callback = new CreateListCallback<>(res, transformer);
535534
ApiFuture<Void> finished = setCallback(executor, callback);
536535
return ApiFutures.transformAsync(
537536
finished,

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public interface AsyncTransactionManager extends AutoCloseable {
4949
* {@link ApiFuture} that returns a {@link TransactionContext} and that supports chaining of
5050
* multiple {@link TransactionContextFuture}s to form a transaction.
5151
*/
52-
public interface TransactionContextFuture extends ApiFuture<TransactionContext> {
52+
interface TransactionContextFuture extends ApiFuture<TransactionContext> {
5353
/**
5454
* Sets the first step to execute as part of this transaction after the transaction has started
5555
* using the specified executor. {@link MoreExecutors#directExecutor()} can be be used for
@@ -65,7 +65,7 @@ <O> AsyncTransactionStep<Void, O> then(
6565
* is executed using an {@link AsyncTransactionManager}. This future is returned by the call to
6666
* {@link AsyncTransactionStep#commitAsync()} of the last step in the transaction.
6767
*/
68-
public interface CommitTimestampFuture extends ApiFuture<Timestamp> {
68+
interface CommitTimestampFuture extends ApiFuture<Timestamp> {
6969
/**
7070
* Returns the commit timestamp of the transaction. Getting this value should always be done in
7171
* order to ensure that the transaction succeeded. If any of the steps in the transaction fails
@@ -125,7 +125,7 @@ Timestamp get(long timeout, TimeUnit unit)
125125
* })
126126
* }</pre>
127127
*/
128-
public interface AsyncTransactionStep<I, O> extends ApiFuture<O> {
128+
interface AsyncTransactionStep<I, O> extends ApiFuture<O> {
129129
/**
130130
* Adds a step to the transaction chain that should be executed using the specified executor.
131131
* This step is guaranteed to be executed only after the previous step executed successfully.
@@ -150,11 +150,11 @@ <RES> AsyncTransactionStep<O, RES> then(
150150
* parameters. The method should return an {@link ApiFuture} that will return the result of this
151151
* step.
152152
*/
153-
public interface AsyncTransactionFunction<I, O> {
153+
interface AsyncTransactionFunction<I, O> {
154154
/**
155-
* {@link #apply(TransactionContext, Object)} is called when this transaction step is executed.
156-
* The input value is the result of the previous step, and this method will only be called if
157-
* the previous step executed successfully.
155+
* This method is called when this transaction step is executed. The input value is the result
156+
* of the previous step, and this method will only be called if the previous step executed
157+
* successfully.
158158
*
159159
* @param txn the {@link TransactionContext} that can be used to execute statements.
160160
* @param input the result of the previous transaction step.

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,7 @@ public ApiFuture<Void> closeAsync() {
7979
@Override
8080
public TransactionContextFutureImpl beginAsync() {
8181
Preconditions.checkState(txn == null, "begin can only be called once");
82-
TransactionContextFutureImpl begin =
83-
new TransactionContextFutureImpl(this, internalBeginAsync(true));
84-
return begin;
82+
return new TransactionContextFutureImpl(this, internalBeginAsync(true));
8583
}
8684

8785
private ApiFuture<TransactionContext> internalBeginAsync(boolean firstAttempt) {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public OperationFuture<Database, RestoreDatabaseMetadata> restoreDatabase(Restor
105105
final OperationFuture<com.google.spanner.admin.database.v1.Database, RestoreDatabaseMetadata>
106106
rawOperationFuture = rpc.restoreDatabase(restore);
107107

108-
return new OperationFutureImpl<Database, RestoreDatabaseMetadata>(
108+
return new OperationFutureImpl<>(
109109
rawOperationFuture.getPollingFuture(),
110110
rawOperationFuture.getInitialFuture(),
111111
new ApiFunction<OperationSnapshot, Database>() {
@@ -308,7 +308,7 @@ public OperationFuture<Database, CreateDatabaseMetadata> createDatabase(
308308
rawOperationFuture =
309309
rpc.createDatabase(
310310
database.getId().getInstanceId().getName(), createStatement, statements, database);
311-
return new OperationFutureImpl<Database, CreateDatabaseMetadata>(
311+
return new OperationFutureImpl<>(
312312
rawOperationFuture.getPollingFuture(),
313313
rawOperationFuture.getInitialFuture(),
314314
new ApiFunction<OperationSnapshot, Database>() {
@@ -347,7 +347,7 @@ public OperationFuture<Void, UpdateDatabaseDdlMetadata> updateDatabaseDdl(
347347
final String opId = operationId != null ? operationId : randomOperationId();
348348
OperationFuture<Empty, UpdateDatabaseDdlMetadata> rawOperationFuture =
349349
rpc.updateDatabaseDdl(dbName, statements, opId);
350-
return new OperationFutureImpl<Void, UpdateDatabaseDdlMetadata>(
350+
return new OperationFutureImpl<>(
351351
rawOperationFuture.getPollingFuture(),
352352
rawOperationFuture.getInitialFuture(),
353353
new ApiFunction<OperationSnapshot, Void>() {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public OperationFuture<Instance, CreateInstanceMetadata> createInstance(Instance
103103
rawOperationFuture =
104104
rpc.createInstance(projectName, instance.getId().getInstance(), instance.toProto());
105105

106-
return new OperationFutureImpl<Instance, CreateInstanceMetadata>(
106+
return new OperationFutureImpl<>(
107107
rawOperationFuture.getPollingFuture(),
108108
rawOperationFuture.getInitialFuture(),
109109
new ApiFunction<OperationSnapshot, Instance>() {
@@ -172,7 +172,7 @@ public OperationFuture<Instance, UpdateInstanceMetadata> updateInstance(
172172

173173
OperationFuture<com.google.spanner.admin.instance.v1.Instance, UpdateInstanceMetadata>
174174
rawOperationFuture = rpc.updateInstance(instance.toProto(), fieldMask);
175-
return new OperationFutureImpl<Instance, UpdateInstanceMetadata>(
175+
return new OperationFutureImpl<>(
176176
rawOperationFuture.getPollingFuture(),
177177
rawOperationFuture.getInitialFuture(),
178178
new ApiFunction<OperationSnapshot, Instance>() {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ static FieldMask toFieldMask(InstanceField... fields) {
5656
}
5757

5858
/** State of the Instance. */
59-
public static enum State {
59+
public enum State {
6060
UNSPECIFIED,
6161
CREATING,
6262
READY

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public class Operation<R, M> {
4545
.setMaxRetryDelay(Duration.ofMinutes(500L))
4646
.build();
4747

48-
static interface Parser<R, M> {
48+
interface Parser<R, M> {
4949
R parseResult(Any response);
5050

5151
M parseMetadata(Any metadata);
@@ -85,7 +85,7 @@ private static <R, M> Operation<R, M> failed(
8585
SpannerException e =
8686
SpannerExceptionFactory.newSpannerException(
8787
ErrorCode.fromRpcStatus(status), status.getMessage(), null);
88-
return new Operation<R, M>(rpc, name, metadata, null, e, true, parser, clock);
88+
return new Operation<>(rpc, name, metadata, null, e, true, parser, clock);
8989
}
9090

9191
private static <R, M> Operation<R, M> successful(

0 commit comments

Comments
 (0)