Skip to content

Commit e683c8f

Browse files
committed
DATAMONGO-1854 - Polishing.
Extract common collation resolution code into EntityOperations (TypedOperations). Original pull request: #644.
1 parent ac1873a commit e683c8f

File tree

5 files changed

+372
-262
lines changed

5 files changed

+372
-262
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/EntityOperations.java

Lines changed: 119 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,23 @@
2121

2222
import java.util.Collection;
2323
import java.util.Map;
24+
import java.util.Optional;
2425

2526
import org.bson.Document;
27+
2628
import org.springframework.core.convert.ConversionService;
2729
import org.springframework.dao.InvalidDataAccessApiUsageException;
2830
import org.springframework.data.mapping.IdentifierAccessor;
2931
import org.springframework.data.mapping.MappingException;
32+
import org.springframework.data.mapping.PersistentEntity;
3033
import org.springframework.data.mapping.PersistentPropertyAccessor;
3134
import org.springframework.data.mapping.context.MappingContext;
3235
import org.springframework.data.mapping.model.ConvertingPropertyAccessor;
3336
import org.springframework.data.mongodb.core.convert.MongoWriter;
3437
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
3538
import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty;
3639
import org.springframework.data.mongodb.core.mapping.MongoSimpleTypes;
40+
import org.springframework.data.mongodb.core.query.Collation;
3741
import org.springframework.data.mongodb.core.query.Criteria;
3842
import org.springframework.data.mongodb.core.query.Query;
3943
import org.springframework.lang.Nullable;
@@ -176,6 +180,20 @@ private static Document parse(String source) {
176180
}
177181
}
178182

183+
public <T> TypedOperations<T> forType(@Nullable Class<T> entityClass) {
184+
185+
if (entityClass != null) {
186+
187+
MongoPersistentEntity<?> entity = context.getPersistentEntity(entityClass);
188+
189+
if (entity != null) {
190+
return new TypedEntityOperations(entity);
191+
}
192+
193+
}
194+
return UntypedOperations.instance();
195+
}
196+
179197
/**
180198
* A representation of information about an entity.
181199
*
@@ -263,7 +281,7 @@ default boolean isVersionedEntity() {
263281

264282
/**
265283
* Returns whether the entity is considered to be new.
266-
*
284+
*
267285
* @return
268286
* @since 2.1.2
269287
*/
@@ -414,7 +432,7 @@ public T getBean() {
414432
return map;
415433
}
416434

417-
/*
435+
/*
418436
* (non-Javadoc)
419437
* @see org.springframework.data.mongodb.core.EntityOperations.Entity#isNew()
420438
*/
@@ -585,7 +603,7 @@ public T getBean() {
585603
return propertyAccessor.getBean();
586604
}
587605

588-
/*
606+
/*
589607
* (non-Javadoc)
590608
* @see org.springframework.data.mongodb.core.EntityOperations.Entity#isNew()
591609
*/
@@ -698,4 +716,102 @@ public T incrementVersion() {
698716
return propertyAccessor.getBean();
699717
}
700718
}
719+
720+
/**
721+
* Type-specific operations abstraction.
722+
*
723+
* @author Mark Paluch
724+
* @param <T>
725+
* @since 2.2
726+
*/
727+
interface TypedOperations<T> {
728+
729+
/**
730+
* Return the optional {@link Collation} for the underlying entity.
731+
*
732+
* @return
733+
*/
734+
Optional<Collation> getCollation();
735+
736+
/**
737+
* Return the optional {@link Collation} from the given {@link Query} and fall back to the collation configured for
738+
* the underlying entity.
739+
*
740+
* @return
741+
*/
742+
Optional<Collation> getCollation(Query query);
743+
}
744+
745+
/**
746+
* {@link TypedOperations} for generic entities that are not represented with {@link PersistentEntity} (e.g. custom
747+
* conversions).
748+
*/
749+
@RequiredArgsConstructor
750+
enum UntypedOperations implements TypedOperations<Object> {
751+
752+
INSTANCE;
753+
754+
@SuppressWarnings({ "unchecked", "rawtypes" })
755+
public static <T> TypedOperations<T> instance() {
756+
return (TypedOperations) INSTANCE;
757+
}
758+
759+
/*
760+
* (non-Javadoc)
761+
* @see org.springframework.data.mongodb.core.EntityOperations.TypedOperations#getCollation()
762+
*/
763+
@Override
764+
public Optional<Collation> getCollation() {
765+
return Optional.empty();
766+
}
767+
768+
/*
769+
* (non-Javadoc)
770+
* @see org.springframework.data.mongodb.core.EntityOperations.TypedOperations#getCollation(org.springframework.data.mongodb.core.query.Query)
771+
*/
772+
@Override
773+
public Optional<Collation> getCollation(Query query) {
774+
775+
if (query == null) {
776+
return Optional.empty();
777+
}
778+
779+
return query.getCollation();
780+
}
781+
}
782+
783+
/**
784+
* {@link TypedOperations} backed by {@link MongoPersistentEntity}.
785+
*
786+
* @param <T>
787+
*/
788+
@RequiredArgsConstructor
789+
static class TypedEntityOperations<T> implements TypedOperations<T> {
790+
791+
private final @NonNull MongoPersistentEntity<T> entity;
792+
793+
/*
794+
* (non-Javadoc)
795+
* @see org.springframework.data.mongodb.core.EntityOperations.TypedOperations#getCollation()
796+
*/
797+
@Override
798+
public Optional<Collation> getCollation() {
799+
return Optional.ofNullable(entity.getCollation());
800+
}
801+
802+
/*
803+
* (non-Javadoc)
804+
* @see org.springframework.data.mongodb.core.EntityOperations.TypedOperations#getCollation(org.springframework.data.mongodb.core.query.Query)
805+
*/
806+
@Override
807+
public Optional<Collation> getCollation(Query query) {
808+
809+
if (query.getCollation().isPresent()) {
810+
return query.getCollation();
811+
}
812+
813+
return Optional.ofNullable(entity.getCollation());
814+
}
815+
}
816+
701817
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/FindAndModifyOptions.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public FindAndModifyOptions remove(boolean remove) {
5353
}
5454

5555
@Override
56-
public FindAndModifyOptions collation(Collation collation) {
56+
public FindAndModifyOptions collation(@Nullable Collation collation) {
5757
throw new UnsupportedOperationException(ERROR_MSG);
5858
}
5959
};
@@ -79,7 +79,7 @@ public static FindAndModifyOptions none() {
7979

8080
/**
8181
* Create new {@link FindAndModifyOptions} based on option of given {@litearl source}.
82-
*
82+
*
8383
* @param source can be {@literal null}.
8484
* @return new instance of {@link FindAndModifyOptions}.
8585
* @since 2.0

0 commit comments

Comments
 (0)