Skip to content

Refactor and polish with Java 8 idioms #9781

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -175,15 +175,10 @@ public void endpoints() throws Exception {
.perform(get("/application" + endpointPath)
.accept(ActuatorMediaTypes.APPLICATION_ACTUATOR_V2_JSON))
.andExpect(status().isOk()).andDo(document(output))
.andDo(new ResultHandler() {

@Override
public void handle(MvcResult mvcResult) throws Exception {
EndpointDoc endpoint = new EndpointDoc(docs,
endpointPath);
endpoints.add(endpoint);
}

.andDo(mvcResult -> {
EndpointDoc endpoint1 = new EndpointDoc(docs,
endpointPath);
endpoints.add(endpoint1);
});
}
}
Expand All @@ -199,12 +194,8 @@ public void handle(MvcResult mvcResult) throws Exception {
private Collection<? extends MvcEndpoint> getEndpoints() {
List<? extends MvcEndpoint> endpoints = new ArrayList<>(
this.mvcEndpoints.getEndpoints());
Collections.sort(endpoints, new Comparator<MvcEndpoint>() {
@Override
public int compare(MvcEndpoint o1, MvcEndpoint o2) {
return o1.getPath().compareTo(o2.getPath());
}
});
endpoints.sort(
(Comparator.comparing(MvcEndpoint::getPath)));
return endpoints;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,11 @@ static class NoOpCacheStatisticsConfiguration {

@Bean
public CacheStatisticsProvider<Cache> noOpCacheStatisticsProvider() {
return new CacheStatisticsProvider<Cache>() {
@Override
public CacheStatistics getCacheStatistics(CacheManager cacheManager,
Cache cache) {
if (cacheManager instanceof NoOpCacheManager) {
return NO_OP_STATS;
}
return null;
return (cacheManager, cache) -> {
if (cacheManager instanceof NoOpCacheManager) {
return NO_OP_STATS;
}
return null;
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public MetricsEndpoint metricsEndpoint() {
if (this.publicMetrics != null) {
publicMetrics.addAll(this.publicMetrics);
}
Collections.sort(publicMetrics, AnnotationAwareOrderComparator.INSTANCE);
publicMetrics.sort(AnnotationAwareOrderComparator.INSTANCE);
return new MetricsEndpoint(publicMetrics);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,7 @@ public ManagementContextResolver managementContextResolver() {
@Bean
public ManagementServletContext managementServletContext(
final ManagementServerProperties properties) {
return new ManagementServletContext() {

@Override
public String getContextPath() {
return properties.getContextPath();
}

};
return properties::getContextPath;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,7 @@ public class EndpointWebMvcHypermediaManagementContextConfiguration {
@Bean
public ManagementServletContext managementServletContext(
final ManagementServerProperties properties) {
return new ManagementServletContext() {

@Override
public String getContextPath() {
return properties.getContextPath();
}

};
return properties::getContextPath;
}

@ConditionalOnEnabledEndpoint("actuator")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public static class Security {
/**
* Comma-separated list of roles that can access the management endpoint.
*/
private List<String> roles = new ArrayList<String>(
private List<String> roles = new ArrayList<>(
Collections.singletonList("ACTUATOR"));

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,13 @@ protected <T> T getAttribute(ObjectName objectName, String attributeName,
Object attribute = getMBeanServer().getAttribute(objectName, attributeName);
return type.cast(attribute);
}
catch (MBeanException ex) {
catch (MBeanException | ReflectionException ex) {
throw new IllegalStateException(ex);
}
catch (AttributeNotFoundException ex) {
throw new IllegalStateException("Unexpected: MBean with name '" + objectName
+ "' " + "does not expose attribute with name " + attributeName, ex);
}
catch (ReflectionException ex) {
throw new IllegalStateException(ex);
}
catch (InstanceNotFoundException ex) {
logger.warn("Cache statistics are no longer available", ex);
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public PropertyResolver getResolver() {
}

private Map<String, PropertySource<?>> getPropertySourcesAsMap() {
Map<String, PropertySource<?>> map = new LinkedHashMap<String, PropertySource<?>>();
Map<String, PropertySource<?>> map = new LinkedHashMap<>();
for (PropertySource<?> source : getPropertySources()) {
extract("", map, source);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,14 @@ public Map<String, Object> invoke() {
return SHUTDOWN_MESSAGE;
}
finally {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(500L);
}
catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
ShutdownEndpoint.this.context.close();
Thread thread = new Thread(() -> {
try {
Thread.sleep(500L);
}
catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
ShutdownEndpoint.this.context.close();
});
thread.setContextClassLoader(getClass().getClassLoader());
thread.start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class CompositeHealthIndicator implements HealthIndicator {
* @param healthAggregator the health aggregator
*/
public CompositeHealthIndicator(HealthAggregator healthAggregator) {
this(healthAggregator, new LinkedHashMap<String, HealthIndicator>());
this(healthAggregator, new LinkedHashMap<>());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package org.springframework.boot.actuate.health;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
Expand All @@ -26,7 +25,6 @@

import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.jdbc.DatabaseDriver;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.support.DataAccessUtils;
import org.springframework.jdbc.IncorrectResultSetColumnCountException;
import org.springframework.jdbc.core.ConnectionCallback;
Expand Down Expand Up @@ -120,13 +118,8 @@ private void doDataSourceHealthCheck(Health.Builder builder) throws Exception {
}

private String getProduct() {
return this.jdbcTemplate.execute(new ConnectionCallback<String>() {
@Override
public String doInConnection(Connection connection)
throws SQLException, DataAccessException {
return connection.getMetaData().getDatabaseProductName();
}
});
return this.jdbcTemplate.execute(
(ConnectionCallback<String>) connection -> connection.getMetaData().getDatabaseProductName());
}

protected String getValidationQuery(String product) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

Expand Down Expand Up @@ -80,7 +79,7 @@ protected Status aggregateStatus(List<Status> candidates) {
return Status.UNKNOWN;
}
// Sort given Status instances by configured order
Collections.sort(filteredCandidates, new StatusComparator(this.statusOrder));
filteredCandidates.sort(new StatusComparator(this.statusOrder));
return filteredCandidates.get(0);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@

import java.util.Map;

import com.rabbitmq.client.Channel;

import org.springframework.amqp.rabbit.core.ChannelCallback;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.util.Assert;

Expand All @@ -46,13 +43,10 @@ protected void doHealthCheck(Health.Builder builder) throws Exception {
}

private String getVersion() {
return this.rabbitTemplate.execute(new ChannelCallback<String>() {
@Override
public String doInRabbit(Channel channel) throws Exception {
Map<String, Object> serverProperties = channel.getConnection()
.getServerProperties();
return serverProperties.get("version").toString();
}
return this.rabbitTemplate.execute(channel -> {
Map<String, Object> serverProperties = channel.getConnection()
.getServerProperties();
return serverProperties.get("version").toString();
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Predicate;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -81,14 +80,7 @@ private Iterable<Metric<?>> findAll(Predicate<String> predicate) {
private <T extends Number, B extends Buffer<T>> void collectMetrics(
Buffers<B> buffers, Predicate<String> predicate,
final List<Metric<?>> metrics) {
buffers.forEach(predicate, new BiConsumer<String, B>() {

@Override
public void accept(String name, B value) {
metrics.add(asMetric(name, value));
}

});
buffers.forEach(predicate, (name, value) -> metrics.add(asMetric(name, value)));
}

private <T extends Number> Metric<T> asMetric(final String name, Buffer<T> buffer) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;

/**
Expand All @@ -35,15 +34,10 @@ abstract class Buffers<B extends Buffer<?>> {

public void forEach(final Predicate<String> predicate,
final BiConsumer<String, B> consumer) {
this.buffers.forEach(new BiConsumer<String, B>() {

@Override
public void accept(String name, B value) {
if (predicate.test(name)) {
consumer.accept(name, value);
}
this.buffers.forEach((name, value) -> {
if (predicate.test(name)) {
consumer.accept(name, value);
}

});
}

Expand All @@ -58,12 +52,7 @@ public int count() {
protected final void doWith(final String name, final Consumer<B> consumer) {
B buffer = this.buffers.get(name);
if (buffer == null) {
buffer = this.buffers.computeIfAbsent(name, new Function<String, B>() {
@Override
public B apply(String name) {
return createBuffer();
}
});
buffer = this.buffers.computeIfAbsent(name, name1 -> createBuffer());
}
consumer.accept(buffer);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

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

import java.util.function.Consumer;

/**
* Fast writes to in-memory metrics store using {@link CounterBuffer}.
*
Expand All @@ -27,26 +25,16 @@
public class CounterBuffers extends Buffers<CounterBuffer> {

public void increment(final String name, final long delta) {
doWith(name, new Consumer<CounterBuffer>() {

@Override
public void accept(CounterBuffer buffer) {
buffer.setTimestamp(System.currentTimeMillis());
buffer.add(delta);
}

doWith(name, buffer -> {
buffer.setTimestamp(System.currentTimeMillis());
buffer.add(delta);
});
}

public void reset(final String name) {
doWith(name, new Consumer<CounterBuffer>() {

@Override
public void accept(CounterBuffer buffer) {
buffer.setTimestamp(System.currentTimeMillis());
buffer.reset();
}

doWith(name, buffer -> {
buffer.setTimestamp(System.currentTimeMillis());
buffer.reset();
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

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

import java.util.function.Consumer;

/**
* Fast writes to in-memory metrics store using {@link GaugeBuffer}.
*
Expand All @@ -27,12 +25,9 @@
public class GaugeBuffers extends Buffers<GaugeBuffer> {

public void set(final String name, final double value) {
doWith(name, new Consumer<GaugeBuffer>() {
@Override
public void accept(GaugeBuffer buffer) {
buffer.setTimestamp(System.currentTimeMillis());
buffer.setValue(value);
}
doWith(name, buffer -> {
buffer.setTimestamp(System.currentTimeMillis());
buffer.setValue(value);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,7 @@ public interface ReservoirFactory {
/**
* Default empty {@link ReservoirFactory} implementation.
*/
ReservoirFactory NONE = new ReservoirFactory() {

@Override
public Reservoir getReservoir(String name) {
return null;
}

};
ReservoirFactory NONE = name -> null;

/**
* Return the {@link Reservoir} instance to use or {@code null} if a custom reservoir
Expand Down
Loading