, P extends PersistentProperty>
implements MappingContext, ApplicationEventPublisherAware, InitializingBean {
- private final Map, E> persistentEntities = new HashMap, E>();
+ private final Optional NONE = Optional.empty();
+ private final Map, Optional> persistentEntities = new HashMap<>();
private final PersistentPropertyAccessorFactory persistentPropertyAccessorFactory = new ClassGeneratingPropertyAccessorFactory();
private ApplicationEventPublisher applicationEventPublisher;
@@ -132,8 +136,13 @@ public abstract class AbstractMappingContext getPersistentEntities() {
try {
+
read.lock();
- return Collections.unmodifiableSet(new HashSet(persistentEntities.values()));
+
+ return persistentEntities.values().stream()//
+ .flatMap(it -> Optionals.toStream(it))//
+ .collect(Collectors.toSet());
+
} finally {
read.unlock();
}
@@ -143,10 +152,21 @@ public abstract class AbstractMappingContext type) {
+ public Optional getPersistentEntity(Class> type) {
return getPersistentEntity(ClassTypeInformation.from(type));
}
+ /*
+ * (non-Javadoc)
+ * @see org.springframework.data.mapping.context.MappingContext#getRequiredPersistentEntity(java.lang.Class)
+ */
+ @Override
+ public E getRequiredPersistentEntity(Class> type) {
+
+ return getPersistentEntity(type).orElseThrow(
+ () -> new IllegalArgumentException(String.format("Couldn't find PersistentEntity for type %s!", type)));
+ }
+
/*
* (non-Javadoc)
* @see org.springframework.data.mapping.context.MappingContext#hasPersistentEntityFor(java.lang.Class)
@@ -160,13 +180,15 @@ public abstract class AbstractMappingContext type) {
+ public Optional getPersistentEntity(TypeInformation> type) {
Assert.notNull(type, "Type must not be null!");
try {
+
read.lock();
- E entity = persistentEntities.get(type);
+
+ Optional entity = persistentEntities.get(type);
if (entity != null) {
return entity;
@@ -177,7 +199,15 @@ public abstract class AbstractMappingContext type) {
+
+ return getPersistentEntity(type)
+ .orElseThrow(() -> new MappingException(String.format("Couldn't find PersistentEntity for type %s!", type)));
+ }
+
/*
* (non-Javadoc)
* @see org.springframework.data.mapping.context.MappingContext#getPersistentEntity(org.springframework.data.mapping.PersistentProperty)
*/
- public E getPersistentEntity(P persistentProperty) {
+ public Optional getPersistentEntity(P persistentProperty) {
- if (persistentProperty == null) {
- return null;
- }
+ Assert.notNull(persistentProperty, "PersistentProperty must not be null!");
TypeInformation> typeInfo = persistentProperty.getTypeInformation();
return getPersistentEntity(typeInfo.getActualType());
}
+ /*
+ * (non-Javadoc)
+ * @see org.springframework.data.mapping.context.MappingContext#getRequiredPersistentEntity(org.springframework.data.mapping.PersistentProperty)
+ */
+ @Override
+ public E getRequiredPersistentEntity(P persistentProperty) {
+
+ return getPersistentEntity(persistentProperty).orElseThrow(() -> new IllegalArgumentException(
+ String.format("Couldn't find PersistentEntity for type %s!", persistentProperty)));
+ }
+
/*
* (non-Javadoc)
* @see org.springframework.data.mapping.context.MappingContext#getPersistentPropertyPath(java.lang.Class, java.lang.String)
@@ -248,7 +298,7 @@ public abstract class AbstractMappingContext path = DefaultPersistentPropertyPath.empty();
Iterator iterator = parts.iterator();
- E current = getPersistentEntity(type);
+ E current = getRequiredPersistentEntity(type);
while (iterator.hasNext()) {
@@ -277,8 +327,11 @@ public abstract class AbstractMappingContext