DATACMNS-867 - First draft.
This commit is contained in:
@@ -48,6 +48,7 @@ public class AnnotatedTypeScanner implements ResourceLoaderAware, EnvironmentAwa
|
||||
*
|
||||
* @param annotationTypes the annotation types to scan for.
|
||||
*/
|
||||
@SafeVarargs
|
||||
public AnnotatedTypeScanner(Class<? extends Annotation>... annotationTypes) {
|
||||
this(true, annotationTypes);
|
||||
}
|
||||
@@ -58,6 +59,7 @@ public class AnnotatedTypeScanner implements ResourceLoaderAware, EnvironmentAwa
|
||||
* @param considerInterfaces whether to consider interfaces as well.
|
||||
* @param annotationTypes the annotations to scan for.
|
||||
*/
|
||||
@SafeVarargs
|
||||
public AnnotatedTypeScanner(boolean considerInterfaces, Class<? extends Annotation>... annotationTypes) {
|
||||
|
||||
this.annotationTypess = Arrays.asList(annotationTypes);
|
||||
|
||||
51
src/main/java/org/springframework/data/util/Lazy.java
Normal file
51
src/main/java/org/springframework/data/util/Lazy.java
Normal file
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Copyright 2016 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.util;
|
||||
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
@RequiredArgsConstructor
|
||||
@EqualsAndHashCode
|
||||
public class Lazy<T> {
|
||||
|
||||
private final Supplier<T> supplier;
|
||||
private Optional<T> value;
|
||||
|
||||
public static <T> Lazy<T> of(Supplier<T> supplier) {
|
||||
return new Lazy<T>(supplier);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the value created by the configured {@link Supplier}.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public T get() {
|
||||
|
||||
if (value == null) {
|
||||
this.value = Optional.ofNullable(supplier.get());
|
||||
}
|
||||
|
||||
return value.orElse(null);
|
||||
}
|
||||
}
|
||||
65
src/main/java/org/springframework/data/util/Optionals.java
Normal file
65
src/main/java/org/springframework/data/util/Optionals.java
Normal file
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
* Copyright 2016 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.util;
|
||||
|
||||
import lombok.experimental.UtilityClass;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Iterator;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Utility methods to work with {@link Optional}s.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
@UtilityClass
|
||||
public class Optionals {
|
||||
|
||||
/**
|
||||
* Returns whether any of the given {@link Optional}s is present.
|
||||
*
|
||||
* @param optionals must not be {@literal null}.
|
||||
* @return
|
||||
*/
|
||||
public static boolean isAnyPresent(Optional<?>... optionals) {
|
||||
|
||||
Assert.notNull(optionals, "Optionals must not be null!");
|
||||
|
||||
return Arrays.stream(optionals).anyMatch(Optional::isPresent);
|
||||
}
|
||||
|
||||
/**
|
||||
* Turns the given {@link Optional} into a one-element {@link Stream} or an empty one if not present.
|
||||
*
|
||||
* @param optionals must not be {@literal null}.
|
||||
* @return
|
||||
*/
|
||||
@SafeVarargs
|
||||
public static <T> Stream<T> toStream(Optional<? extends T>... optionals) {
|
||||
|
||||
Assert.notNull(optionals, "Optional must not be null!");
|
||||
|
||||
return Arrays.asList(optionals).stream().flatMap(it -> it.map(Stream::of).orElse(Stream.empty()));
|
||||
}
|
||||
|
||||
public static <T> Optional<T> next(Iterator<T> iterator) {
|
||||
return iterator.hasNext() ? Optional.of(iterator.next()) : Optional.empty();
|
||||
}
|
||||
}
|
||||
@@ -21,6 +21,10 @@ import lombok.NonNull;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.ToString;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collector;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* A tuple of things.
|
||||
*
|
||||
@@ -66,4 +70,8 @@ public final class Pair<S, T> {
|
||||
public T getSecond() {
|
||||
return second;
|
||||
}
|
||||
|
||||
public static <S, T> Collector<Pair<S, T>, ?, Map<S, T>> toMap() {
|
||||
return Collectors.toMap(Pair::getFirst, Pair::getSecond);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,12 +15,16 @@
|
||||
*/
|
||||
package org.springframework.data.util;
|
||||
|
||||
import lombok.NonNull;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.Arrays;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.springframework.beans.BeanUtils;
|
||||
@@ -105,18 +109,10 @@ public abstract class ReflectionUtils {
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
@RequiredArgsConstructor
|
||||
public static class AnnotationFieldFilter implements DescribedFieldFilter {
|
||||
|
||||
private final Class<? extends Annotation> annotationType;
|
||||
|
||||
/**
|
||||
* Creates a new {@link AnnotationFieldFilter} for the given annotation type.
|
||||
*/
|
||||
public AnnotationFieldFilter(Class<? extends Annotation> annotationType) {
|
||||
|
||||
Assert.notNull(annotationType, "Annotation type must not be null!");
|
||||
this.annotationType = annotationType;
|
||||
}
|
||||
private final @NonNull Class<? extends Annotation> annotationType;
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
@@ -242,27 +238,20 @@ public abstract class ReflectionUtils {
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds a constructoron the given type that matches the given constructor arguments.
|
||||
* Finds a constructor on the given type that matches the given constructor arguments.
|
||||
*
|
||||
* @param type must not be {@literal null}.
|
||||
* @param constructorArguments must not be {@literal null}.
|
||||
* @return a {@link Constructor} that is compatible with the given arguments or {@literal null} if none found.
|
||||
* @return a {@link Constructor} that is compatible with the given arguments.
|
||||
*/
|
||||
public static Constructor<?> findConstructor(Class<?> type, Object... constructorArguments) {
|
||||
public static Optional<Constructor<?>> findConstructor(Class<?> type, Object... constructorArguments) {
|
||||
|
||||
Assert.notNull(type, "Target type must not be null!");
|
||||
Assert.notNull(constructorArguments, "Constructor arguments must not be null!");
|
||||
|
||||
for (Constructor<?> candidate : type.getDeclaredConstructors()) {
|
||||
|
||||
Class<?>[] parameterTypes = candidate.getParameterTypes();
|
||||
|
||||
if (argumentsMatch(parameterTypes, constructorArguments)) {
|
||||
return candidate;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
return Arrays.stream(type.getDeclaredConstructors())//
|
||||
.filter(constructor -> argumentsMatch(constructor.getParameterTypes(), constructorArguments))//
|
||||
.findFirst();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
62
src/main/java/org/springframework/data/util/Streamable.java
Normal file
62
src/main/java/org/springframework/data/util/Streamable.java
Normal file
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* Copyright 2016 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.util;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.stream.Stream;
|
||||
import java.util.stream.StreamSupport;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Simple interface to ease streamability of {@link Iterable}s.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public interface Streamable<T> extends Iterable<T> {
|
||||
|
||||
/**
|
||||
* Creates a non-parallel {@link Stream} of the underlying {@link Iterable}.
|
||||
*
|
||||
* @return will never be {@literal null}.
|
||||
*/
|
||||
default Stream<T> stream() {
|
||||
return StreamSupport.stream(spliterator(), false);
|
||||
}
|
||||
|
||||
public static <T> Streamable<T> empty() {
|
||||
return () -> Collections.emptyIterator();
|
||||
}
|
||||
|
||||
@SafeVarargs
|
||||
public static <T> Streamable<T> of(T... t) {
|
||||
return () -> Arrays.asList(t).iterator();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a {@link Streamable} for the given {@link Iterable}.
|
||||
*
|
||||
* @param iterable must not be {@literal null}.
|
||||
* @return
|
||||
*/
|
||||
public static <T> Streamable<T> of(Iterable<T> iterable) {
|
||||
|
||||
Assert.notNull(iterable, "Iterable must not be null!");
|
||||
|
||||
return () -> iterable.iterator();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user