diff --git a/src/main/java/org/springframework/data/mapping/context/PersistentEntities.java b/src/main/java/org/springframework/data/mapping/context/PersistentEntities.java index 245f9751b..747a74635 100644 --- a/src/main/java/org/springframework/data/mapping/context/PersistentEntities.java +++ b/src/main/java/org/springframework/data/mapping/context/PersistentEntities.java @@ -47,8 +47,8 @@ public class PersistentEntities implements Streamable> { /** * Returns the {@link PersistentEntity} for the given type. Will consider all {@link MappingContext}s registered but - * return {@literal null} in case none of the registered ones already have a {@link PersistentEntity} registered for - * the given type. + * return {@literal Optional#empty()} in case none of the registered ones already have a {@link PersistentEntity} + * registered for the given type. * * @param type can be {@literal null}. * @return @@ -60,6 +60,23 @@ public class PersistentEntities implements Streamable> { .findFirst().map(it -> it.getRequiredPersistentEntity(type)); } + /** + * Returns the {@link PersistentEntity} for the given type. Will consider all {@link MappingContext}s registered but + * throw an {@link IllegalArgumentException} in case none of the registered ones already have a + * {@link PersistentEntity} registered for the given type. + * + * @param type must not be {@literal null}. + * @return the {@link PersistentEntity} for the given domain type. + * @throws IllegalArgumentException in case no {@link PersistentEntity} can be found for the given type. + */ + public PersistentEntity getRequiredPersistentEntity(Class type) { + + Assert.notNull(type, "Domain type must not be null!"); + + return getPersistentEntity(type).orElseThrow( + () -> new IllegalArgumentException(String.format("Couldn't find PersistentEntity for type %s!", type))); + } + /** * Returns all {@link TypeInformation} exposed by the registered {@link MappingContext}s. * @@ -79,7 +96,7 @@ public class PersistentEntities implements Streamable> { @Override public Iterator> iterator() { - return contexts.stream().>flatMap(it -> it.getPersistentEntities().stream()) + return contexts.stream().> flatMap(it -> it.getPersistentEntities().stream()) .collect(Collectors.toList()).iterator(); } } diff --git a/src/main/java/org/springframework/data/repository/support/Repositories.java b/src/main/java/org/springframework/data/repository/support/Repositories.java index 73dd36f6b..7f75af323 100644 --- a/src/main/java/org/springframework/data/repository/support/Repositories.java +++ b/src/main/java/org/springframework/data/repository/support/Repositories.java @@ -74,7 +74,7 @@ public class Repositories implements Iterable> { */ public Repositories(ListableBeanFactory factory) { - Assert.notNull(factory, "Factory must not be null!"); + Assert.notNull(factory, "ListableBeanFactory must not be null!"); this.beanFactory = Optional.of(factory); this.repositoryFactoryInfos = new HashMap<>(); @@ -196,6 +196,19 @@ public class Repositories implements Iterable> { : Optional.of(information.getRepositoryInformation()); } + /** + * Returns the {@link RepositoryInformation} for the given domain type. + * + * @param domainType must not be {@literal null}. + * @return the {@link RepositoryInformation} for the given domain type. + * @throws IllegalArgumentException in case no {@link RepositoryInformation} could be found for the given domain type. + */ + public RepositoryInformation getRequiredRepositoryInformation(Class domainType) { + + return getRepositoryInformationFor(domainType).orElseThrow(() -> new IllegalArgumentException( + "No required RepositoryInformation found for domain type " + domainType.getName() + "!")); + } + /** * Returns the {@link RepositoryInformation} for the given repository interface. *