diff --git a/src/main/java/org/springframework/data/mapping/context/AbstractMappingContext.java b/src/main/java/org/springframework/data/mapping/context/AbstractMappingContext.java index 0d3a6504c4..12f02b97f9 100644 --- a/src/main/java/org/springframework/data/mapping/context/AbstractMappingContext.java +++ b/src/main/java/org/springframework/data/mapping/context/AbstractMappingContext.java @@ -35,7 +35,6 @@ import java.util.Set; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantReadWriteLock; -import java.util.stream.Collectors; import org.springframework.beans.BeanUtils; import org.springframework.beans.BeansException; @@ -52,7 +51,6 @@ import org.springframework.data.mapping.model.Property; import org.springframework.data.mapping.model.SimpleTypeHolder; import org.springframework.data.util.ClassTypeInformation; -import org.springframework.data.util.Optionals; import org.springframework.data.util.Pair; import org.springframework.data.util.Streamable; import org.springframework.data.util.TypeInformation; @@ -73,6 +71,7 @@ * * @param the concrete {@link PersistentEntity} type the {@link MappingContext} implementation creates * @param

the concrete {@link PersistentProperty} type the {@link MappingContext} implementation creates + * @author Bartosz Kielczewski * @author Jon Brisbin * @author Oliver Gierke * @author Michael Hunger @@ -85,8 +84,7 @@ public abstract class AbstractMappingContext, P extends PersistentProperty

> implements MappingContext, ApplicationEventPublisherAware, InitializingBean { - private final Optional NONE = Optional.empty(); - private final Map, Optional> persistentEntities = new HashMap<>(); + private final Map, E> persistentEntities = new HashMap<>(); private final Map> propertyPaths = new ConcurrentReferenceHashMap<>(); private final PersistentPropertyAccessorFactory persistentPropertyAccessorFactory = new ClassGeneratingPropertyAccessorFactory(); @@ -145,7 +143,7 @@ public void setSimpleTypeHolder(SimpleTypeHolder simpleTypes) { /* * (non-Javadoc) - * @see org.springframework.data.mapping.model.MappingContext#getPersistentEntities() + * @see org.springframework.data.mapping.context.MappingContext#getPersistentEntities() */ @Override public Collection getPersistentEntities() { @@ -154,9 +152,7 @@ public Collection getPersistentEntities() { read.lock(); - return persistentEntities.values().stream()// - .flatMap(Optionals::toStream)// - .collect(Collectors.toSet()); + return new HashSet<>(persistentEntities.values()); } finally { read.unlock(); @@ -186,7 +182,7 @@ public boolean hasPersistentEntityFor(Class type) { /* * (non-Javadoc) - * @see org.springframework.data.mapping.model.MappingContext#getPersistentEntity(org.springframework.data.util.TypeInformation) + * @see org.springframework.data.mapping.context.MappingContext#getPersistentEntity(org.springframework.data.util.TypeInformation) */ @Nullable @Override @@ -194,37 +190,34 @@ public E getPersistentEntity(TypeInformation type) { Assert.notNull(type, "Type must not be null!"); + E entity = getExistingPersistentEntity(type); + if (entity != null) { + return entity; + } else if (!shouldCreatePersistentEntityFor(type)) { + return null; + } else if (strict) { + throw new MappingException("Unknown persistent entity " + type); + } else { + return addPersistentEntity(type); + } + } + + /** + * (non-Javadoc) + * @see org.springframework.data.mapping.context.MappingContext#getPersistentEntity(org.springframework.data.util.TypeInformation) + */ + @Nullable + private E getExistingPersistentEntity(TypeInformation type) { + try { read.lock(); - Optional entity = persistentEntities.get(type); - - if (entity != null) { - return entity.orElse(null); - } + return persistentEntities.get(type); } finally { read.unlock(); } - - if (!shouldCreatePersistentEntityFor(type)) { - - try { - write.lock(); - persistentEntities.put(type, NONE); - } finally { - write.unlock(); - } - - return null; - } - - if (strict) { - throw new MappingException("Unknown persistent entity " + type); - } - - return addPersistentEntity(type).orElse(null); } /* @@ -339,7 +332,7 @@ private Pair, E> getPair(DefaultPersistentPrope * @param type must not be {@literal null}. * @return */ - protected Optional addPersistentEntity(Class type) { + protected E addPersistentEntity(Class type) { return addPersistentEntity(ClassTypeInformation.from(type)); } @@ -349,26 +342,16 @@ protected Optional addPersistentEntity(Class type) { * @param typeInformation must not be {@literal null}. * @return */ - protected Optional addPersistentEntity(TypeInformation typeInformation) { + protected E addPersistentEntity(TypeInformation typeInformation) { Assert.notNull(typeInformation, "TypeInformation must not be null!"); - try { - - read.lock(); - - Optional persistentEntity = persistentEntities.get(typeInformation); - - if (persistentEntity != null) { - return persistentEntity; - } - - } finally { - read.unlock(); + E entity = persistentEntities.get(typeInformation); + if (entity != null) { + return entity; } Class type = typeInformation.getType(); - E entity = null; try { @@ -377,7 +360,7 @@ protected Optional addPersistentEntity(TypeInformation typeInformation) { entity = createPersistentEntity(typeInformation); // Eagerly cache the entity as we might have to find it during recursive lookups. - persistentEntities.put(typeInformation, Optional.of(entity)); + persistentEntities.put(typeInformation, entity); PropertyDescriptor[] pds = BeanUtils.getPropertyDescriptors(type); @@ -414,7 +397,7 @@ protected Optional addPersistentEntity(TypeInformation typeInformation) { applicationEventPublisher.publishEvent(new MappingContextEvent<>(this, entity)); } - return Optional.of(entity); + return entity; } /* diff --git a/src/test/java/org/springframework/data/mapping/context/AbstractMappingContextUnitTests.java b/src/test/java/org/springframework/data/mapping/context/AbstractMappingContextUnitTests.java index 1b27db1754..601c3754e8 100755 --- a/src/test/java/org/springframework/data/mapping/context/AbstractMappingContextUnitTests.java +++ b/src/test/java/org/springframework/data/mapping/context/AbstractMappingContextUnitTests.java @@ -43,6 +43,7 @@ /** * Unit test for {@link AbstractMappingContext}. * + * @author Bartosz Kielczewski * @author Oliver Gierke * @author Thomas Darimont * @author Mark Paluch @@ -115,6 +116,14 @@ public void returnsNullPersistentEntityForSimpleTypes() { assertThat(context.getPersistentEntity(String.class)).isNull(); } + @Test // DATACMNS-1208 + public void ensureHasPersistentEntityReportsFalseForTypesThatShouldntBeCreated() { + SampleMappingContext context = new SampleMappingContext(); + assertThat(context.hasPersistentEntityFor(String.class)).isFalse(); + assertThat(context.getPersistentEntity(String.class)).isNull(); + assertThat(context.hasPersistentEntityFor(String.class)).isFalse(); + } + @Test(expected = IllegalArgumentException.class) // DATACMNS-214 public void rejectsNullValueForGetPersistentEntityOfClass() { context.getPersistentEntity((Class) null);