Skip to content

Commit f13ce78

Browse files
ludochgae-java-bot
authored andcommitted
Refactor: Use lambda expressions and Character.valueOf.
PiperOrigin-RevId: 862378286 Change-Id: I140e95ffda132265f442bc1d222c587331e367d6
1 parent 146ebd7 commit f13ce78

28 files changed

Lines changed: 202 additions & 360 deletions

File tree

api/src/main/java/com/google/appengine/api/datastore/BaseQueryResultsSource.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,7 @@ public BaseQueryResultsSource(
129129
this.offset = fetchOptions.getOffset() != null ? fetchOptions.getOffset() : 0;
130130
this.txn = txn;
131131
this.query = query;
132-
this.currentTransactionProvider =
133-
new CurrentTransactionProvider() {
134-
@Override
135-
public Transaction getCurrentTransaction(Transaction defaultValue) {
136-
return txn;
137-
}
138-
};
132+
this.currentTransactionProvider = defaultValue -> txn;
139133
this.initialQueryResultFuture = initialQueryResultFuture;
140134
this.skippedResults = 0;
141135
}

api/src/main/java/com/google/appengine/api/datastore/DatastoreCallbacksImpl.java

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -336,22 +336,19 @@ private static Object newInstance(Class<?> cls) {
336336
}
337337

338338
private Callback allocateCallback(final Object callbackImplementor, final Method callbackMethod) {
339-
return new Callback() {
340-
@Override
341-
public void run(CallbackContext<?> context) {
342-
try {
343-
callbackMethod.invoke(callbackImplementor, context);
344-
} catch (IllegalAccessException e) {
345-
// shouldn't happen since we made the method accessible
346-
throw new RuntimeException(e);
347-
} catch (InvocationTargetException e) {
348-
// TODO: Check whether it is possible for this to be null
349-
if (e.getCause() != null) {
350-
throwIfUnchecked(e.getCause());
351-
}
352-
// Should not happen since we don't allow checked exceptions.
353-
throw new RuntimeException("Callback method threw a checked exception.", e.getCause());
339+
return context -> {
340+
try {
341+
callbackMethod.invoke(callbackImplementor, context);
342+
} catch (IllegalAccessException e) {
343+
// shouldn't happen since we made the method accessible
344+
throw new RuntimeException(e);
345+
} catch (InvocationTargetException e) {
346+
// TODO: Check whether it is possible for this to be null
347+
if (e.getCause() != null) {
348+
throwIfUnchecked(e.getCause());
354349
}
350+
// Should not happen since we don't allow checked exceptions.
351+
throw new RuntimeException("Callback method threw a checked exception.", e.getCause());
355352
}
356353
};
357354
}

api/src/main/java/com/google/appengine/api/datastore/DatastoreConfig.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,5 @@ public interface DatastoreConfig {
3939
*/
4040
@Deprecated
4141
DatastoreConfig DEFAULT =
42-
new DatastoreConfig() {
43-
@Override
44-
public ImplicitTransactionManagementPolicy getImplicitTransactionManagementPolicy() {
45-
return ImplicitTransactionManagementPolicy.NONE;
46-
}
47-
};
42+
() -> ImplicitTransactionManagementPolicy.NONE;
4843
}

api/src/main/java/com/google/appengine/api/memcache/AsyncMemcacheServiceImpl.java

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -234,35 +234,15 @@ public int hashCode() {
234234
private static class DefaultValueProviders {
235235

236236
@SuppressWarnings("rawtypes")
237-
private static final Provider NULL_PROVIDER = new Provider() {
238-
@Override public Object get() {
239-
return null;
240-
}
241-
};
237+
private static final Provider NULL_PROVIDER = () -> null;
242238

243-
private static final Provider<Boolean> FALSE_PROVIDER = new Provider<Boolean>() {
244-
@Override public Boolean get() {
245-
return Boolean.FALSE;
246-
}
247-
};
239+
private static final Provider<Boolean> FALSE_PROVIDER = () -> Boolean.FALSE;
248240

249241
@SuppressWarnings("rawtypes")
250-
private static final Provider SET_PROVIDER =
251-
new Provider<Set<?>>() {
252-
@Override
253-
public Set<?> get() {
254-
return new HashSet<>(0, 1);
255-
}
256-
};
242+
private static final Provider SET_PROVIDER = () -> new HashSet<>(0, 1);
257243

258244
@SuppressWarnings("rawtypes")
259-
private static final Provider MAP_PROVIDER =
260-
new Provider<Map<?, ?>>() {
261-
@Override
262-
public Map<?, ?> get() {
263-
return new HashMap<>(0, 1);
264-
}
265-
};
245+
private static final Provider MAP_PROVIDER = () -> new HashMap<>(0, 1);
266246

267247
private static final Provider<Stats> STATS_PROVIDER = new Provider<Stats>() {
268248
final Stats emptyStats = new AsyncMemcacheServiceImpl.StatsImpl(null);

api/src/main/java/com/google/appengine/api/utils/SystemProperty.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public class SystemProperty {
4242
* {@code "com.google.appengine.runtime.environment"}.
4343
* Has the values {@code "Production"} and {@code "Development"}.
4444
*/
45+
@SuppressWarnings("ClassInitializationDeadlock")
4546
public static final Environment environment = new Environment();
4647

4748
/**

api/src/main/java/com/google/appengine/setup/RequestThreadFactory.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,14 @@ public RequestThreadFactory(Environment requestEnvironment) {
6161
public Thread newThread(final Runnable runnable) {
6262
checkState(requestEnvironment != null,
6363
"Request threads can only be created within the context of a running request.");
64-
Thread thread = new Thread(new Runnable() {
65-
@Override
66-
public void run() {
67-
if (runnable == null) {
68-
return;
69-
}
70-
checkState(allowNewRequestThreadCreation,
71-
"Cannot start new threads after the request thread stops.");
72-
ApiProxy.setEnvironmentForCurrentThread(requestEnvironment);
73-
runnable.run();
64+
Thread thread = new Thread(() -> {
65+
if (runnable == null) {
66+
return;
7467
}
68+
checkState(allowNewRequestThreadCreation,
69+
"Cannot start new threads after the request thread stops.");
70+
ApiProxy.setEnvironmentForCurrentThread(requestEnvironment);
71+
runnable.run();
7572
});
7673
checkState(
7774
allowNewRequestThreadCreation, "Cannot create new threads after the request thread stops.");

api_dev/src/main/java/com/google/appengine/api/search/dev/PrefixFieldAnalyzerUtil.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.util.ArrayList;
2626
import java.util.LinkedList;
2727
import java.util.List;
28+
import java.util.Locale;
2829
import java.util.logging.Level;
2930
import java.util.logging.Logger;
3031
import org.apache.lucene.analysis.LetterTokenizer;
@@ -43,7 +44,9 @@ final class PrefixFieldAnalyzerUtil {
4344

4445
static String normalizePrefixField(String value) {
4546
String normalizedString = Normalizer.normalize(value, Normalizer.Form.NFKC);
46-
return CharMatcher.whitespace().trimAndCollapseFrom(normalizedString, ' ').toLowerCase();
47+
return CharMatcher.whitespace()
48+
.trimAndCollapseFrom(normalizedString, ' ')
49+
.toLowerCase(Locale.ROOT);
4750
}
4851

4952
static List<String> createUntokenizedPrefixes(String value) {
@@ -83,7 +86,7 @@ protected char normalize(char c) {
8386
*/
8487
@Override
8588
protected boolean isTokenChar(char c) {
86-
return !LuceneUtils.WORD_SEPARATORS.contains(new Character(c));
89+
return !LuceneUtils.WORD_SEPARATORS.contains(Character.valueOf(c));
8790
}
8891
}
8992

api_dev/src/main/java/com/google/appengine/tools/development/AbstractContainerService.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import java.net.UnknownHostException;
3737
import java.security.Permissions;
3838
import java.util.Collection;
39-
import java.util.Collections;
4039
import java.util.Map;
4140
import java.util.concurrent.CountDownLatch;
4241
import java.util.logging.Level;
@@ -118,13 +117,7 @@ public abstract class AbstractContainerService implements ContainerService {
118117
// mapping is available. The advantage to this approach is that it avoids changing
119118
// the public ContainerService interface and hence avoids exposing our management
120119
// of port mappings to users.
121-
protected PortMappingProvider portMappingProvider =
122-
new PortMappingProvider() {
123-
@Override
124-
public Map<String, String> getPortMapping() {
125-
return Collections.emptyMap();
126-
}
127-
};
120+
protected PortMappingProvider portMappingProvider = () -> ImmutableMap.of();
128121

129122
/**
130123
* Latch that will open once the module instance is fully initialized. TODO: This is used by some

api_dev/src/main/java/com/google/appengine/tools/development/Modules.java

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -227,12 +227,7 @@ public String getScalingType(final String moduleName) throws ApplicationExceptio
227227
@Override
228228
public void startModule(final String moduleName, final String version)
229229
throws ApplicationException {
230-
doDynamicConfiguration("startServing", new Runnable(){
231-
@Override
232-
public void run() {
233-
doStartModule(moduleName, version);
234-
}
235-
});
230+
doDynamicConfiguration("startServing", () -> doStartModule(moduleName, version));
236231
}
237232

238233
private void doStartModule(String moduleName, String version) {
@@ -251,12 +246,7 @@ private void doStartModule(String moduleName, String version) {
251246
@Override
252247
public void stopModule(final String moduleName, final String version)
253248
throws ApplicationException {
254-
doDynamicConfiguration("stopServing", new Runnable(){
255-
@Override
256-
public void run() {
257-
doStopModule(moduleName, version);
258-
}
259-
});
249+
doDynamicConfiguration("stopServing", () -> doStopModule(moduleName, version));
260250
}
261251

262252
/**

api_dev/src/main/java/com/google/appengine/tools/development/RequestThreadFactory.java

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -65,43 +65,42 @@ public synchronized void start() {
6565
super.start();
6666
final Thread thread = this; // Thread.this doesn't work from an anon subclass
6767
RequestEndListenerHelper.register(
68-
new RequestEndListener() {
69-
@Override
70-
public void onRequestEnd(ApiProxy.Environment environment) {
68+
env -> {
69+
if (thread.isAlive()) {
70+
logger.info("Interrupting request thread: " + thread);
71+
// This is one of the few places where it's okay to call thread.interrupt().
72+
@SuppressWarnings("Interruption")
73+
boolean unused1 = true;
74+
thread.interrupt();
75+
logger.info("Waiting up to 100ms for thread to complete: " + thread);
76+
try {
77+
thread.join(100);
78+
} catch (InterruptedException ex) {
79+
logger.info("Interrupted while waiting.");
80+
}
7181
if (thread.isAlive()) {
72-
logger.info("Interrupting request thread: " + thread);
82+
logger.info("Interrupting request thread again: " + thread);
83+
@SuppressWarnings("Interruption")
84+
boolean unused2 = true;
7385
thread.interrupt();
74-
logger.info("Waiting up to 100ms for thread to complete: " + thread);
86+
long remaining = getRemainingDeadlineMillis(env);
87+
logger.info(
88+
"Waiting up to " + remaining + " ms for thread to complete: " + thread);
7589
try {
76-
thread.join(100);
90+
thread.join(remaining);
7791
} catch (InterruptedException ex) {
7892
logger.info("Interrupted while waiting.");
7993
}
8094
if (thread.isAlive()) {
81-
logger.info("Interrupting request thread again: " + thread);
82-
thread.interrupt();
83-
long remaining = getRemainingDeadlineMillis(environment);
84-
logger.info(
85-
"Waiting up to "
86-
+ remaining
87-
+ " ms for thread to complete: "
88-
+ thread);
89-
try {
90-
thread.join(remaining);
91-
} catch (InterruptedException ex) {
92-
logger.info("Interrupted while waiting.");
93-
}
94-
if (thread.isAlive()) {
95-
Throwable stack = new Throwable();
96-
stack.setStackTrace(thread.getStackTrace());
97-
logger.log(
98-
Level.SEVERE,
99-
"Thread left running: "
100-
+ thread
101-
+ ". "
102-
+ "In production this will cause the request to fail.",
103-
stack);
104-
}
95+
Throwable stack = new Throwable();
96+
stack.setStackTrace(thread.getStackTrace());
97+
logger.log(
98+
Level.SEVERE,
99+
"Thread left running: "
100+
+ thread
101+
+ ". "
102+
+ "In production this will cause the request to fail.",
103+
stack);
105104
}
106105
}
107106
}

0 commit comments

Comments
 (0)