DATACMNS-867 - Additional Java 8 language feature cleanup.
Make use of lambdas and method references though out the codebase. Remove no longer required generic type parameters. Additionally remove unused imports and replace single element list initialization with dedicated singletonList. Use Assertion overloads taking Supplier for dynamic assertion error messages.
This commit is contained in:
committed by
Oliver Gierke
parent
aa4c51e1ea
commit
be347eaaab
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2015 the original author or authors.
|
||||
* Copyright 2014-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.
|
||||
@@ -34,6 +34,7 @@ import org.springframework.util.ClassUtils;
|
||||
* Scanner to find types with annotations on the classpath.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Christoph Strobl
|
||||
*/
|
||||
public class AnnotatedTypeScanner implements ResourceLoaderAware, EnvironmentAware {
|
||||
|
||||
@@ -104,7 +105,7 @@ public class AnnotatedTypeScanner implements ResourceLoaderAware, EnvironmentAwa
|
||||
provider.addIncludeFilter(new AnnotationTypeFilter(annotationType, true, considerInterfaces));
|
||||
}
|
||||
|
||||
Set<Class<?>> types = new HashSet<Class<?>>();
|
||||
Set<Class<?>> types = new HashSet<>();
|
||||
|
||||
for (String basePackage : basePackages) {
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2011-2014 the original author or authors.
|
||||
* Copyright 2011-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.
|
||||
@@ -39,6 +39,7 @@ import org.springframework.util.ClassUtils;
|
||||
* {@link TypeInformation} for a plain {@link Class}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Christoph Strobl
|
||||
*/
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
public class ClassTypeInformation<S> extends TypeDiscoverer<S> {
|
||||
@@ -54,7 +55,7 @@ public class ClassTypeInformation<S> extends TypeDiscoverer<S> {
|
||||
|
||||
static {
|
||||
for (ClassTypeInformation<?> info : Arrays.asList(COLLECTION, LIST, SET, MAP, OBJECT)) {
|
||||
CACHE.put(info.getType(), new WeakReference<ClassTypeInformation<?>>(info));
|
||||
CACHE.put(info.getType(), new WeakReference<>(info));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -79,7 +80,7 @@ public class ClassTypeInformation<S> extends TypeDiscoverer<S> {
|
||||
}
|
||||
|
||||
ClassTypeInformation<S> result = new ClassTypeInformation<>(type);
|
||||
CACHE.put(type, new WeakReference<ClassTypeInformation<?>>(result));
|
||||
CACHE.put(type, new WeakReference<>(result));
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -126,7 +127,7 @@ public class ClassTypeInformation<S> extends TypeDiscoverer<S> {
|
||||
}
|
||||
|
||||
Map<TypeVariable, Type> source = GenericTypeResolver.getTypeVariableMap(type);
|
||||
Map<TypeVariable<?>, Type> map = new HashMap<TypeVariable<?>, Type>(source.size());
|
||||
Map<TypeVariable<?>, Type> map = new HashMap<>(source.size());
|
||||
|
||||
for (Entry<TypeVariable, Type> entry : source.entrySet()) {
|
||||
|
||||
|
||||
@@ -32,6 +32,7 @@ import org.springframework.util.Assert;
|
||||
* Utility methods to work with {@link Optional}s.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Christoph Strobl
|
||||
*/
|
||||
@UtilityClass
|
||||
public class Optionals {
|
||||
@@ -62,7 +63,7 @@ public class Optionals {
|
||||
|
||||
Assert.notNull(optionals, "Optional must not be null!");
|
||||
|
||||
return Arrays.asList(optionals).stream().flatMap(it -> it.map(Stream::of).orElseGet(() -> Stream.empty()));
|
||||
return Arrays.asList(optionals).stream().flatMap(it -> it.map(Stream::of).orElseGet(Stream::empty));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -78,9 +79,9 @@ public class Optionals {
|
||||
Assert.notNull(function, "Function must not be null!");
|
||||
|
||||
return Streamable.of(source).stream()//
|
||||
.map(it -> function.apply(it))//
|
||||
.filter(it -> it.isPresent())//
|
||||
.findFirst().orElseGet(() -> Optional.empty());
|
||||
.map(function::apply)//
|
||||
.filter(Optional::isPresent)//
|
||||
.findFirst().orElseGet(Optional::empty);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -96,7 +97,7 @@ public class Optionals {
|
||||
Assert.notNull(function, "Function must not be null!");
|
||||
|
||||
return Streamable.of(source).stream()//
|
||||
.map(it -> function.apply(it))//
|
||||
.map(function::apply)//
|
||||
.filter(it -> !it.equals(defaultValue))//
|
||||
.findFirst().orElse(defaultValue);
|
||||
}
|
||||
@@ -126,8 +127,8 @@ public class Optionals {
|
||||
Assert.notNull(suppliers, "Suppliers must not be null!");
|
||||
|
||||
return Streamable.of(suppliers).stream()//
|
||||
.map(it -> it.get())//
|
||||
.filter(it -> it.isPresent())//
|
||||
.map(Supplier::get)//
|
||||
.filter(Optional::isPresent)//
|
||||
.findFirst().orElse(Optional.empty());
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2015-2016 the original author or authors.
|
||||
* Copyright 2015-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.
|
||||
@@ -30,6 +30,7 @@ import java.util.stream.Collectors;
|
||||
*
|
||||
* @author Tobias Trelle
|
||||
* @author Oliver Gierke
|
||||
* @author Christoph Strobl
|
||||
* @param <S> Type of the first thing.
|
||||
* @param <T> Type of the second thing.
|
||||
* @since 1.12
|
||||
@@ -50,7 +51,7 @@ public final class Pair<S, T> {
|
||||
* @return
|
||||
*/
|
||||
public static <S, T> Pair<S, T> of(S first, T second) {
|
||||
return new Pair<S, T>(first, second);
|
||||
return new Pair<>(first, second);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2011-2016 the original author or authors.
|
||||
* Copyright 2011-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.
|
||||
@@ -34,6 +34,7 @@ import org.springframework.util.StringUtils;
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Mark Paluch
|
||||
* @author Christoph Strobl
|
||||
*/
|
||||
class ParameterizedTypeInformation<T> extends ParentTypeAwareTypeInformation<T> {
|
||||
|
||||
@@ -72,7 +73,7 @@ class ParameterizedTypeInformation<T> extends ParentTypeAwareTypeInformation<T>
|
||||
Class<?> rawType = getType();
|
||||
|
||||
Set<Type> supertypes = new HashSet<>();
|
||||
Optional.ofNullable(rawType.getGenericSuperclass()).ifPresent(it -> supertypes.add(it));
|
||||
Optional.ofNullable(rawType.getGenericSuperclass()).ifPresent(supertypes::add);
|
||||
supertypes.addAll(Arrays.asList(rawType.getGenericInterfaces()));
|
||||
|
||||
Optional<TypeInformation<?>> result = supertypes.stream()//
|
||||
@@ -95,7 +96,7 @@ class ParameterizedTypeInformation<T> extends ParentTypeAwareTypeInformation<T>
|
||||
@Override
|
||||
public List<TypeInformation<?>> getTypeArguments() {
|
||||
|
||||
List<TypeInformation<?>> result = new ArrayList<TypeInformation<?>>();
|
||||
List<TypeInformation<?>> result = new ArrayList<>();
|
||||
|
||||
for (Type argument : type.getActualTypeArguments()) {
|
||||
result.add(createInfo(argument));
|
||||
|
||||
@@ -52,7 +52,7 @@ public abstract class ParentTypeAwareTypeInformation<S> extends TypeDiscoverer<S
|
||||
*/
|
||||
private static Map<TypeVariable<?>, Type> mergeMaps(TypeDiscoverer<?> parent, Map<TypeVariable<?>, Type> map) {
|
||||
|
||||
Map<TypeVariable<?>, Type> typeVariableMap = new HashMap<TypeVariable<?>, Type>();
|
||||
Map<TypeVariable<?>, Type> typeVariableMap = new HashMap<>();
|
||||
typeVariableMap.putAll(map);
|
||||
typeVariableMap.putAll(parent.getTypeVariableMap());
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2015 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.
|
||||
@@ -42,6 +42,7 @@ import org.springframework.util.ReflectionUtils.FieldFilter;
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Thomas Darimont
|
||||
* @author Christoph Strobl
|
||||
* @since 1.5
|
||||
*/
|
||||
public abstract class ReflectionUtils {
|
||||
@@ -277,7 +278,7 @@ public abstract class ReflectionUtils {
|
||||
|
||||
public static Optional<Method> getMethod(Class<?> type, String name, ResolvableType... parameterTypes) {
|
||||
|
||||
List<Class<?>> collect = Arrays.stream(parameterTypes).map(it -> it.getRawClass()).collect(Collectors.toList());
|
||||
List<Class<?>> collect = Arrays.stream(parameterTypes).map(ResolvableType::getRawClass).collect(Collectors.toList());
|
||||
|
||||
Optional<Method> method = Optional.ofNullable(
|
||||
org.springframework.util.ReflectionUtils.findMethod(type, name, collect.toArray(new Class<?>[collect.size()])));
|
||||
@@ -286,7 +287,7 @@ public abstract class ReflectionUtils {
|
||||
.allMatch(index -> ResolvableType.forMethodParameter(it, index).equals(parameterTypes[index])));
|
||||
}
|
||||
|
||||
private static final boolean argumentsMatch(Class<?>[] parameterTypes, Object[] arguments) {
|
||||
private static boolean argumentsMatch(Class<?>[] parameterTypes, Object[] arguments) {
|
||||
|
||||
if (parameterTypes.length != arguments.length) {
|
||||
return false;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2016 the original author or authors.
|
||||
* Copyright 2016-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.
|
||||
@@ -26,6 +26,7 @@ import org.springframework.util.Assert;
|
||||
* Simple interface to ease streamability of {@link Iterable}s.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Christoph Strobl
|
||||
*/
|
||||
public interface Streamable<T> extends Iterable<T> {
|
||||
|
||||
@@ -43,8 +44,8 @@ public interface Streamable<T> extends Iterable<T> {
|
||||
*
|
||||
* @return will never be {@literal null}.
|
||||
*/
|
||||
public static <T> Streamable<T> empty() {
|
||||
return () -> Collections.emptyIterator();
|
||||
static <T> Streamable<T> empty() {
|
||||
return Collections::emptyIterator;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -54,7 +55,7 @@ public interface Streamable<T> extends Iterable<T> {
|
||||
* @return
|
||||
*/
|
||||
@SafeVarargs
|
||||
public static <T> Streamable<T> of(T... t) {
|
||||
static <T> Streamable<T> of(T... t) {
|
||||
return () -> Arrays.asList(t).iterator();
|
||||
}
|
||||
|
||||
@@ -64,10 +65,10 @@ public interface Streamable<T> extends Iterable<T> {
|
||||
* @param iterable must not be {@literal null}.
|
||||
* @return
|
||||
*/
|
||||
public static <T> Streamable<T> of(Iterable<T> iterable) {
|
||||
static <T> Streamable<T> of(Iterable<T> iterable) {
|
||||
|
||||
Assert.notNull(iterable, "Iterable must not be null!");
|
||||
|
||||
return () -> iterable.iterator();
|
||||
return iterable::iterator;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2011-2015 the original author or authors.
|
||||
* Copyright 2011-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.
|
||||
@@ -51,6 +51,7 @@ import org.springframework.util.ReflectionUtils;
|
||||
* Basic {@link TypeDiscoverer} that contains basic functionality to discover property types.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Christoph Strobl
|
||||
*/
|
||||
class TypeDiscoverer<S> implements TypeInformation<S> {
|
||||
|
||||
@@ -60,7 +61,7 @@ class TypeDiscoverer<S> implements TypeInformation<S> {
|
||||
|
||||
ClassLoader classLoader = TypeDiscoverer.class.getClassLoader();
|
||||
|
||||
Set<Class<?>> mapTypes = new HashSet<Class<?>>();
|
||||
Set<Class<?>> mapTypes = new HashSet<>();
|
||||
mapTypes.add(Map.class);
|
||||
|
||||
try {
|
||||
@@ -92,8 +93,8 @@ class TypeDiscoverer<S> implements TypeInformation<S> {
|
||||
|
||||
this.type = type;
|
||||
this.resolvedType = Lazy.of(() -> resolveType(type));
|
||||
this.componentType = Lazy.of(() -> doGetComponentType());
|
||||
this.valueType = Lazy.of(() -> doGetMapValueType());
|
||||
this.componentType = Lazy.of(this::doGetComponentType);
|
||||
this.valueType = Lazy.of(this::doGetMapValueType);
|
||||
this.typeVariableMap = typeVariableMap;
|
||||
this.hashCode = 17 + (31 * type.hashCode()) + (31 * typeVariableMap.hashCode());
|
||||
}
|
||||
@@ -108,7 +109,7 @@ class TypeDiscoverer<S> implements TypeInformation<S> {
|
||||
}
|
||||
|
||||
private TypeInformation<?> createInfo(Optional<Type> fieldType) {
|
||||
return fieldType.map(it -> createInfo(it)).orElseThrow(() -> new IllegalArgumentException());
|
||||
return fieldType.map(this::createInfo).orElseThrow(IllegalArgumentException::new);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -129,7 +130,7 @@ class TypeDiscoverer<S> implements TypeInformation<S> {
|
||||
}
|
||||
|
||||
Class<S> resolveType = resolveType(fieldType);
|
||||
Map<TypeVariable, Type> variableMap = new HashMap<TypeVariable, Type>();
|
||||
Map<TypeVariable, Type> variableMap = new HashMap<>();
|
||||
variableMap.putAll(GenericTypeResolver.getTypeVariableMap(resolveType));
|
||||
|
||||
if (fieldType instanceof ParameterizedType) {
|
||||
@@ -183,7 +184,7 @@ class TypeDiscoverer<S> implements TypeInformation<S> {
|
||||
@SuppressWarnings({ "unchecked", "rawtypes", "deprecation" })
|
||||
protected Class<S> resolveType(Type type) {
|
||||
|
||||
Map<TypeVariable, Type> map = new HashMap<TypeVariable, Type>();
|
||||
Map<TypeVariable, Type> map = new HashMap<>();
|
||||
map.putAll(getTypeVariableMap());
|
||||
|
||||
return (Class<S>) GenericTypeResolver.resolveType(type, map);
|
||||
@@ -198,7 +199,7 @@ class TypeDiscoverer<S> implements TypeInformation<S> {
|
||||
Assert.notNull(constructor, "Constructor must not be null!");
|
||||
|
||||
Type[] types = constructor.getGenericParameterTypes();
|
||||
List<TypeInformation<?>> result = new ArrayList<TypeInformation<?>>(types.length);
|
||||
List<TypeInformation<?>> result = new ArrayList<>(types.length);
|
||||
|
||||
for (Type parameterType : types) {
|
||||
result.add(createInfo(parameterType));
|
||||
@@ -216,13 +217,13 @@ class TypeDiscoverer<S> implements TypeInformation<S> {
|
||||
int separatorIndex = fieldname.indexOf('.');
|
||||
|
||||
if (separatorIndex == -1) {
|
||||
return fieldTypes.computeIfAbsent(fieldname, it -> getPropertyInformation(it));
|
||||
return fieldTypes.computeIfAbsent(fieldname, this::getPropertyInformation);
|
||||
}
|
||||
|
||||
String head = fieldname.substring(0, separatorIndex);
|
||||
Optional<TypeInformation<?>> info = getProperty(head);
|
||||
|
||||
return info.map(it -> it.getProperty(fieldname.substring(separatorIndex + 1))).orElseGet(() -> Optional.empty());
|
||||
return info.map(it -> it.getProperty(fieldname.substring(separatorIndex + 1))).orElseGet(Optional::empty);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -261,7 +262,7 @@ class TypeDiscoverer<S> implements TypeInformation<S> {
|
||||
return Optional.of(descriptor);
|
||||
}
|
||||
|
||||
List<Class<?>> superTypes = new ArrayList<Class<?>>();
|
||||
List<Class<?>> superTypes = new ArrayList<>();
|
||||
superTypes.addAll(Arrays.asList(type.getInterfaces()));
|
||||
superTypes.add(type.getSuperclass());
|
||||
|
||||
@@ -415,7 +416,7 @@ class TypeDiscoverer<S> implements TypeInformation<S> {
|
||||
Assert.notNull(method, "Method most not be null!");
|
||||
|
||||
return Streamable.of(method.getGenericParameterTypes()).stream()//
|
||||
.map(it -> createInfo(it))//
|
||||
.map(this::createInfo)//
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user