DATACMNS-867 - Hacking.

This commit is contained in:
Oliver Gierke
2017-01-30 14:01:50 +01:00
parent 47e2e61ff5
commit cc1c07ecca
4 changed files with 57 additions and 8 deletions

View File

@@ -15,6 +15,8 @@
*/
package org.springframework.data.repository.support;
import static org.springframework.data.util.Optionals.*;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
@@ -24,8 +26,6 @@ import org.springframework.core.convert.ConversionService;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.core.RepositoryInformation;
import org.springframework.data.util.Optionals;
import org.springframework.data.util.Pair;
import org.springframework.format.support.DefaultFormattingConversionService;
import org.springframework.util.Assert;
@@ -87,11 +87,10 @@ public class DefaultRepositoryInvokerFactory implements RepositoryInvokerFactory
*/
private RepositoryInvoker prepareInvokers(Class<?> domainType) {
Optional<Pair<Object, RepositoryInformation>> repositoryAndInformation = Optionals
.withBoth(repositories.getRepositoryFor(domainType), repositories.getRepositoryInformationFor(domainType));
Optional<RepositoryInformation> information = repositories.getRepositoryInformationFor(domainType);
Optional<Object> repository = repositories.getRepositoryFor(domainType);
return repositoryAndInformation//
.map(it -> createInvoker(it.getSecond(), it.getFirst())) //
return mapIfAllPresent(information, repository, (left, right) -> createInvoker(left, right))//
.orElseThrow(
() -> new IllegalArgumentException(String.format("No repository found for domain type: %s", domainType)));
}

View File

@@ -20,6 +20,8 @@ import lombok.experimental.UtilityClass;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Optional;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Stream;
@@ -34,6 +36,8 @@ import org.springframework.util.Assert;
@UtilityClass
public class Optionals {
private static final Object SUCCESS = new Object();
/**
* Returns whether any of the given {@link Optional}s is present.
*
@@ -152,4 +156,49 @@ public class Optionals {
public static <T, S> Optional<Pair<T, S>> withBoth(Optional<T> left, Optional<S> right) {
return left.flatMap(l -> right.map(r -> Pair.of(l, r)));
}
/**
* Invokes the given {@link BiConsumer} if all given {@link Optional} are present.
*
* @param left must not be {@literal null}.
* @param right must not be {@literal null}.
* @param consumer must not be {@literal null}.
*/
public static <T, S> Optional<Object> ifAllPresent(Optional<T> left, Optional<S> right, BiConsumer<T, S> consumer) {
Assert.notNull(left, "Optional must not be null!");
Assert.notNull(right, "Optional must not be null!");
Assert.notNull(consumer, "Consumer must not be null!");
return mapIfAllPresent(left, right, (l, r) -> {
consumer.accept(l, r);
return SUCCESS;
});
}
/**
* Maps the values contained in the given {@link Optional} if both of them are present.
*
* @param left must not be {@literal null}.
* @param right must not be {@literal null}.
* @param function must not be {@literal null}.
* @return
*/
public static <T, S, R> Optional<R> mapIfAllPresent(Optional<T> left, Optional<S> right,
BiFunction<T, S, R> function) {
Assert.notNull(left, "Optional must not be null!");
Assert.notNull(right, "Optional must not be null!");
Assert.notNull(function, "BiFunctionmust not be null!");
return left.flatMap(l -> right.map(r -> function.apply(l, r)));
}
public static <T extends Throwable> void ifBothAbsent(Optional<?> left, Optional<?> right, Supplier<T> supplier)
throws T {
if (!left.isPresent() && !right.isPresent()) {
throw supplier.get();
}
}
}