DATAMONGO-1563 - Polishing.
Rename TerminatingAggregationOperation.get() to TerminatingAggregationOperation.all() to name methods consistently. Extract collection name retrieval to method. Javadoc, formatting, add generics where required/use diamond syntax where applicable. Original pull request: #466.
This commit is contained in:
@@ -36,6 +36,7 @@ import org.springframework.data.util.CloseableIterator;
|
||||
* </pre>
|
||||
*
|
||||
* @author Christoph Strobl
|
||||
* @author Mark Paluch
|
||||
* @since 2.0
|
||||
*/
|
||||
public interface ExecutableAggregationOperation {
|
||||
@@ -54,7 +55,6 @@ public interface ExecutableAggregationOperation {
|
||||
/**
|
||||
* Collection override (Optional).
|
||||
*
|
||||
* @param <T>
|
||||
* @author Christoph Strobl
|
||||
* @since 2.0
|
||||
*/
|
||||
@@ -74,19 +74,20 @@ public interface ExecutableAggregationOperation {
|
||||
/**
|
||||
* Trigger execution by calling one of the terminating methods.
|
||||
*
|
||||
* @param <T>
|
||||
* @author Christoph Strobl
|
||||
* @since 2.0
|
||||
*/
|
||||
interface TerminatingAggregationOperation<T> {
|
||||
|
||||
/**
|
||||
* Apply pipeline operations as specified.
|
||||
* Apply pipeline operations as specified and get all matching elements.
|
||||
*
|
||||
* @return never {@literal null}.
|
||||
*/
|
||||
AggregationResults<T> get();
|
||||
AggregationResults<T> all();
|
||||
|
||||
/**
|
||||
* Apply pipeline operations as specified. <br />
|
||||
* Apply pipeline operations as specified and stream all matching elements. <br />
|
||||
* Returns a {@link CloseableIterator} that wraps the a Mongo DB {@link com.mongodb.Cursor}
|
||||
*
|
||||
* @return a {@link CloseableIterator} that wraps the a Mongo DB {@link com.mongodb.Cursor} that needs to be closed.
|
||||
@@ -98,7 +99,6 @@ public interface ExecutableAggregationOperation {
|
||||
/**
|
||||
* Define the aggregation with pipeline stages.
|
||||
*
|
||||
* @param <T>
|
||||
* @author Christoph Strobl
|
||||
* @since 2.0
|
||||
*/
|
||||
@@ -115,7 +115,6 @@ public interface ExecutableAggregationOperation {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param <T>
|
||||
* @author Christoph Strobl
|
||||
* @since 2.0
|
||||
*/
|
||||
|
||||
@@ -35,7 +35,7 @@ class ExecutableAggregationOperationSupport implements ExecutableAggregationOper
|
||||
private final MongoTemplate template;
|
||||
|
||||
/**
|
||||
* Create new instance of ExecutableAggregationOperationSupport.
|
||||
* Create new instance of {@link ExecutableAggregationOperationSupport}.
|
||||
*
|
||||
* @param template must not be {@literal null}.
|
||||
* @throws IllegalArgumentException if template is {@literal null}.
|
||||
@@ -43,6 +43,7 @@ class ExecutableAggregationOperationSupport implements ExecutableAggregationOper
|
||||
ExecutableAggregationOperationSupport(MongoTemplate template) {
|
||||
|
||||
Assert.notNull(template, "Template must not be null!");
|
||||
|
||||
this.template = template;
|
||||
}
|
||||
|
||||
@@ -50,11 +51,11 @@ class ExecutableAggregationOperationSupport implements ExecutableAggregationOper
|
||||
public <T> AggregationOperation<T> aggregateAndReturn(Class<T> domainType) {
|
||||
|
||||
Assert.notNull(domainType, "DomainType must not be null!");
|
||||
return new AggregationOperationSupport<T>(template, null, domainType, null);
|
||||
|
||||
return new AggregationOperationSupport<>(template, null, domainType, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param <T>
|
||||
* @author Christoph Strobl
|
||||
* @since 2.0
|
||||
*/
|
||||
@@ -71,18 +72,20 @@ class ExecutableAggregationOperationSupport implements ExecutableAggregationOper
|
||||
public AggregationOperationWithAggregation<T> inCollection(String collection) {
|
||||
|
||||
Assert.hasText(collection, "Collection must not be null nor empty!");
|
||||
return new AggregationOperationSupport<T>(template, aggregation, domainType, collection);
|
||||
|
||||
return new AggregationOperationSupport<>(template, aggregation, domainType, collection);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TerminatingAggregationOperation<T> by(Aggregation aggregation) {
|
||||
|
||||
Assert.notNull(aggregation, "Aggregation must not be null!");
|
||||
return new AggregationOperationSupport<T>(template, aggregation, domainType, collection);
|
||||
|
||||
return new AggregationOperationSupport<>(template, aggregation, domainType, collection);
|
||||
}
|
||||
|
||||
@Override
|
||||
public AggregationResults<T> get() {
|
||||
public AggregationResults<T> all() {
|
||||
return template.aggregate(aggregation, getCollectionName(aggregation), domainType);
|
||||
}
|
||||
|
||||
@@ -99,8 +102,10 @@ class ExecutableAggregationOperationSupport implements ExecutableAggregationOper
|
||||
|
||||
if (aggregation instanceof TypedAggregation) {
|
||||
|
||||
if (((TypedAggregation<?>) aggregation).getInputType() != null) {
|
||||
return template.determineCollectionName(((TypedAggregation<?>) aggregation).getInputType());
|
||||
TypedAggregation<?> typedAggregation = (TypedAggregation<?>) aggregation;
|
||||
|
||||
if (typedAggregation.getInputType() != null) {
|
||||
return template.determineCollectionName(typedAggregation.getInputType());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -27,9 +27,9 @@ import org.springframework.data.util.CloseableIterator;
|
||||
* {@link ExecutableFindOperation} allows creation and execution of MongoDB find operations in a fluent API style.
|
||||
* <br />
|
||||
* The starting {@literal domainType} is used for mapping the {@link Query} provided via {@code matching} into the
|
||||
* MongoDB specific representation. By default this originating {@literal domainType} is also used for mapping back the
|
||||
* result from the {@link org.bson.Document}. However it is possible to define an different {@literal returnType} via
|
||||
* {@code as} that is then used for mapping the result mapping. <br />
|
||||
* MongoDB specific representation. By default, the originating {@literal domainType} is also used for mapping back the
|
||||
* result from the {@link org.bson.Document}. However, it is possible to define an different {@literal returnType} via
|
||||
* {@code as} to mapping the result.<br />
|
||||
* The collection to operate on is by default derived from the initial {@literal domainType} and can be defined there
|
||||
* via {@link org.springframework.data.mongodb.core.mapping.Document}. Using {@code inCollection} allows to override the
|
||||
* collection name for the execution.
|
||||
@@ -45,6 +45,7 @@ import org.springframework.data.util.CloseableIterator;
|
||||
* </pre>
|
||||
*
|
||||
* @author Christoph Strobl
|
||||
* @author Mark Paluch
|
||||
* @since 2.0
|
||||
*/
|
||||
public interface ExecutableFindOperation {
|
||||
@@ -61,7 +62,6 @@ public interface ExecutableFindOperation {
|
||||
/**
|
||||
* Trigger find execution by calling one of the terminating methods.
|
||||
*
|
||||
* @param <T>
|
||||
* @author Christoph Strobl
|
||||
* @since 2.0
|
||||
*/
|
||||
@@ -85,7 +85,7 @@ public interface ExecutableFindOperation {
|
||||
/**
|
||||
* Get all matching elements.
|
||||
*
|
||||
* @return never {@literal}.
|
||||
* @return never {@literal null}.
|
||||
*/
|
||||
List<T> all();
|
||||
|
||||
@@ -101,7 +101,6 @@ public interface ExecutableFindOperation {
|
||||
/**
|
||||
* Trigger geonear execution by calling one of the terminating methods.
|
||||
*
|
||||
* @param <T>
|
||||
* @author Christoph Strobl
|
||||
* @since 2.0
|
||||
*/
|
||||
@@ -118,7 +117,6 @@ public interface ExecutableFindOperation {
|
||||
/**
|
||||
* Terminating operations invoking the actual query execution.
|
||||
*
|
||||
* @param <T>
|
||||
* @author Christoph Strobl
|
||||
* @since 2.0
|
||||
*/
|
||||
@@ -146,7 +144,6 @@ public interface ExecutableFindOperation {
|
||||
/**
|
||||
* Collection override (Optional).
|
||||
*
|
||||
* @param <T>
|
||||
* @author Christoph Strobl
|
||||
* @since 2.0
|
||||
*/
|
||||
@@ -166,7 +163,6 @@ public interface ExecutableFindOperation {
|
||||
/**
|
||||
* Result type override (Optional).
|
||||
*
|
||||
* @param <T>
|
||||
* @author Christoph Strobl
|
||||
* @since 2.0
|
||||
*/
|
||||
@@ -187,11 +183,8 @@ public interface ExecutableFindOperation {
|
||||
/**
|
||||
* {@link FindOperation} provides methods for constructing lookup operations in a fluent way.
|
||||
*
|
||||
* @param <T>
|
||||
* @author Christoph Strobl
|
||||
* @since 2.0
|
||||
*/
|
||||
interface FindOperation<T> extends FindOperationWithCollection<T>, FindOperationWithProjection<T> {
|
||||
|
||||
}
|
||||
interface FindOperation<T> extends FindOperationWithCollection<T>, FindOperationWithProjection<T> {}
|
||||
}
|
||||
|
||||
@@ -123,6 +123,7 @@ class ExecutableFindOperationSupport implements ExecutableFindOperation {
|
||||
public Optional<T> first() {
|
||||
|
||||
List<T> result = doFind(new DelegatingQueryCursorPreparer(getCursorPreparer(query, null)).limit(1));
|
||||
|
||||
return ObjectUtils.isEmpty(result) ? Optional.empty() : Optional.of(result.iterator().next());
|
||||
}
|
||||
|
||||
@@ -170,7 +171,6 @@ class ExecutableFindOperationSupport implements ExecutableFindOperation {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param <T>
|
||||
* @author Christoph Strobl
|
||||
* @since 2.0
|
||||
*/
|
||||
@@ -179,7 +179,7 @@ class ExecutableFindOperationSupport implements ExecutableFindOperation {
|
||||
private final CursorPreparer delegate;
|
||||
private Optional<Integer> limit = Optional.empty();
|
||||
|
||||
public DelegatingQueryCursorPreparer(CursorPreparer delegate) {
|
||||
DelegatingQueryCursorPreparer(CursorPreparer delegate) {
|
||||
this.delegate = delegate;
|
||||
}
|
||||
|
||||
@@ -187,7 +187,8 @@ class ExecutableFindOperationSupport implements ExecutableFindOperation {
|
||||
public FindIterable<Document> prepare(FindIterable<Document> cursor) {
|
||||
|
||||
FindIterable<Document> target = delegate.prepare(cursor);
|
||||
return limit.map(it -> target.limit(it)).orElse(target);
|
||||
|
||||
return limit.map(target::limit).orElse(target);
|
||||
}
|
||||
|
||||
CursorPreparer limit(int limit) {
|
||||
|
||||
@@ -22,7 +22,7 @@ import org.springframework.data.mongodb.core.BulkOperations.BulkMode;
|
||||
import com.mongodb.bulk.BulkWriteResult;
|
||||
|
||||
/**
|
||||
* {@link ExecutableFindOperation} allows creation and execution of MongoDB insert and bulk insert operations in a
|
||||
* {@link ExecutableInsertOperation} allows creation and execution of MongoDB insert and bulk insert operations in a
|
||||
* fluent API style. <br />
|
||||
* The collection to operate on is by default derived from the initial {@literal domainType} and can be defined there
|
||||
* via {@link org.springframework.data.mongodb.core.mapping.Document}. Using {@code inCollection} allows to override the
|
||||
@@ -53,7 +53,6 @@ public interface ExecutableInsertOperation {
|
||||
/**
|
||||
* Trigger insert execution by calling one of the terminating methods.
|
||||
*
|
||||
* @param <T>
|
||||
* @author Christoph Strobl
|
||||
* @since 2.0
|
||||
*/
|
||||
@@ -79,7 +78,6 @@ public interface ExecutableInsertOperation {
|
||||
/**
|
||||
* Trigger bulk insert execution by calling one of the terminating methods.
|
||||
*
|
||||
* @param <T>
|
||||
* @author Christoph Strobl
|
||||
* @since 2.0
|
||||
*/
|
||||
@@ -96,19 +94,15 @@ public interface ExecutableInsertOperation {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param <T>
|
||||
* @author Christoph Strobl
|
||||
* @since 2.0
|
||||
*/
|
||||
interface InsertOperation<T>
|
||||
extends TerminatingInsertOperation<T>, InsertOperationWithCollection<T>, InsertOperationWithBulkMode<T> {
|
||||
|
||||
}
|
||||
extends TerminatingInsertOperation<T>, InsertOperationWithCollection<T>, InsertOperationWithBulkMode<T> {}
|
||||
|
||||
/**
|
||||
* Collection override (Optional).
|
||||
*
|
||||
* @param <T>
|
||||
* @author Christoph Strobl
|
||||
* @since 2.0
|
||||
*/
|
||||
@@ -126,7 +120,6 @@ public interface ExecutableInsertOperation {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param <T>
|
||||
* @author Christoph Strobl
|
||||
* @since 2.0
|
||||
*/
|
||||
@@ -135,7 +128,7 @@ public interface ExecutableInsertOperation {
|
||||
/**
|
||||
* Define the {@link BulkMode} to use for bulk insert operation.
|
||||
*
|
||||
* @param mode must not be {@literal null}.
|
||||
* @param bulkMode must not be {@literal null}.
|
||||
* @return new instance of {@link TerminatingBulkInsertOperation}.
|
||||
* @throws IllegalArgumentException if bulkMode is {@literal null}.
|
||||
*/
|
||||
|
||||
@@ -53,11 +53,11 @@ class ExecutableInsertOperationSupport implements ExecutableInsertOperation {
|
||||
public <T> InsertOperation<T> insert(Class<T> domainType) {
|
||||
|
||||
Assert.notNull(domainType, "DomainType must not be null!");
|
||||
return new InsertOperationSupport<T>(template, domainType, null, null);
|
||||
|
||||
return new InsertOperationSupport<>(template, domainType, null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param <T>
|
||||
* @author Christoph Strobl
|
||||
* @since 2.0
|
||||
*/
|
||||
@@ -73,6 +73,7 @@ class ExecutableInsertOperationSupport implements ExecutableInsertOperation {
|
||||
public void one(T object) {
|
||||
|
||||
Assert.notNull(object, "Object must not be null!");
|
||||
|
||||
template.insert(object, getCollectionName());
|
||||
}
|
||||
|
||||
@@ -80,6 +81,7 @@ class ExecutableInsertOperationSupport implements ExecutableInsertOperation {
|
||||
public void all(Collection<? extends T> objects) {
|
||||
|
||||
Assert.notNull(objects, "Objects must not be null!");
|
||||
|
||||
template.insert(objects, getCollectionName());
|
||||
}
|
||||
|
||||
@@ -87,27 +89,29 @@ class ExecutableInsertOperationSupport implements ExecutableInsertOperation {
|
||||
public BulkWriteResult bulk(Collection<? extends T> objects) {
|
||||
|
||||
Assert.notNull(objects, "Objects must not be null!");
|
||||
|
||||
return template.bulkOps(bulkMode != null ? bulkMode : BulkMode.ORDERED, domainType, getCollectionName())
|
||||
.insert(new ArrayList<>(objects)).execute();
|
||||
}
|
||||
|
||||
@Override
|
||||
public InsertOperationWithBulkMode inCollection(String collection) {
|
||||
public InsertOperationWithBulkMode<T> inCollection(String collection) {
|
||||
|
||||
Assert.hasText(collection, "Collection must not be null nor empty.");
|
||||
return new InsertOperationSupport<T>(template, domainType, collection, bulkMode);
|
||||
|
||||
return new InsertOperationSupport<>(template, domainType, collection, bulkMode);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TerminatingBulkInsertOperation withBulkMode(BulkMode bulkMode) {
|
||||
public TerminatingBulkInsertOperation<T> withBulkMode(BulkMode bulkMode) {
|
||||
|
||||
Assert.notNull(bulkMode, "BulkMode must not be null!");
|
||||
return new InsertOperationSupport<T>(template, domainType, collection, bulkMode);
|
||||
|
||||
return new InsertOperationSupport<>(template, domainType, collection, bulkMode);
|
||||
}
|
||||
|
||||
private String getCollectionName() {
|
||||
return StringUtils.hasText(collection) ? collection : template.determineCollectionName(domainType);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -73,7 +73,6 @@ public interface ExecutableRemoveOperation {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param <T>
|
||||
* @author Christoph Strobl
|
||||
* @since 2.0
|
||||
*/
|
||||
@@ -82,7 +81,7 @@ public interface ExecutableRemoveOperation {
|
||||
/**
|
||||
* Remove all documents matching.
|
||||
*
|
||||
* @return
|
||||
* @return the {@link DeleteResult}. Never {@literal null}.
|
||||
*/
|
||||
DeleteResult all();
|
||||
|
||||
@@ -98,7 +97,6 @@ public interface ExecutableRemoveOperation {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param <T>
|
||||
* @author Christoph Strobl
|
||||
* @since 2.0
|
||||
*/
|
||||
@@ -115,11 +113,8 @@ public interface ExecutableRemoveOperation {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param <T>
|
||||
* @author Christoph Strobl
|
||||
* @since 2.0
|
||||
*/
|
||||
interface RemoveOperation<T> extends RemoveOperationWithCollection<T> {
|
||||
|
||||
}
|
||||
interface RemoveOperation<T> extends RemoveOperationWithCollection<T> {}
|
||||
}
|
||||
|
||||
@@ -46,6 +46,7 @@ class ExecutableRemoveOperationSupport implements ExecutableRemoveOperation {
|
||||
ExecutableRemoveOperationSupport(MongoTemplate template) {
|
||||
|
||||
Assert.notNull(template, "Template must not be null!");
|
||||
|
||||
this.tempate = template;
|
||||
}
|
||||
|
||||
@@ -53,11 +54,11 @@ class ExecutableRemoveOperationSupport implements ExecutableRemoveOperation {
|
||||
public <T> RemoveOperation<T> remove(Class<T> domainType) {
|
||||
|
||||
Assert.notNull(domainType, "DomainType must not be null!");
|
||||
|
||||
return new RemoveOperationSupport<>(tempate, null, domainType, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param <T>
|
||||
* @author Christoph Strobl
|
||||
* @since 2.0
|
||||
*/
|
||||
@@ -73,6 +74,7 @@ class ExecutableRemoveOperationSupport implements ExecutableRemoveOperation {
|
||||
public RemoveOperationWithQuery<T> inCollection(String collection) {
|
||||
|
||||
Assert.hasText(collection, "Collection must not be null nor empty!");
|
||||
|
||||
return new RemoveOperationSupport<>(template, query, domainType, collection);
|
||||
}
|
||||
|
||||
@@ -80,26 +82,32 @@ class ExecutableRemoveOperationSupport implements ExecutableRemoveOperation {
|
||||
public TerminatingRemoveOperation<T> matching(Query query) {
|
||||
|
||||
Assert.notNull(query, "Query must not be null!");
|
||||
|
||||
return new RemoveOperationSupport<>(template, query, domainType, collection);
|
||||
}
|
||||
|
||||
@Override
|
||||
public DeleteResult all() {
|
||||
|
||||
String collectionName = StringUtils.hasText(collection) ? collection
|
||||
: template.determineCollectionName(domainType);
|
||||
String collectionName = getCollectionName();
|
||||
|
||||
return template.doRemove(collectionName, query != null ? query : new BasicQuery(new Document()), domainType);
|
||||
return template.doRemove(collectionName, getQuery(), domainType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<T> findAndRemove() {
|
||||
|
||||
String collectionName = StringUtils.hasText(collection) ? collection
|
||||
: template.determineCollectionName(domainType);
|
||||
String collectionName = getCollectionName();
|
||||
|
||||
return template.doFindAndDelete(collectionName, query != null ? query : new BasicQuery(new Document()),
|
||||
domainType);
|
||||
return template.doFindAndDelete(collectionName, getQuery(), domainType);
|
||||
}
|
||||
|
||||
private String getCollectionName() {
|
||||
return StringUtils.hasText(collection) ? collection : template.determineCollectionName(domainType);
|
||||
}
|
||||
|
||||
private Query getQuery() {
|
||||
return query != null ? query : new BasicQuery(new Document());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -50,20 +50,21 @@ public interface ExecutableUpdateOperation {
|
||||
* Start creating an update operation for the given {@literal domainType}.
|
||||
*
|
||||
* @param domainType must not be {@literal null}.
|
||||
* @return
|
||||
* @return new instance of {@link UpdateOperation}.
|
||||
* @throws IllegalArgumentException if domainType is {@literal null}.
|
||||
*/
|
||||
<T> UpdateOperation<T> update(Class<T> domainType);
|
||||
|
||||
/**
|
||||
* @author Christoph Strobl
|
||||
* @since 2.0
|
||||
*/
|
||||
interface UpdateOperation<T>
|
||||
extends UpdateOperationWithCollection<T>, UpdateOperationWithQuery<T>, UpdateOperationWithUpdate<T> {
|
||||
|
||||
}
|
||||
extends UpdateOperationWithCollection<T>, UpdateOperationWithQuery<T>, UpdateOperationWithUpdate<T> {}
|
||||
|
||||
/**
|
||||
* Declare the {@link Update} to apply.
|
||||
*
|
||||
* @param <T>
|
||||
* @author Christoph Strobl
|
||||
* @since 2.0
|
||||
*/
|
||||
@@ -82,7 +83,6 @@ public interface ExecutableUpdateOperation {
|
||||
/**
|
||||
* Explicitly define the name of the collection to perform operation in.
|
||||
*
|
||||
* @param <T>
|
||||
* @author Christoph Strobl
|
||||
* @since 2.0
|
||||
*/
|
||||
@@ -102,7 +102,6 @@ public interface ExecutableUpdateOperation {
|
||||
/**
|
||||
* Define a filter query for the {@link Update}.
|
||||
*
|
||||
* @param <T>
|
||||
* @author Christoph Strobl
|
||||
* @since 2.0
|
||||
*/
|
||||
@@ -121,7 +120,6 @@ public interface ExecutableUpdateOperation {
|
||||
/**
|
||||
* Define {@link FindAndModifyOptions}.
|
||||
*
|
||||
* @param <T>
|
||||
* @author Christoph Strobl
|
||||
* @since 2.0
|
||||
*/
|
||||
@@ -139,8 +137,6 @@ public interface ExecutableUpdateOperation {
|
||||
|
||||
/**
|
||||
* Trigger findAndModify execution by calling one of the terminating methods.
|
||||
*
|
||||
* @param <T>
|
||||
*/
|
||||
interface TerminatingFindAndModifyOperation<T> {
|
||||
|
||||
@@ -155,7 +151,6 @@ public interface ExecutableUpdateOperation {
|
||||
/**
|
||||
* Trigger update execution by calling one of the terminating methods.
|
||||
*
|
||||
* @param <T>
|
||||
* @author Christoph Strobl
|
||||
* @since 2.0
|
||||
*/
|
||||
|
||||
@@ -46,6 +46,7 @@ class ExecutableUpdateOperationSupport implements ExecutableUpdateOperation {
|
||||
ExecutableUpdateOperationSupport(MongoTemplate template) {
|
||||
|
||||
Assert.notNull(template, "Template must not be null!");
|
||||
|
||||
this.template = template;
|
||||
}
|
||||
|
||||
@@ -53,11 +54,11 @@ class ExecutableUpdateOperationSupport implements ExecutableUpdateOperation {
|
||||
public <T> UpdateOperation<T> update(Class<T> domainType) {
|
||||
|
||||
Assert.notNull(domainType, "DomainType must not be null!");
|
||||
return new UpdateOperationSupport<T>(template, null, domainType, null, null, null);
|
||||
|
||||
return new UpdateOperationSupport<>(template, null, domainType, null, null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param <T>
|
||||
* @author Christoph Strobl
|
||||
* @since 2.0
|
||||
*/
|
||||
@@ -76,14 +77,16 @@ class ExecutableUpdateOperationSupport implements ExecutableUpdateOperation {
|
||||
public TerminatingUpdateOperation<T> apply(Update update) {
|
||||
|
||||
Assert.notNull(update, "Update must not be null!");
|
||||
return new UpdateOperationSupport<T>(template, query, domainType, update, collection, options);
|
||||
|
||||
return new UpdateOperationSupport<>(template, query, domainType, update, collection, options);
|
||||
}
|
||||
|
||||
@Override
|
||||
public UpdateOperationWithQuery<T> inCollection(String collection) {
|
||||
|
||||
Assert.hasText(collection, "Collection must not be null nor empty!");
|
||||
return new UpdateOperationSupport<T>(template, query, domainType, update, collection, options);
|
||||
|
||||
return new UpdateOperationSupport<>(template, query, domainType, update, collection, options);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -99,8 +102,7 @@ class ExecutableUpdateOperationSupport implements ExecutableUpdateOperation {
|
||||
@Override
|
||||
public Optional<T> findAndModify() {
|
||||
|
||||
String collectionName = StringUtils.hasText(collection) ? collection
|
||||
: template.determineCollectionName(domainType);
|
||||
String collectionName = getCollectionName();
|
||||
|
||||
return Optional.ofNullable(template.findAndModify(query != null ? query : new BasicQuery(new Document()), update,
|
||||
options, domainType, collectionName));
|
||||
@@ -110,7 +112,8 @@ class ExecutableUpdateOperationSupport implements ExecutableUpdateOperation {
|
||||
public UpdateOperationWithUpdate<T> matching(Query query) {
|
||||
|
||||
Assert.notNull(query, "Query must not be null!");
|
||||
return new UpdateOperationSupport<T>(template, query, domainType, update, collection, options);
|
||||
|
||||
return new UpdateOperationSupport<>(template, query, domainType, update, collection, options);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -122,17 +125,21 @@ class ExecutableUpdateOperationSupport implements ExecutableUpdateOperation {
|
||||
public TerminatingFindAndModifyOperation<T> withOptions(FindAndModifyOptions options) {
|
||||
|
||||
Assert.notNull(options, "Options must not be null!");
|
||||
return new UpdateOperationSupport<T>(template, query, domainType, update, collection, options);
|
||||
|
||||
return new UpdateOperationSupport<>(template, query, domainType, update, collection, options);
|
||||
}
|
||||
|
||||
private UpdateResult doUpdate(boolean multi, boolean upsert) {
|
||||
|
||||
String collectionName = StringUtils.hasText(collection) ? collection
|
||||
: template.determineCollectionName(domainType);
|
||||
String collectionName = getCollectionName();
|
||||
|
||||
Query query = this.query != null ? this.query : new BasicQuery(new Document());
|
||||
|
||||
return template.doUpdate(collectionName, query, update, domainType, upsert, multi);
|
||||
}
|
||||
|
||||
private String getCollectionName() {
|
||||
return StringUtils.hasText(collection) ? collection : template.determineCollectionName(domainType);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,6 +22,4 @@ package org.springframework.data.mongodb.core;
|
||||
* @since 2.0
|
||||
*/
|
||||
public interface FluentMongoOperations extends ExecutableFindOperation, ExecutableInsertOperation,
|
||||
ExecutableUpdateOperation, ExecutableRemoveOperation, ExecutableAggregationOperation {
|
||||
|
||||
}
|
||||
ExecutableUpdateOperation, ExecutableRemoveOperation, ExecutableAggregationOperation {}
|
||||
|
||||
@@ -1646,8 +1646,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware,
|
||||
return aggregateStream(aggregation, collectionName, outputType, null);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.mongodb.core.MongoOperations#findAllAndRemove(org.springframework.data.mongodb.core.query.Query, java.lang.String)
|
||||
*/
|
||||
@Override
|
||||
@@ -1655,8 +1654,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware,
|
||||
return findAndRemove(query, null, collectionName);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.mongodb.core.MongoOperations#findAllAndRemove(org.springframework.data.mongodb.core.query.Query, java.lang.Class)
|
||||
*/
|
||||
@Override
|
||||
@@ -1664,8 +1662,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware,
|
||||
return findAllAndRemove(query, entityClass, determineCollectionName(entityClass));
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.mongodb.core.MongoOperations#findAllAndRemove(org.springframework.data.mongodb.core.query.Query, java.lang.Class, java.lang.String)
|
||||
*/
|
||||
@Override
|
||||
@@ -1786,26 +1783,46 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware,
|
||||
});
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.mongodb.core.ExecutableFindOperation#query(java.lang.Class)
|
||||
*/
|
||||
@Override
|
||||
public <T> FindOperation<T> query(Class<T> domainType) {
|
||||
return new ExecutableFindOperationSupport(this).query(domainType);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.mongodb.core.ExecutableUpdateOperation#update(java.lang.Class)
|
||||
*/
|
||||
@Override
|
||||
public <T> UpdateOperation<T> update(Class<T> domainType) {
|
||||
return new ExecutableUpdateOperationSupport(this).update(domainType);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.mongodb.core.ExecutableRemoveOperation#remove(java.lang.Class)
|
||||
*/
|
||||
@Override
|
||||
public <T> RemoveOperation<T> remove(Class<T> domainType) {
|
||||
return new ExecutableRemoveOperationSupport(this).remove(domainType);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.mongodb.core.ExecutableAggregationOperation#aggregateAndReturn(java.lang.Class)
|
||||
*/
|
||||
@Override
|
||||
public <T> AggregationOperation<T> aggregateAndReturn(Class<T> domainType) {
|
||||
return new ExecutableAggregationOperationSupport(this).aggregateAndReturn(domainType);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.mongodb.core.ExecutableInsertOperation#insert(java.lang.Class)
|
||||
*/
|
||||
@Override
|
||||
public <T> InsertOperation<T> insert(Class<T> domainType) {
|
||||
return new ExecutableInsertOperationSupport(this).insert(domainType);
|
||||
@@ -1997,15 +2014,6 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware,
|
||||
* Map the results of an ad-hoc query on the default MongoDB collection to a List of the specified targetClass while
|
||||
* using sourceClass for mapping the query.
|
||||
*
|
||||
* @param collectionName
|
||||
* @param query
|
||||
* @param fields
|
||||
* @param sourceClass
|
||||
* @param targetClass
|
||||
* @param objectCallback
|
||||
* @param <S>
|
||||
* @param <T>
|
||||
* @return
|
||||
* @since 2.0
|
||||
*/
|
||||
<S, T> List<T> doFind(String collectionName, Document query, Document fields, Class<S> sourceClass,
|
||||
@@ -2253,11 +2261,11 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware,
|
||||
}
|
||||
|
||||
private Optional<? extends MongoPersistentEntity<?>> getPersistentEntity(Class<?> type) {
|
||||
return Optional.ofNullable(type).flatMap(it -> mappingContext.getPersistentEntity(it));
|
||||
return Optional.ofNullable(type).flatMap(mappingContext::getPersistentEntity);
|
||||
}
|
||||
|
||||
private Optional<MongoPersistentProperty> getIdPropertyFor(Class<?> type) {
|
||||
return mappingContext.getPersistentEntity(type).flatMap(it -> it.getIdProperty());
|
||||
return mappingContext.getPersistentEntity(type).flatMap(PersistentEntity::getIdProperty);
|
||||
}
|
||||
|
||||
private <T> String determineEntityCollectionName(T obj) {
|
||||
|
||||
@@ -30,6 +30,8 @@ import org.mockito.junit.MockitoJUnitRunner;
|
||||
import org.springframework.data.mongodb.core.aggregation.Aggregation;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link ExecutableAggregationOperationSupport}.
|
||||
*
|
||||
* @author Christoph Strobl
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
@@ -66,7 +68,7 @@ public class ExecutableAggregationOperationSupportUnitTests {
|
||||
@Test // DATAMONGO-1563
|
||||
public void aggregateWithUntypedAggregationAndExplicitCollection() {
|
||||
|
||||
opSupport.aggregateAndReturn(Person.class).inCollection("star-wars").by(newAggregation(project("foo"))).get();
|
||||
opSupport.aggregateAndReturn(Person.class).inCollection("star-wars").by(newAggregation(project("foo"))).all();
|
||||
|
||||
ArgumentCaptor<Class> captor = ArgumentCaptor.forClass(Class.class);
|
||||
verify(template).aggregate(any(Aggregation.class), eq("star-wars"), captor.capture());
|
||||
@@ -78,7 +80,7 @@ public class ExecutableAggregationOperationSupportUnitTests {
|
||||
|
||||
when(template.determineCollectionName(any(Class.class))).thenReturn("person");
|
||||
|
||||
opSupport.aggregateAndReturn(Person.class).by(newAggregation(project("foo"))).get();
|
||||
opSupport.aggregateAndReturn(Person.class).by(newAggregation(project("foo"))).all();
|
||||
|
||||
ArgumentCaptor<Class> captor = ArgumentCaptor.forClass(Class.class);
|
||||
|
||||
@@ -93,7 +95,7 @@ public class ExecutableAggregationOperationSupportUnitTests {
|
||||
|
||||
when(template.determineCollectionName(any(Class.class))).thenReturn("person");
|
||||
|
||||
opSupport.aggregateAndReturn(Jedi.class).by(newAggregation(Person.class, project("foo"))).get();
|
||||
opSupport.aggregateAndReturn(Jedi.class).by(newAggregation(Person.class, project("foo"))).all();
|
||||
|
||||
ArgumentCaptor<Class> captor = ArgumentCaptor.forClass(Class.class);
|
||||
|
||||
@@ -143,10 +145,7 @@ public class ExecutableAggregationOperationSupportUnitTests {
|
||||
assertThat(captor.getAllValues()).containsExactly(Person.class, Jedi.class);
|
||||
}
|
||||
|
||||
static class Person {
|
||||
|
||||
}
|
||||
static class Person {}
|
||||
|
||||
static class Jedi {}
|
||||
|
||||
}
|
||||
|
||||
@@ -37,7 +37,10 @@ import org.springframework.data.util.CloseableIterator;
|
||||
import com.mongodb.MongoClient;
|
||||
|
||||
/**
|
||||
* Integration tests for {@link ExecutableFindOperationSupport}.
|
||||
*
|
||||
* @author Christoph Strobl
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
public class ExecutableFindOperationSupportTests {
|
||||
|
||||
@@ -92,9 +95,7 @@ public class ExecutableFindOperationSupportTests {
|
||||
|
||||
@Test // DATAMONGO-1563
|
||||
public void findAllWithProjection() {
|
||||
|
||||
assertThat(template.query(Person.class).as(Jedi.class).all()).hasOnlyElementsOfType(Jedi.class)
|
||||
.hasSize(2);
|
||||
assertThat(template.query(Person.class).as(Jedi.class).all()).hasOnlyElementsOfType(Jedi.class).hasSize(2);
|
||||
}
|
||||
|
||||
@Test // DATAMONGO-1563
|
||||
@@ -107,8 +108,8 @@ public class ExecutableFindOperationSupportTests {
|
||||
@Test // DATAMONGO-1563
|
||||
public void findAllByWithCollectionUsingMappingInformation() {
|
||||
|
||||
assertThat(template.query(Jedi.class).inCollection(STAR_WARS).matching(query(where("name").is("luke"))).all()).hasSize(1)
|
||||
.hasOnlyElementsOfType(Jedi.class);
|
||||
assertThat(template.query(Jedi.class).inCollection(STAR_WARS).matching(query(where("name").is("luke"))).all())
|
||||
.hasSize(1).hasOnlyElementsOfType(Jedi.class);
|
||||
}
|
||||
|
||||
@Test // DATAMONGO-1563
|
||||
@@ -166,8 +167,8 @@ public class ExecutableFindOperationSupportTests {
|
||||
@Test // DATAMONGO-1563
|
||||
public void streamAllBy() {
|
||||
|
||||
try (CloseableIterator<Person> stream = template.query(Person.class)
|
||||
.matching(query(where("firstname").is("luke"))).stream()) {
|
||||
try (CloseableIterator<Person> stream = template.query(Person.class).matching(query(where("firstname").is("luke")))
|
||||
.stream()) {
|
||||
|
||||
assertThat(stream).containsExactlyInAnyOrder(luke);
|
||||
}
|
||||
@@ -185,8 +186,8 @@ public class ExecutableFindOperationSupportTests {
|
||||
template.save(alderan);
|
||||
template.save(dantooine);
|
||||
|
||||
GeoResults<Planet> results = template.query(Planet.class)
|
||||
.near(NearQuery.near(-73.9667, 40.78).spherical(true)).all();
|
||||
GeoResults<Planet> results = template.query(Planet.class).near(NearQuery.near(-73.9667, 40.78).spherical(true))
|
||||
.all();
|
||||
assertThat(results.getContent()).hasSize(2);
|
||||
assertThat(results.getContent().get(0).getDistance()).isNotNull();
|
||||
}
|
||||
@@ -238,5 +239,4 @@ public class ExecutableFindOperationSupportTests {
|
||||
@Id String name;
|
||||
Point coordinates;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -35,10 +35,12 @@ import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.mongodb.core.BulkOperations.BulkMode;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link ExecutableInsertOperationSupport}.
|
||||
*
|
||||
* @author Christoph Strobl
|
||||
* @since 2017/06
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.Silent.class)
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class ExecutableInsertOperationSupportUnitTests {
|
||||
|
||||
private static final String STAR_WARS = "star-wars";
|
||||
|
||||
@@ -32,7 +32,10 @@ import com.mongodb.MongoClient;
|
||||
import com.mongodb.client.result.DeleteResult;
|
||||
|
||||
/**
|
||||
* Integration tests for {@link ExecutableRemoveOperationSupport}.
|
||||
*
|
||||
* @author Christoph Strobl
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
public class ExecutableRemoveOperationSupportTests {
|
||||
|
||||
|
||||
@@ -35,7 +35,10 @@ import com.mongodb.MongoClient;
|
||||
import com.mongodb.client.result.UpdateResult;
|
||||
|
||||
/**
|
||||
* Integration tests for {@link ExecutableUpdateOperationSupport}.
|
||||
*
|
||||
* @author Christoph Strobl
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
public class ExecutableUpdateOperationSupportTests {
|
||||
|
||||
@@ -186,5 +189,4 @@ public class ExecutableUpdateOperationSupportTests {
|
||||
|
||||
@Field("firstname") String name;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user