#649 - Use Java 8 Map enhancements.

Related ticket: #642.
This commit is contained in:
Kulcsár Roland
2017-10-14 18:55:27 +02:00
committed by Oliver Gierke
parent 160afea34d
commit 7570b003d1
7 changed files with 46 additions and 102 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013-2014 the original author or authors.
* Copyright 2013-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -29,7 +29,7 @@ import org.springframework.hateoas.RelProvider;
@Order(100)
public class AnnotationRelProvider implements RelProvider {
private final Map<Class<?>, Relation> annotationCache = new HashMap<Class<?>, Relation>();
private final Map<Class<?>, Relation> annotationCache = new HashMap<>();
/*
* (non-Javadoc)
@@ -73,19 +73,6 @@ public class AnnotationRelProvider implements RelProvider {
}
private Relation lookupAnnotation(Class<?> type) {
Relation relation = annotationCache.get(type);
if (relation != null) {
return relation;
}
relation = AnnotationUtils.getAnnotation(type, Relation.class);
if (relation != null) {
annotationCache.put(type, relation);
}
return relation;
return annotationCache.computeIfAbsent(type, key -> AnnotationUtils.getAnnotation(key, Relation.class));
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2016 the original author or authors.
* Copyright 2012-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -44,7 +44,7 @@ import org.springframework.util.ReflectionUtils;
public class DummyInvocationUtils {
private static final ObjenesisStd OBJENESIS = new ObjenesisStd();
private static final Map<Class<?>, Class<?>> CLASS_CACHE = new ConcurrentReferenceHashMap<Class<?>, Class<?>>(16,
private static final Map<Class<?>, Class<?>> CLASS_CACHE = new ConcurrentReferenceHashMap<>(16,
ReferenceType.WEAK);
public interface LastInvocationAware {
@@ -206,23 +206,16 @@ public class DummyInvocationUtils {
Assert.notNull(type, "Source type must not be null!");
Assert.notNull(classLoader, "ClassLoader must not be null!");
Class<?> result = CLASS_CACHE.get(type);
return CLASS_CACHE.computeIfAbsent(type, key -> {
if (result != null) {
return result;
}
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(key);
enhancer.setInterfaces(new Class<?>[] { LastInvocationAware.class });
enhancer.setCallbackType(org.springframework.cglib.proxy.MethodInterceptor.class);
enhancer.setClassLoader(classLoader);
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(type);
enhancer.setInterfaces(new Class<?>[] { LastInvocationAware.class });
enhancer.setCallbackType(org.springframework.cglib.proxy.MethodInterceptor.class);
enhancer.setClassLoader(classLoader);
result = enhancer.createClass();
CLASS_CACHE.put(type, result);
return result;
return enhancer.createClass();
});
}
@Value

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2016 the original author or authors.
* Copyright 2012-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -38,7 +38,7 @@ public class MethodParameters {
private static ParameterNameDiscoverer DISCOVERER = new DefaultParameterNameDiscoverer();
private final List<MethodParameter> parameters;
private final Map<Class<?>, List<MethodParameter>> parametersWithAnnotationCache = new ConcurrentReferenceHashMap<Class<?>, List<MethodParameter>>();
private final Map<Class<?>, List<MethodParameter>> parametersWithAnnotationCache = new ConcurrentReferenceHashMap<>();
/**
* Creates a new {@link MethodParameters} from the given {@link Method}.
@@ -59,7 +59,7 @@ public class MethodParameters {
public MethodParameters(Method method, AnnotationAttribute namingAnnotation) {
Assert.notNull(method, "Method must not be null!");
this.parameters = new ArrayList<MethodParameter>();
this.parameters = new ArrayList<>();
for (int i = 0; i < method.getParameterTypes().length; i++) {
@@ -107,7 +107,7 @@ public class MethodParameters {
public List<MethodParameter> getParametersOfType(Class<?> type) {
Assert.notNull(type, "Type must not be null!");
List<MethodParameter> result = new ArrayList<MethodParameter>();
List<MethodParameter> result = new ArrayList<>();
for (MethodParameter parameter : getParameters()) {
if (parameter.getParameterType().equals(type)) {
@@ -126,24 +126,19 @@ public class MethodParameters {
*/
public List<MethodParameter> getParametersWith(Class<? extends Annotation> annotation) {
List<MethodParameter> cached = parametersWithAnnotationCache.get(annotation);
return parametersWithAnnotationCache.computeIfAbsent(annotation, key -> {
if (cached != null) {
return cached;
}
Assert.notNull(annotation, "Annotation must not be null!");
List<MethodParameter> result = new ArrayList<>();
Assert.notNull(annotation, "Annotation must not be null!");
List<MethodParameter> result = new ArrayList<MethodParameter>();
for (MethodParameter parameter : getParameters()) {
if (parameter.hasParameterAnnotation(annotation)) {
result.add(parameter);
for (MethodParameter parameter : getParameters()) {
if (parameter.hasParameterAnnotation(annotation)) {
result.add(parameter);
}
}
}
parametersWithAnnotationCache.put(annotation, result);
return result;
return result;
});
}
/**

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013-2016 the original author or authors.
* Copyright 2013-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -20,7 +20,6 @@ import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.springframework.hateoas.IanaRels;
import org.springframework.hateoas.Link;
@@ -78,16 +77,13 @@ public class DefaultCurieProvider implements CurieProvider {
Assert.notNull(curies, "Curies must not be null!");
for (Entry<String, UriTemplate> entry : curies.entrySet()) {
String name = entry.getKey();
UriTemplate template = entry.getValue();
curies.forEach((name, template) -> {
Assert.hasText(name, "Curie name must not be null or empty!");
Assert.notNull(template, "UriTemplate must not be null!");
Assert.isTrue(template.getVariableNames().size() == 1,
String.format("Expected a single template variable in the UriTemplate %s!", template.toString()));
}
});
this.defaultCurie = StringUtils.hasText(defaultCurieName) ? defaultCurieName
: curies.size() == 1 ? curies.keySet().iterator().next() : null;
@@ -101,15 +97,9 @@ public class DefaultCurieProvider implements CurieProvider {
@Override
public Collection<? extends Object> getCurieInformation(Links links) {
List<Curie> result = new ArrayList<Curie>(curies.size());
List<Curie> result = new ArrayList<>(curies.size());
for (Entry<String, UriTemplate> source : curies.entrySet()) {
String name = source.getKey();
UriTemplate template = source.getValue();
result.add(new Curie(name, getCurieHref(name, template)));
}
curies.forEach((name, template) -> result.add(new Curie(name, getCurieHref(name, template))));
return Collections.unmodifiableCollection(result);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2016 the original author or authors.
* Copyright 2012-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -152,8 +152,8 @@ public class Jackson2HalModule extends SimpleModule {
throws IOException, JsonGenerationException {
// sort links according to their relation
Map<String, List<Object>> sortedLinks = new LinkedHashMap<String, List<Object>>();
List<Link> links = new ArrayList<Link>();
Map<String, List<Object>> sortedLinks = new LinkedHashMap<>();
List<Link> links = new ArrayList<>();
boolean prefixingRequired = curieProvider != null;
boolean curiedLinkPresent = false;
@@ -179,18 +179,14 @@ public class Jackson2HalModule extends SimpleModule {
curiedLinkPresent = true;
}
if (sortedLinks.get(rel) == null) {
sortedLinks.put(rel, new ArrayList<Object>());
}
sortedLinks.computeIfAbsent(rel, key -> new ArrayList<>()).add(toHalLink(link));
links.add(link);
sortedLinks.get(rel).add(toHalLink(link));
}
if (!skipCuries && prefixingRequired && curiedLinkPresent) {
ArrayList<Object> curies = new ArrayList<Object>();
ArrayList<Object> curies = new ArrayList<>();
curies.add(curieProvider.getCurieInformation(new Links(links)));
sortedLinks.put("curies", curies);
@@ -414,7 +410,7 @@ public class Jackson2HalModule extends SimpleModule {
super(TypeFactory.defaultInstance().constructType(List.class));
this.property = property;
this.serializers = new HashMap<Class<?>, JsonSerializer<Object>>();
this.serializers = new HashMap<>();
this.halConfiguration = halConfiguration;
}
@@ -559,7 +555,7 @@ public class Jackson2HalModule extends SimpleModule {
public List<Link> deserialize(JsonParser jp, DeserializationContext ctxt)
throws IOException, JsonProcessingException {
List<Link> result = new ArrayList<Link>();
List<Link> result = new ArrayList<>();
String relation;
Link link;
@@ -635,7 +631,7 @@ public class Jackson2HalModule extends SimpleModule {
public List<Object> deserialize(JsonParser jp, DeserializationContext ctxt)
throws IOException, JsonProcessingException {
List<Object> result = new ArrayList<Object>();
List<Object> result = new ArrayList<>();
JsonDeserializer<Object> deser = ctxt.findRootValueDeserializer(contentType);
Object object;
@@ -677,7 +673,7 @@ public class Jackson2HalModule extends SimpleModule {
*/
public static class HalHandlerInstantiator extends HandlerInstantiator {
private final Map<Class<?>, Object> serializers = new HashMap<Class<?>, Object>();
private final Map<Class<?>, Object> serializers = new HashMap<>();
private final AutowireCapableBeanFactory delegate;
/**

View File

@@ -45,7 +45,7 @@ import org.springframework.web.util.UriTemplate;
@RequiredArgsConstructor
class AnnotatedParametersParameterAccessor {
private static final Map<Method, MethodParameters> METHOD_PARAMETERS_CACHE = new ConcurrentReferenceHashMap<Method, MethodParameters>(
private static final Map<Method, MethodParameters> METHOD_PARAMETERS_CACHE = new ConcurrentReferenceHashMap<>(
16, ReferenceType.WEAK);
private final @NonNull AnnotationAttribute attribute;
@@ -62,7 +62,7 @@ class AnnotatedParametersParameterAccessor {
MethodParameters parameters = getOrCreateMethodParametersFor(invocation.getMethod());
Object[] arguments = invocation.getArguments();
List<BoundMethodParameter> result = new ArrayList<BoundMethodParameter>();
List<BoundMethodParameter> result = new ArrayList<>();
for (MethodParameter parameter : parameters.getParametersWith(attribute.getAnnotationType())) {
@@ -110,17 +110,7 @@ class AnnotatedParametersParameterAccessor {
* @return
*/
private static MethodParameters getOrCreateMethodParametersFor(Method method) {
MethodParameters methodParameters = METHOD_PARAMETERS_CACHE.get(method);
if (methodParameters != null) {
return methodParameters;
}
methodParameters = new MethodParameters(method);
METHOD_PARAMETERS_CACHE.put(method, methodParameters);
return methodParameters;
return METHOD_PARAMETERS_CACHE.computeIfAbsent(method, MethodParameters::new);
}
/**

View File

@@ -305,20 +305,13 @@ public class ControllerLinkBuilder extends LinkBuilderSupport<ControllerLinkBuil
private static class CachingAnnotationMappingDiscoverer implements MappingDiscoverer {
private final @Delegate AnnotationMappingDiscoverer delegate;
private final Map<String, UriTemplate> templates = new ConcurrentReferenceHashMap<String, UriTemplate>();
private final Map<String, UriTemplate> templates = new ConcurrentReferenceHashMap<>();
public UriTemplate getMappingAsUriTemplate(Class<?> type, Method method) {
String mapping = delegate.getMapping(type, method);
UriTemplate template = templates.get(mapping);
if (template == null) {
template = new UriTemplate(mapping);
templates.put(mapping, template);
}
return template;
return templates.computeIfAbsent(mapping, UriTemplate::new);
}
}