diff --git a/src/main/java/org/springframework/hateoas/core/AnnotationRelProvider.java b/src/main/java/org/springframework/hateoas/core/AnnotationRelProvider.java index e3f29846..dbb96db1 100644 --- a/src/main/java/org/springframework/hateoas/core/AnnotationRelProvider.java +++ b/src/main/java/org/springframework/hateoas/core/AnnotationRelProvider.java @@ -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, Relation> annotationCache = new HashMap, Relation>(); + private final Map, 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)); } } diff --git a/src/main/java/org/springframework/hateoas/core/DummyInvocationUtils.java b/src/main/java/org/springframework/hateoas/core/DummyInvocationUtils.java index 7a899579..ef9693a5 100644 --- a/src/main/java/org/springframework/hateoas/core/DummyInvocationUtils.java +++ b/src/main/java/org/springframework/hateoas/core/DummyInvocationUtils.java @@ -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_CACHE = new ConcurrentReferenceHashMap, Class>(16, + private static final Map, 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 diff --git a/src/main/java/org/springframework/hateoas/core/MethodParameters.java b/src/main/java/org/springframework/hateoas/core/MethodParameters.java index cd9e0f69..b130c408 100644 --- a/src/main/java/org/springframework/hateoas/core/MethodParameters.java +++ b/src/main/java/org/springframework/hateoas/core/MethodParameters.java @@ -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 parameters; - private final Map, List> parametersWithAnnotationCache = new ConcurrentReferenceHashMap, List>(); + private final Map, List> 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(); + this.parameters = new ArrayList<>(); for (int i = 0; i < method.getParameterTypes().length; i++) { @@ -107,7 +107,7 @@ public class MethodParameters { public List getParametersOfType(Class type) { Assert.notNull(type, "Type must not be null!"); - List result = new ArrayList(); + List result = new ArrayList<>(); for (MethodParameter parameter : getParameters()) { if (parameter.getParameterType().equals(type)) { @@ -126,24 +126,19 @@ public class MethodParameters { */ public List getParametersWith(Class annotation) { - List cached = parametersWithAnnotationCache.get(annotation); + return parametersWithAnnotationCache.computeIfAbsent(annotation, key -> { - if (cached != null) { - return cached; - } + Assert.notNull(annotation, "Annotation must not be null!"); + List result = new ArrayList<>(); - Assert.notNull(annotation, "Annotation must not be null!"); - List result = new ArrayList(); - - 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; + }); } /** diff --git a/src/main/java/org/springframework/hateoas/hal/DefaultCurieProvider.java b/src/main/java/org/springframework/hateoas/hal/DefaultCurieProvider.java index aedf6f7a..99c26d78 100644 --- a/src/main/java/org/springframework/hateoas/hal/DefaultCurieProvider.java +++ b/src/main/java/org/springframework/hateoas/hal/DefaultCurieProvider.java @@ -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 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 getCurieInformation(Links links) { - List result = new ArrayList(curies.size()); + List result = new ArrayList<>(curies.size()); - for (Entry 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); } diff --git a/src/main/java/org/springframework/hateoas/hal/Jackson2HalModule.java b/src/main/java/org/springframework/hateoas/hal/Jackson2HalModule.java index f15643f2..4ee37b8b 100644 --- a/src/main/java/org/springframework/hateoas/hal/Jackson2HalModule.java +++ b/src/main/java/org/springframework/hateoas/hal/Jackson2HalModule.java @@ -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> sortedLinks = new LinkedHashMap>(); - List links = new ArrayList(); + Map> sortedLinks = new LinkedHashMap<>(); + List 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()); - } + sortedLinks.computeIfAbsent(rel, key -> new ArrayList<>()).add(toHalLink(link)); links.add(link); - - sortedLinks.get(rel).add(toHalLink(link)); } if (!skipCuries && prefixingRequired && curiedLinkPresent) { - ArrayList curies = new ArrayList(); + ArrayList 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, JsonSerializer>(); + this.serializers = new HashMap<>(); this.halConfiguration = halConfiguration; } @@ -559,7 +555,7 @@ public class Jackson2HalModule extends SimpleModule { public List deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { - List result = new ArrayList(); + List result = new ArrayList<>(); String relation; Link link; @@ -635,7 +631,7 @@ public class Jackson2HalModule extends SimpleModule { public List deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { - List result = new ArrayList(); + List result = new ArrayList<>(); JsonDeserializer deser = ctxt.findRootValueDeserializer(contentType); Object object; @@ -677,7 +673,7 @@ public class Jackson2HalModule extends SimpleModule { */ public static class HalHandlerInstantiator extends HandlerInstantiator { - private final Map, Object> serializers = new HashMap, Object>(); + private final Map, Object> serializers = new HashMap<>(); private final AutowireCapableBeanFactory delegate; /** diff --git a/src/main/java/org/springframework/hateoas/mvc/AnnotatedParametersParameterAccessor.java b/src/main/java/org/springframework/hateoas/mvc/AnnotatedParametersParameterAccessor.java index 347d71e9..dd21f980 100644 --- a/src/main/java/org/springframework/hateoas/mvc/AnnotatedParametersParameterAccessor.java +++ b/src/main/java/org/springframework/hateoas/mvc/AnnotatedParametersParameterAccessor.java @@ -45,7 +45,7 @@ import org.springframework.web.util.UriTemplate; @RequiredArgsConstructor class AnnotatedParametersParameterAccessor { - private static final Map METHOD_PARAMETERS_CACHE = new ConcurrentReferenceHashMap( + private static final Map 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 result = new ArrayList(); + List 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); } /** diff --git a/src/main/java/org/springframework/hateoas/mvc/ControllerLinkBuilder.java b/src/main/java/org/springframework/hateoas/mvc/ControllerLinkBuilder.java index 58b2fae7..81123bef 100755 --- a/src/main/java/org/springframework/hateoas/mvc/ControllerLinkBuilder.java +++ b/src/main/java/org/springframework/hateoas/mvc/ControllerLinkBuilder.java @@ -305,20 +305,13 @@ public class ControllerLinkBuilder extends LinkBuilderSupport templates = new ConcurrentReferenceHashMap(); + private final Map 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); } }