Browse Source

DATACMNS-867 - Hacking.

pull/194/head
Oliver Gierke 9 years ago
parent
commit
cc1c07ecca
  1. 11
      src/main/java/org/springframework/data/repository/support/DefaultRepositoryInvokerFactory.java
  2. 49
      src/main/java/org/springframework/data/util/Optionals.java
  3. 2
      src/test/java/org/springframework/data/projection/DefaultProjectionInformationUnitTests.java
  4. 3
      src/test/java/org/springframework/data/repository/query/ReturnedTypeUnitTests.java

11
src/main/java/org/springframework/data/repository/support/DefaultRepositoryInvokerFactory.java

@ -15,6 +15,8 @@ @@ -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; @@ -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 @@ -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)));
}

49
src/main/java/org/springframework/data/util/Optionals.java

@ -20,6 +20,8 @@ import lombok.experimental.UtilityClass; @@ -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; @@ -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 { @@ -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();
}
}
}

2
src/test/java/org/springframework/data/projection/DefaultProjectionInformationUnitTests.java

@ -53,7 +53,7 @@ public class DefaultProjectionInformationUnitTests { @@ -53,7 +53,7 @@ public class DefaultProjectionInformationUnitTests {
ProjectionInformation information = new DefaultProjectionInformation(WithDefaultMethod.class);
assertThat(information.isClosed()).isTrue();
assertThat(toNames(information.getInputProperties())).contains("firstname");
assertThat(toNames(information.getInputProperties())).containsExactly("firstname");
}
private static List<String> toNames(List<PropertyDescriptor> descriptors) {

3
src/test/java/org/springframework/data/repository/query/ReturnedTypeUnitTests.java

@ -153,7 +153,8 @@ public class ReturnedTypeUnitTests { @@ -153,7 +153,8 @@ public class ReturnedTypeUnitTests {
List<String> properties = type.getInputProperties();
assertThat(properties).hasSize(1).contains("firstname");
assertThat(properties).hasSize(1);
assertThat(properties).containsExactly("firstname");
}
private static ReturnedType getReturnedType(String methodName, Class<?>... parameters) throws Exception {

Loading…
Cancel
Save