Skip to content

Commit af5d9a2

Browse files
authored
chore: uses lambda syntax for Runnables and Callables (#1067)
* chore: uses lambdas for Runnable classes Changes the syntax when using Runnables, from new Runnable(...) { ... } to () -> { ... } * chore: uses lambdas for Callables Instead of using new Callable() { ... }, uses () -> { ... }
1 parent 1b5d6c7 commit af5d9a2

39 files changed

+1011
-1597
lines changed

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

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,13 @@ public <R> ApiFuture<R> runAsync(final AsyncWork<R> work, Executor executor) {
4242
commitResponse = SettableApiFuture.create();
4343
final SettableApiFuture<R> res = SettableApiFuture.create();
4444
executor.execute(
45-
new Runnable() {
46-
@Override
47-
public void run() {
48-
try {
49-
res.set(runTransaction(work));
50-
} catch (Throwable t) {
51-
res.setException(t);
52-
} finally {
53-
setCommitResponse();
54-
}
45+
() -> {
46+
try {
47+
res.set(runTransaction(work));
48+
} catch (Throwable t) {
49+
res.setException(t);
50+
} finally {
51+
setCommitResponse();
5552
}
5653
});
5754
return res;

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import com.google.longrunning.Operation.ResultCase;
2929
import com.google.protobuf.Any;
3030
import com.google.rpc.Status;
31-
import java.util.concurrent.Callable;
3231
import java.util.concurrent.ExecutionException;
3332
import javax.annotation.Nullable;
3433
import org.threeten.bp.Duration;
@@ -143,12 +142,7 @@ public Operation<R, M> waitFor(RetryOption... waitOptions) throws SpannerExcepti
143142
try {
144143
com.google.longrunning.Operation proto =
145144
RetryHelper.poll(
146-
new Callable<com.google.longrunning.Operation>() {
147-
@Override
148-
public com.google.longrunning.Operation call() throws Exception {
149-
return rpc.getOperation(name);
150-
}
151-
},
145+
() -> rpc.getOperation(name),
152146
waitSettings,
153147
new BasicResultRetryAlgorithm<com.google.longrunning.Operation>() {
154148
@Override

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

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -319,29 +319,26 @@ ApiFuture<ByteString> beginTransactionAsync() {
319319
requestFuture.addListener(
320320
tracer.withSpan(
321321
span,
322-
new Runnable() {
323-
@Override
324-
public void run() {
325-
try {
326-
Transaction txn = requestFuture.get();
327-
if (txn.getId().isEmpty()) {
328-
throw newSpannerException(
329-
ErrorCode.INTERNAL, "Missing id in transaction\n" + getName());
330-
}
331-
span.end(TraceUtil.END_SPAN_OPTIONS);
332-
res.set(txn.getId());
333-
} catch (ExecutionException e) {
334-
TraceUtil.endSpanWithFailure(span, e);
335-
res.setException(
336-
SpannerExceptionFactory.newSpannerException(
337-
e.getCause() == null ? e : e.getCause()));
338-
} catch (InterruptedException e) {
339-
TraceUtil.endSpanWithFailure(span, e);
340-
res.setException(SpannerExceptionFactory.propagateInterrupt(e));
341-
} catch (Exception e) {
342-
TraceUtil.endSpanWithFailure(span, e);
343-
res.setException(e);
322+
() -> {
323+
try {
324+
Transaction txn = requestFuture.get();
325+
if (txn.getId().isEmpty()) {
326+
throw newSpannerException(
327+
ErrorCode.INTERNAL, "Missing id in transaction\n" + getName());
344328
}
329+
span.end(TraceUtil.END_SPAN_OPTIONS);
330+
res.set(txn.getId());
331+
} catch (ExecutionException e) {
332+
TraceUtil.endSpanWithFailure(span, e);
333+
res.setException(
334+
SpannerExceptionFactory.newSpannerException(
335+
e.getCause() == null ? e : e.getCause()));
336+
} catch (InterruptedException e) {
337+
TraceUtil.endSpanWithFailure(span, e);
338+
res.setException(SpannerExceptionFactory.propagateInterrupt(e));
339+
} catch (Exception e) {
340+
TraceUtil.endSpanWithFailure(span, e);
341+
res.setException(e);
345342
}
346343
}),
347344
MoreExecutors.directExecutor());

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

Lines changed: 55 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -176,14 +176,11 @@ private AutoClosingReadContextAsyncResultSetImpl(
176176
@Override
177177
public ApiFuture<Void> setCallback(Executor exec, ReadyCallback cb) {
178178
Runnable listener =
179-
new Runnable() {
180-
@Override
181-
public void run() {
182-
synchronized (lock) {
183-
if (asyncOperationsCount.decrementAndGet() == 0 && closed) {
184-
// All async operations for this read context have finished.
185-
AutoClosingReadContext.this.close();
186-
}
179+
() -> {
180+
synchronized (lock) {
181+
if (asyncOperationsCount.decrementAndGet() == 0 && closed) {
182+
// All async operations for this read context have finished.
183+
AutoClosingReadContext.this.close();
187184
}
188185
}
189186
};
@@ -205,7 +202,7 @@ public void run() {
205202
private final boolean isSingleUse;
206203
private final AtomicInteger asyncOperationsCount = new AtomicInteger();
207204

208-
private Object lock = new Object();
205+
private final Object lock = new Object();
209206

210207
@GuardedBy("lock")
211208
private boolean sessionUsedForQuery = false;
@@ -993,49 +990,46 @@ public <R> ApiFuture<R> runAsync(final AsyncWork<R> work, Executor executor) {
993990
commitResponse = SettableApiFuture.create();
994991
final SettableApiFuture<R> res = SettableApiFuture.create();
995992
executor.execute(
996-
new Runnable() {
997-
@Override
998-
public void run() {
999-
SpannerException exception = null;
1000-
R r = null;
1001-
AsyncRunner runner = null;
1002-
while (true) {
1003-
SpannerException se = null;
1004-
try {
1005-
runner = session.get().runAsync(options);
1006-
r = runner.runAsync(work, MoreExecutors.directExecutor()).get();
1007-
break;
1008-
} catch (ExecutionException e) {
1009-
se = SpannerExceptionFactory.asSpannerException(e.getCause());
1010-
} catch (InterruptedException e) {
1011-
se = SpannerExceptionFactory.propagateInterrupt(e);
1012-
} catch (Throwable t) {
1013-
se = SpannerExceptionFactory.newSpannerException(t);
1014-
} finally {
1015-
if (se instanceof SessionNotFoundException) {
1016-
try {
1017-
// The replaceSession method will re-throw the SessionNotFoundException if the
1018-
// session cannot be replaced with a new one.
1019-
session = sessionPool.replaceSession((SessionNotFoundException) se, session);
1020-
se = null;
1021-
} catch (SessionNotFoundException e) {
1022-
exception = e;
1023-
break;
1024-
}
1025-
} else {
1026-
exception = se;
993+
() -> {
994+
SpannerException exception = null;
995+
R r = null;
996+
AsyncRunner runner = null;
997+
while (true) {
998+
SpannerException se = null;
999+
try {
1000+
runner = session.get().runAsync(options);
1001+
r = runner.runAsync(work, MoreExecutors.directExecutor()).get();
1002+
break;
1003+
} catch (ExecutionException e) {
1004+
se = SpannerExceptionFactory.asSpannerException(e.getCause());
1005+
} catch (InterruptedException e) {
1006+
se = SpannerExceptionFactory.propagateInterrupt(e);
1007+
} catch (Throwable t) {
1008+
se = SpannerExceptionFactory.newSpannerException(t);
1009+
} finally {
1010+
if (se instanceof SessionNotFoundException) {
1011+
try {
1012+
// The replaceSession method will re-throw the SessionNotFoundException if the
1013+
// session cannot be replaced with a new one.
1014+
session = sessionPool.replaceSession((SessionNotFoundException) se, session);
1015+
se = null;
1016+
} catch (SessionNotFoundException e) {
1017+
exception = e;
10271018
break;
10281019
}
1020+
} else {
1021+
exception = se;
1022+
break;
10291023
}
10301024
}
1031-
session.get().markUsed();
1032-
session.close();
1033-
setCommitResponse(runner);
1034-
if (exception != null) {
1035-
res.setException(exception);
1036-
} else {
1037-
res.set(r);
1038-
}
1025+
}
1026+
session.get().markUsed();
1027+
session.close();
1028+
setCommitResponse(runner);
1029+
if (exception != null) {
1030+
res.setException(exception);
1031+
} else {
1032+
res.set(r);
10391033
}
10401034
});
10411035
return res;
@@ -1643,15 +1637,7 @@ void init() {
16431637
synchronized (lock) {
16441638
scheduledFuture =
16451639
executor.scheduleAtFixedRate(
1646-
new Runnable() {
1647-
@Override
1648-
public void run() {
1649-
maintainPool();
1650-
}
1651-
},
1652-
loopFrequency,
1653-
loopFrequency,
1654-
TimeUnit.MILLISECONDS);
1640+
this::maintainPool, loopFrequency, loopFrequency, TimeUnit.MILLISECONDS);
16551641
}
16561642
}
16571643

@@ -2237,14 +2223,7 @@ ListenableFuture<Void> closeAsync(ClosedException closedException) {
22372223
}
22382224
}
22392225

2240-
retFuture.addListener(
2241-
new Runnable() {
2242-
@Override
2243-
public void run() {
2244-
executorFactory.release(executor);
2245-
}
2246-
},
2247-
MoreExecutors.directExecutor());
2226+
retFuture.addListener(() -> executorFactory.release(executor), MoreExecutors.directExecutor());
22482227
return retFuture;
22492228
}
22502229

@@ -2264,20 +2243,17 @@ int totalSessions() {
22642243
private ApiFuture<Empty> closeSessionAsync(final PooledSession sess) {
22652244
ApiFuture<Empty> res = sess.delegate.asyncClose();
22662245
res.addListener(
2267-
new Runnable() {
2268-
@Override
2269-
public void run() {
2270-
synchronized (lock) {
2271-
allSessions.remove(sess);
2272-
if (isClosed()) {
2273-
decrementPendingClosures(1);
2274-
return;
2275-
}
2276-
// Create a new session if needed to unblock some waiter.
2277-
if (numWaiters() > numSessionsBeingCreated) {
2278-
createSessions(
2279-
getAllowedCreateSessions(numWaiters() - numSessionsBeingCreated), false);
2280-
}
2246+
() -> {
2247+
synchronized (lock) {
2248+
allSessions.remove(sess);
2249+
if (isClosed()) {
2250+
decrementPendingClosures(1);
2251+
return;
2252+
}
2253+
// Create a new session if needed to unblock some waiter.
2254+
if (numWaiters() > numSessionsBeingCreated) {
2255+
createSessions(
2256+
getAllowedCreateSessions(numWaiters() - numSessionsBeingCreated), false);
22812257
}
22822258
}
22832259
},

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

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,15 @@ private void createTransaction(PooledSessionFuture session) {
5959
this.session = session;
6060
this.delegate = SettableApiFuture.create();
6161
this.session.addListener(
62-
new Runnable() {
63-
@Override
64-
public void run() {
65-
try {
66-
delegate.set(
67-
SessionPoolAsyncTransactionManager.this
68-
.session
69-
.get()
70-
.transactionManagerAsync(options));
71-
} catch (Throwable t) {
72-
delegate.setException(t);
73-
}
62+
() -> {
63+
try {
64+
delegate.set(
65+
SessionPoolAsyncTransactionManager.this
66+
.session
67+
.get()
68+
.transactionManagerAsync(options));
69+
} catch (Throwable t) {
70+
delegate.setException(t);
7471
}
7572
},
7673
MoreExecutors.directExecutor());
@@ -234,14 +231,7 @@ public ApiFuture<Void> rollbackAsync() {
234231
@Override
235232
public ApiFuture<Void> apply(AsyncTransactionManagerImpl input) throws Exception {
236233
ApiFuture<Void> res = input.rollbackAsync();
237-
res.addListener(
238-
new Runnable() {
239-
@Override
240-
public void run() {
241-
session.close();
242-
}
243-
},
244-
MoreExecutors.directExecutor());
234+
res.addListener(() -> session.close(), MoreExecutors.directExecutor());
245235
return res;
246236
}
247237
},

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

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -162,25 +162,23 @@ public static interface CallCredentialsProvider {
162162
* Context context =
163163
* Context.current().withValue(SpannerOptions.CALL_CONTEXT_CONFIGURATOR_KEY, configurator);
164164
* context.run(
165-
* new Runnable() {
166-
* public void run() {
167-
* try {
168-
* client
169-
* .readWriteTransaction()
170-
* .run(
171-
* new TransactionCallable<long[]>() {
172-
* public long[] run(TransactionContext transaction) throws Exception {
173-
* return transaction.batchUpdate(
174-
* ImmutableList.of(statement1, statement2));
175-
* }
176-
* });
177-
* } catch (SpannerException e) {
178-
* if (e.getErrorCode() == ErrorCode.DEADLINE_EXCEEDED) {
179-
* // handle timeout exception.
180-
* }
165+
* () -> {
166+
* try {
167+
* client
168+
* .readWriteTransaction()
169+
* .run(
170+
* new TransactionCallable<long[]>() {
171+
* public long[] run(TransactionContext transaction) throws Exception {
172+
* return transaction.batchUpdate(
173+
* ImmutableList.of(statement1, statement2));
174+
* }
175+
* });
176+
* } catch (SpannerException e) {
177+
* if (e.getErrorCode() == ErrorCode.DEADLINE_EXCEEDED) {
178+
* // handle timeout exception.
181179
* }
182180
* }
183-
* });
181+
* }
184182
* }</pre>
185183
*/
186184
public static interface CallContextConfigurator {
@@ -285,24 +283,22 @@ static <ReqT, RespT> SpannerMethod valueOf(ReqT request, MethodDescriptor<ReqT,
285283
* SpannerCallContextTimeoutConfigurator.create()
286284
* .withExecuteQueryTimeout(Duration.ofSeconds(10L)));
287285
* context.run(
288-
* new Runnable() {
289-
* public void run() {
290-
* try (ResultSet rs =
291-
* client
292-
* .singleUse()
293-
* .executeQuery(
294-
* Statement.of(
295-
* "SELECT SingerId, FirstName, LastName FROM Singers ORDER BY LastName"))) {
296-
* while (rs.next()) {
297-
* System.out.printf("%d %s %s%n", rs.getLong(0), rs.getString(1), rs.getString(2));
298-
* }
299-
* } catch (SpannerException e) {
300-
* if (e.getErrorCode() == ErrorCode.DEADLINE_EXCEEDED) {
301-
* // Handle timeout.
302-
* }
286+
* () -> {
287+
* try (ResultSet rs =
288+
* client
289+
* .singleUse()
290+
* .executeQuery(
291+
* Statement.of(
292+
* "SELECT SingerId, FirstName, LastName FROM Singers ORDER BY LastName"))) {
293+
* while (rs.next()) {
294+
* System.out.printf("%d %s %s%n", rs.getLong(0), rs.getString(1), rs.getString(2));
295+
* }
296+
* } catch (SpannerException e) {
297+
* if (e.getErrorCode() == ErrorCode.DEADLINE_EXCEEDED) {
298+
* // Handle timeout.
303299
* }
304300
* }
305-
* });
301+
* }
306302
* }</pre>
307303
*/
308304
public static class SpannerCallContextTimeoutConfigurator implements CallContextConfigurator {

0 commit comments

Comments
 (0)