Skip to content

Commit 2626a3a

Browse files
emacampolophilwebb
authored andcommitted
Use lambdas when possible
Replace anonymous inner classes with lambda declarations (when possible using method references). See gh-9781
1 parent d16af43 commit 2626a3a

File tree

150 files changed

+983
-2596
lines changed

Some content is hidden

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

150 files changed

+983
-2596
lines changed

spring-boot-actuator-docs/src/restdoc/java/org/springframework/boot/actuate/hypermedia/EndpointDocumentation.java

Lines changed: 27 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@
5252
import org.springframework.test.context.junit4.SpringRunner;
5353
import org.springframework.test.context.web.WebAppConfiguration;
5454
import org.springframework.test.web.servlet.MockMvc;
55-
import org.springframework.test.web.servlet.MvcResult;
56-
import org.springframework.test.web.servlet.ResultHandler;
5755
import org.springframework.util.FileCopyUtils;
5856
import org.springframework.util.StringUtils;
5957

@@ -161,32 +159,9 @@ public void auditEventsByPrincipalAndType() throws Exception {
161159

162160
@Test
163161
public void endpoints() throws Exception {
164-
final File docs = new File("src/main/asciidoc");
165-
final Map<String, Object> model = new LinkedHashMap<>();
166-
final List<EndpointDoc> endpoints = new ArrayList<>();
167-
model.put("endpoints", endpoints);
168-
for (MvcEndpoint endpoint : getEndpoints()) {
169-
final String endpointPath = (StringUtils.hasText(endpoint.getPath())
170-
? endpoint.getPath() : "/");
171-
if (!SKIPPED.contains(endpointPath)) {
172-
String output = endpointPath.substring(1);
173-
output = output.length() > 0 ? output : "./";
174-
this.mockMvc
175-
.perform(get("/application" + endpointPath)
176-
.accept(ActuatorMediaTypes.APPLICATION_ACTUATOR_V2_JSON))
177-
.andExpect(status().isOk()).andDo(document(output))
178-
.andDo(new ResultHandler() {
179-
180-
@Override
181-
public void handle(MvcResult mvcResult) throws Exception {
182-
EndpointDoc endpoint = new EndpointDoc(docs,
183-
endpointPath);
184-
endpoints.add(endpoint);
185-
}
186-
187-
});
188-
}
189-
}
162+
File docs = new File("src/main/asciidoc");
163+
Map<String, Object> model = new LinkedHashMap<>();
164+
model.put("endpoints", getEndpointDocs(docs));
190165
File file = new File(RESTDOCS_OUTPUT_DIR + "/endpoints.adoc");
191166
file.getParentFile().mkdirs();
192167
try (PrintWriter writer = new PrintWriter(file, "UTF-8")) {
@@ -196,18 +171,36 @@ public void handle(MvcResult mvcResult) throws Exception {
196171
}
197172
}
198173

174+
private List<EndpointDoc> getEndpointDocs(File docs) throws Exception {
175+
final List<EndpointDoc> endpoints = new ArrayList<>();
176+
for (MvcEndpoint endpoint : getEndpoints()) {
177+
String path = endpoint.getPath();
178+
path = (StringUtils.hasText(path) ? path : "/");
179+
if (!SKIPPED.contains(path)) {
180+
documentEndpoint(path);
181+
endpoints.add(new EndpointDoc(docs, path));
182+
}
183+
}
184+
return endpoints;
185+
}
186+
199187
private Collection<? extends MvcEndpoint> getEndpoints() {
200188
List<? extends MvcEndpoint> endpoints = new ArrayList<>(
201189
this.mvcEndpoints.getEndpoints());
202-
endpoints.sort(new Comparator<MvcEndpoint>() {
203-
@Override
204-
public int compare(MvcEndpoint o1, MvcEndpoint o2) {
205-
return o1.getPath().compareTo(o2.getPath());
206-
}
207-
});
190+
endpoints.sort(Comparator.comparing(MvcEndpoint::getPath));
208191
return endpoints;
209192
}
210193

194+
private String documentEndpoint(final String endpointPath) throws Exception {
195+
String output = endpointPath.substring(1);
196+
output = (output.length() > 0 ? output : "./");
197+
this.mockMvc
198+
.perform(get("/application" + endpointPath)
199+
.accept(ActuatorMediaTypes.APPLICATION_ACTUATOR_V2_JSON))
200+
.andExpect(status().isOk()).andDo(document(output));
201+
return output;
202+
}
203+
211204
public static class EndpointDoc {
212205

213206
private String path;

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/CacheStatisticsAutoConfiguration.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2016 the original author or authors.
2+
* Copyright 2012-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -137,15 +137,11 @@ static class NoOpCacheStatisticsConfiguration {
137137

138138
@Bean
139139
public CacheStatisticsProvider<Cache> noOpCacheStatisticsProvider() {
140-
return new CacheStatisticsProvider<Cache>() {
141-
@Override
142-
public CacheStatistics getCacheStatistics(CacheManager cacheManager,
143-
Cache cache) {
144-
if (cacheManager instanceof NoOpCacheManager) {
145-
return NO_OP_STATS;
146-
}
147-
return null;
140+
return (cacheManager, cache) -> {
141+
if (cacheManager instanceof NoOpCacheManager) {
142+
return NO_OP_STATS;
148143
}
144+
return null;
149145
};
150146
}
151147

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/EndpointWebMvcAutoConfiguration.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -120,14 +120,7 @@ public ManagementContextResolver managementContextResolver() {
120120
@Bean
121121
public ManagementServletContext managementServletContext(
122122
final ManagementServerProperties properties) {
123-
return new ManagementServletContext() {
124-
125-
@Override
126-
public String getContextPath() {
127-
return properties.getContextPath();
128-
}
129-
130-
};
123+
return properties::getContextPath;
131124
}
132125

133126
@Override

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/EndpointWebMvcHypermediaManagementContextConfiguration.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,7 @@ public class EndpointWebMvcHypermediaManagementContextConfiguration {
101101
@Bean
102102
public ManagementServletContext managementServletContext(
103103
final ManagementServerProperties properties) {
104-
return new ManagementServletContext() {
105-
106-
@Override
107-
public String getContextPath() {
108-
return properties.getContextPath();
109-
}
110-
111-
};
104+
return properties::getContextPath;
112105
}
113106

114107
@ConditionalOnEnabledEndpoint("actuator")

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/ShutdownEndpoint.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2016 the original author or authors.
2+
* Copyright 2012-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -62,23 +62,22 @@ public Map<String, Object> invoke() {
6262
return SHUTDOWN_MESSAGE;
6363
}
6464
finally {
65-
Thread thread = new Thread(new Runnable() {
66-
@Override
67-
public void run() {
68-
try {
69-
Thread.sleep(500L);
70-
}
71-
catch (InterruptedException ex) {
72-
Thread.currentThread().interrupt();
73-
}
74-
ShutdownEndpoint.this.context.close();
75-
}
76-
});
65+
Thread thread = new Thread(this::performShutdown);
7766
thread.setContextClassLoader(getClass().getClassLoader());
7867
thread.start();
7968
}
8069
}
8170

71+
private void performShutdown() {
72+
try {
73+
Thread.sleep(500L);
74+
}
75+
catch (InterruptedException ex) {
76+
Thread.currentThread().interrupt();
77+
}
78+
this.context.close();
79+
}
80+
8281
@Override
8382
public void setApplicationContext(ApplicationContext context) throws BeansException {
8483
if (context instanceof ConfigurableApplicationContext) {

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/DataSourceHealthIndicator.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2016 the original author or authors.
2+
* Copyright 2012-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -26,7 +26,6 @@
2626

2727
import org.springframework.beans.factory.InitializingBean;
2828
import org.springframework.boot.jdbc.DatabaseDriver;
29-
import org.springframework.dao.DataAccessException;
3029
import org.springframework.dao.support.DataAccessUtils;
3130
import org.springframework.jdbc.IncorrectResultSetColumnCountException;
3231
import org.springframework.jdbc.core.ConnectionCallback;
@@ -120,13 +119,11 @@ private void doDataSourceHealthCheck(Health.Builder builder) throws Exception {
120119
}
121120

122121
private String getProduct() {
123-
return this.jdbcTemplate.execute(new ConnectionCallback<String>() {
124-
@Override
125-
public String doInConnection(Connection connection)
126-
throws SQLException, DataAccessException {
127-
return connection.getMetaData().getDatabaseProductName();
128-
}
129-
});
122+
return this.jdbcTemplate.execute((ConnectionCallback<String>) this::getProduct);
123+
}
124+
125+
private String getProduct(Connection connection) throws SQLException {
126+
return connection.getMetaData().getDatabaseProductName();
130127
}
131128

132129
protected String getValidationQuery(String product) {

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/RabbitHealthIndicator.java

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2014 the original author or authors.
2+
* Copyright 2012-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,11 +16,6 @@
1616

1717
package org.springframework.boot.actuate.health;
1818

19-
import java.util.Map;
20-
21-
import com.rabbitmq.client.Channel;
22-
23-
import org.springframework.amqp.rabbit.core.ChannelCallback;
2419
import org.springframework.amqp.rabbit.core.RabbitTemplate;
2520
import org.springframework.util.Assert;
2621

@@ -46,14 +41,8 @@ protected void doHealthCheck(Health.Builder builder) throws Exception {
4641
}
4742

4843
private String getVersion() {
49-
return this.rabbitTemplate.execute(new ChannelCallback<String>() {
50-
@Override
51-
public String doInRabbit(Channel channel) throws Exception {
52-
Map<String, Object> serverProperties = channel.getConnection()
53-
.getServerProperties();
54-
return serverProperties.get("version").toString();
55-
}
56-
});
44+
return this.rabbitTemplate.execute((channel) -> channel.getConnection()
45+
.getServerProperties().get("version").toString());
5746
}
5847

5948
}

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/buffer/BufferMetricReader.java

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import java.util.ArrayList;
2020
import java.util.Date;
2121
import java.util.List;
22-
import java.util.function.BiConsumer;
2322
import java.util.function.Predicate;
2423
import java.util.regex.Pattern;
2524

@@ -48,7 +47,7 @@ public BufferMetricReader(CounterBuffers counterBuffers, GaugeBuffers gaugeBuffe
4847
}
4948

5049
@Override
51-
public Metric<?> findOne(final String name) {
50+
public Metric<?> findOne(String name) {
5251
Buffer<?> buffer = this.counterBuffers.find(name);
5352
if (buffer == null) {
5453
buffer = this.gaugeBuffers.find(name);
@@ -81,17 +80,10 @@ private Iterable<Metric<?>> findAll(Predicate<String> predicate) {
8180
private <T extends Number, B extends Buffer<T>> void collectMetrics(
8281
Buffers<B> buffers, Predicate<String> predicate,
8382
final List<Metric<?>> metrics) {
84-
buffers.forEach(predicate, new BiConsumer<String, B>() {
85-
86-
@Override
87-
public void accept(String name, B value) {
88-
metrics.add(asMetric(name, value));
89-
}
90-
91-
});
83+
buffers.forEach(predicate, (name, value) -> metrics.add(asMetric(name, value)));
9284
}
9385

94-
private <T extends Number> Metric<T> asMetric(final String name, Buffer<T> buffer) {
86+
private <T extends Number> Metric<T> asMetric(String name, Buffer<T> buffer) {
9587
return new Metric<>(name, buffer.getValue(), new Date(buffer.getTimestamp()));
9688
}
9789

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/buffer/Buffers.java

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import java.util.concurrent.ConcurrentHashMap;
2020
import java.util.function.BiConsumer;
2121
import java.util.function.Consumer;
22-
import java.util.function.Function;
2322
import java.util.function.Predicate;
2423

2524
/**
@@ -34,36 +33,26 @@ abstract class Buffers<B extends Buffer<?>> {
3433
private final ConcurrentHashMap<String, B> buffers = new ConcurrentHashMap<>();
3534

3635
public void forEach(final Predicate<String> predicate,
37-
final BiConsumer<String, B> consumer) {
38-
this.buffers.forEach(new BiConsumer<String, B>() {
39-
40-
@Override
41-
public void accept(String name, B value) {
42-
if (predicate.test(name)) {
43-
consumer.accept(name, value);
44-
}
36+
BiConsumer<String, B> consumer) {
37+
this.buffers.forEach((name, value) -> {
38+
if (predicate.test(name)) {
39+
consumer.accept(name, value);
4540
}
46-
4741
});
4842
}
4943

50-
public B find(final String name) {
44+
public B find(String name) {
5145
return this.buffers.get(name);
5246
}
5347

5448
public int count() {
5549
return this.buffers.size();
5650
}
5751

58-
protected final void doWith(final String name, final Consumer<B> consumer) {
52+
protected final void doWith(String name, Consumer<B> consumer) {
5953
B buffer = this.buffers.get(name);
6054
if (buffer == null) {
61-
buffer = this.buffers.computeIfAbsent(name, new Function<String, B>() {
62-
@Override
63-
public B apply(String name) {
64-
return createBuffer();
65-
}
66-
});
55+
buffer = this.buffers.computeIfAbsent(name, (k) -> createBuffer());
6756
}
6857
consumer.accept(buffer);
6958
}

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/buffer/CounterBuffers.java

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2016 the original author or authors.
2+
* Copyright 2012-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,8 +16,6 @@
1616

1717
package org.springframework.boot.actuate.metrics.buffer;
1818

19-
import java.util.function.Consumer;
20-
2119
/**
2220
* Fast writes to in-memory metrics store using {@link CounterBuffer}.
2321
*
@@ -26,27 +24,17 @@
2624
*/
2725
public class CounterBuffers extends Buffers<CounterBuffer> {
2826

29-
public void increment(final String name, final long delta) {
30-
doWith(name, new Consumer<CounterBuffer>() {
31-
32-
@Override
33-
public void accept(CounterBuffer buffer) {
34-
buffer.setTimestamp(System.currentTimeMillis());
35-
buffer.add(delta);
36-
}
37-
27+
public void increment(String name, long delta) {
28+
doWith(name, (buffer) -> {
29+
buffer.setTimestamp(System.currentTimeMillis());
30+
buffer.add(delta);
3831
});
3932
}
4033

41-
public void reset(final String name) {
42-
doWith(name, new Consumer<CounterBuffer>() {
43-
44-
@Override
45-
public void accept(CounterBuffer buffer) {
46-
buffer.setTimestamp(System.currentTimeMillis());
47-
buffer.reset();
48-
}
49-
34+
public void reset(String name) {
35+
doWith(name, (buffer) -> {
36+
buffer.setTimestamp(System.currentTimeMillis());
37+
buffer.reset();
5038
});
5139
}
5240

0 commit comments

Comments
 (0)