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:
Mark Paluch
2017-06-13 10:25:12 +02:00
parent c5f2abe037
commit deed19187f
17 changed files with 136 additions and 124 deletions

View File

@@ -36,6 +36,7 @@ import org.springframework.data.util.CloseableIterator;
* </pre> * </pre>
* *
* @author Christoph Strobl * @author Christoph Strobl
* @author Mark Paluch
* @since 2.0 * @since 2.0
*/ */
public interface ExecutableAggregationOperation { public interface ExecutableAggregationOperation {
@@ -54,7 +55,6 @@ public interface ExecutableAggregationOperation {
/** /**
* Collection override (Optional). * Collection override (Optional).
* *
* @param <T>
* @author Christoph Strobl * @author Christoph Strobl
* @since 2.0 * @since 2.0
*/ */
@@ -74,19 +74,20 @@ public interface ExecutableAggregationOperation {
/** /**
* Trigger execution by calling one of the terminating methods. * Trigger execution by calling one of the terminating methods.
* *
* @param <T> * @author Christoph Strobl
* @since 2.0
*/ */
interface TerminatingAggregationOperation<T> { interface TerminatingAggregationOperation<T> {
/** /**
* Apply pipeline operations as specified. * Apply pipeline operations as specified and get all matching elements.
* *
* @return never {@literal null}. * @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} * 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. * @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. * Define the aggregation with pipeline stages.
* *
* @param <T>
* @author Christoph Strobl * @author Christoph Strobl
* @since 2.0 * @since 2.0
*/ */
@@ -115,7 +115,6 @@ public interface ExecutableAggregationOperation {
} }
/** /**
* @param <T>
* @author Christoph Strobl * @author Christoph Strobl
* @since 2.0 * @since 2.0
*/ */

View File

@@ -35,7 +35,7 @@ class ExecutableAggregationOperationSupport implements ExecutableAggregationOper
private final MongoTemplate template; private final MongoTemplate template;
/** /**
* Create new instance of ExecutableAggregationOperationSupport. * Create new instance of {@link ExecutableAggregationOperationSupport}.
* *
* @param template must not be {@literal null}. * @param template must not be {@literal null}.
* @throws IllegalArgumentException if template is {@literal null}. * @throws IllegalArgumentException if template is {@literal null}.
@@ -43,6 +43,7 @@ class ExecutableAggregationOperationSupport implements ExecutableAggregationOper
ExecutableAggregationOperationSupport(MongoTemplate template) { ExecutableAggregationOperationSupport(MongoTemplate template) {
Assert.notNull(template, "Template must not be null!"); Assert.notNull(template, "Template must not be null!");
this.template = template; this.template = template;
} }
@@ -50,11 +51,11 @@ class ExecutableAggregationOperationSupport implements ExecutableAggregationOper
public <T> AggregationOperation<T> aggregateAndReturn(Class<T> domainType) { public <T> AggregationOperation<T> aggregateAndReturn(Class<T> domainType) {
Assert.notNull(domainType, "DomainType must not be null!"); 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 * @author Christoph Strobl
* @since 2.0 * @since 2.0
*/ */
@@ -71,18 +72,20 @@ class ExecutableAggregationOperationSupport implements ExecutableAggregationOper
public AggregationOperationWithAggregation<T> inCollection(String collection) { public AggregationOperationWithAggregation<T> inCollection(String collection) {
Assert.hasText(collection, "Collection must not be null nor empty!"); 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 @Override
public TerminatingAggregationOperation<T> by(Aggregation aggregation) { public TerminatingAggregationOperation<T> by(Aggregation aggregation) {
Assert.notNull(aggregation, "Aggregation must not be null!"); Assert.notNull(aggregation, "Aggregation must not be null!");
return new AggregationOperationSupport<T>(template, aggregation, domainType, collection);
return new AggregationOperationSupport<>(template, aggregation, domainType, collection);
} }
@Override @Override
public AggregationResults<T> get() { public AggregationResults<T> all() {
return template.aggregate(aggregation, getCollectionName(aggregation), domainType); return template.aggregate(aggregation, getCollectionName(aggregation), domainType);
} }
@@ -99,8 +102,10 @@ class ExecutableAggregationOperationSupport implements ExecutableAggregationOper
if (aggregation instanceof TypedAggregation) { if (aggregation instanceof TypedAggregation) {
if (((TypedAggregation<?>) aggregation).getInputType() != null) { TypedAggregation<?> typedAggregation = (TypedAggregation<?>) aggregation;
return template.determineCollectionName(((TypedAggregation<?>) aggregation).getInputType());
if (typedAggregation.getInputType() != null) {
return template.determineCollectionName(typedAggregation.getInputType());
} }
} }

View File

@@ -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. * {@link ExecutableFindOperation} allows creation and execution of MongoDB find operations in a fluent API style.
* <br /> * <br />
* The starting {@literal domainType} is used for mapping the {@link Query} provided via {@code matching} into the * 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 * 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 * 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 /> * {@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 * 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 * via {@link org.springframework.data.mongodb.core.mapping.Document}. Using {@code inCollection} allows to override the
* collection name for the execution. * collection name for the execution.
@@ -45,6 +45,7 @@ import org.springframework.data.util.CloseableIterator;
* </pre> * </pre>
* *
* @author Christoph Strobl * @author Christoph Strobl
* @author Mark Paluch
* @since 2.0 * @since 2.0
*/ */
public interface ExecutableFindOperation { public interface ExecutableFindOperation {
@@ -61,7 +62,6 @@ public interface ExecutableFindOperation {
/** /**
* Trigger find execution by calling one of the terminating methods. * Trigger find execution by calling one of the terminating methods.
* *
* @param <T>
* @author Christoph Strobl * @author Christoph Strobl
* @since 2.0 * @since 2.0
*/ */
@@ -85,7 +85,7 @@ public interface ExecutableFindOperation {
/** /**
* Get all matching elements. * Get all matching elements.
* *
* @return never {@literal}. * @return never {@literal null}.
*/ */
List<T> all(); List<T> all();
@@ -101,7 +101,6 @@ public interface ExecutableFindOperation {
/** /**
* Trigger geonear execution by calling one of the terminating methods. * Trigger geonear execution by calling one of the terminating methods.
* *
* @param <T>
* @author Christoph Strobl * @author Christoph Strobl
* @since 2.0 * @since 2.0
*/ */
@@ -118,7 +117,6 @@ public interface ExecutableFindOperation {
/** /**
* Terminating operations invoking the actual query execution. * Terminating operations invoking the actual query execution.
* *
* @param <T>
* @author Christoph Strobl * @author Christoph Strobl
* @since 2.0 * @since 2.0
*/ */
@@ -146,7 +144,6 @@ public interface ExecutableFindOperation {
/** /**
* Collection override (Optional). * Collection override (Optional).
* *
* @param <T>
* @author Christoph Strobl * @author Christoph Strobl
* @since 2.0 * @since 2.0
*/ */
@@ -166,7 +163,6 @@ public interface ExecutableFindOperation {
/** /**
* Result type override (Optional). * Result type override (Optional).
* *
* @param <T>
* @author Christoph Strobl * @author Christoph Strobl
* @since 2.0 * @since 2.0
*/ */
@@ -187,11 +183,8 @@ public interface ExecutableFindOperation {
/** /**
* {@link FindOperation} provides methods for constructing lookup operations in a fluent way. * {@link FindOperation} provides methods for constructing lookup operations in a fluent way.
* *
* @param <T>
* @author Christoph Strobl * @author Christoph Strobl
* @since 2.0 * @since 2.0
*/ */
interface FindOperation<T> extends FindOperationWithCollection<T>, FindOperationWithProjection<T> { interface FindOperation<T> extends FindOperationWithCollection<T>, FindOperationWithProjection<T> {}
}
} }

View File

@@ -123,6 +123,7 @@ class ExecutableFindOperationSupport implements ExecutableFindOperation {
public Optional<T> first() { public Optional<T> first() {
List<T> result = doFind(new DelegatingQueryCursorPreparer(getCursorPreparer(query, null)).limit(1)); List<T> result = doFind(new DelegatingQueryCursorPreparer(getCursorPreparer(query, null)).limit(1));
return ObjectUtils.isEmpty(result) ? Optional.empty() : Optional.of(result.iterator().next()); return ObjectUtils.isEmpty(result) ? Optional.empty() : Optional.of(result.iterator().next());
} }
@@ -170,7 +171,6 @@ class ExecutableFindOperationSupport implements ExecutableFindOperation {
} }
/** /**
* @param <T>
* @author Christoph Strobl * @author Christoph Strobl
* @since 2.0 * @since 2.0
*/ */
@@ -179,7 +179,7 @@ class ExecutableFindOperationSupport implements ExecutableFindOperation {
private final CursorPreparer delegate; private final CursorPreparer delegate;
private Optional<Integer> limit = Optional.empty(); private Optional<Integer> limit = Optional.empty();
public DelegatingQueryCursorPreparer(CursorPreparer delegate) { DelegatingQueryCursorPreparer(CursorPreparer delegate) {
this.delegate = delegate; this.delegate = delegate;
} }
@@ -187,7 +187,8 @@ class ExecutableFindOperationSupport implements ExecutableFindOperation {
public FindIterable<Document> prepare(FindIterable<Document> cursor) { public FindIterable<Document> prepare(FindIterable<Document> cursor) {
FindIterable<Document> target = delegate.prepare(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) { CursorPreparer limit(int limit) {

View File

@@ -22,7 +22,7 @@ import org.springframework.data.mongodb.core.BulkOperations.BulkMode;
import com.mongodb.bulk.BulkWriteResult; 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 /> * fluent API style. <br />
* The collection to operate on is by default derived from the initial {@literal domainType} and can be defined there * 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 * 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. * Trigger insert execution by calling one of the terminating methods.
* *
* @param <T>
* @author Christoph Strobl * @author Christoph Strobl
* @since 2.0 * @since 2.0
*/ */
@@ -79,7 +78,6 @@ public interface ExecutableInsertOperation {
/** /**
* Trigger bulk insert execution by calling one of the terminating methods. * Trigger bulk insert execution by calling one of the terminating methods.
* *
* @param <T>
* @author Christoph Strobl * @author Christoph Strobl
* @since 2.0 * @since 2.0
*/ */
@@ -96,19 +94,15 @@ public interface ExecutableInsertOperation {
} }
/** /**
* @param <T>
* @author Christoph Strobl * @author Christoph Strobl
* @since 2.0 * @since 2.0
*/ */
interface InsertOperation<T> interface InsertOperation<T>
extends TerminatingInsertOperation<T>, InsertOperationWithCollection<T>, InsertOperationWithBulkMode<T> { extends TerminatingInsertOperation<T>, InsertOperationWithCollection<T>, InsertOperationWithBulkMode<T> {}
}
/** /**
* Collection override (Optional). * Collection override (Optional).
* *
* @param <T>
* @author Christoph Strobl * @author Christoph Strobl
* @since 2.0 * @since 2.0
*/ */
@@ -126,7 +120,6 @@ public interface ExecutableInsertOperation {
} }
/** /**
* @param <T>
* @author Christoph Strobl * @author Christoph Strobl
* @since 2.0 * @since 2.0
*/ */
@@ -135,7 +128,7 @@ public interface ExecutableInsertOperation {
/** /**
* Define the {@link BulkMode} to use for bulk insert operation. * 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}. * @return new instance of {@link TerminatingBulkInsertOperation}.
* @throws IllegalArgumentException if bulkMode is {@literal null}. * @throws IllegalArgumentException if bulkMode is {@literal null}.
*/ */

View File

@@ -53,11 +53,11 @@ class ExecutableInsertOperationSupport implements ExecutableInsertOperation {
public <T> InsertOperation<T> insert(Class<T> domainType) { public <T> InsertOperation<T> insert(Class<T> domainType) {
Assert.notNull(domainType, "DomainType must not be null!"); 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 * @author Christoph Strobl
* @since 2.0 * @since 2.0
*/ */
@@ -73,6 +73,7 @@ class ExecutableInsertOperationSupport implements ExecutableInsertOperation {
public void one(T object) { public void one(T object) {
Assert.notNull(object, "Object must not be null!"); Assert.notNull(object, "Object must not be null!");
template.insert(object, getCollectionName()); template.insert(object, getCollectionName());
} }
@@ -80,6 +81,7 @@ class ExecutableInsertOperationSupport implements ExecutableInsertOperation {
public void all(Collection<? extends T> objects) { public void all(Collection<? extends T> objects) {
Assert.notNull(objects, "Objects must not be null!"); Assert.notNull(objects, "Objects must not be null!");
template.insert(objects, getCollectionName()); template.insert(objects, getCollectionName());
} }
@@ -87,27 +89,29 @@ class ExecutableInsertOperationSupport implements ExecutableInsertOperation {
public BulkWriteResult bulk(Collection<? extends T> objects) { public BulkWriteResult bulk(Collection<? extends T> objects) {
Assert.notNull(objects, "Objects must not be null!"); Assert.notNull(objects, "Objects must not be null!");
return template.bulkOps(bulkMode != null ? bulkMode : BulkMode.ORDERED, domainType, getCollectionName()) return template.bulkOps(bulkMode != null ? bulkMode : BulkMode.ORDERED, domainType, getCollectionName())
.insert(new ArrayList<>(objects)).execute(); .insert(new ArrayList<>(objects)).execute();
} }
@Override @Override
public InsertOperationWithBulkMode inCollection(String collection) { public InsertOperationWithBulkMode<T> inCollection(String collection) {
Assert.hasText(collection, "Collection must not be null nor empty."); 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 @Override
public TerminatingBulkInsertOperation withBulkMode(BulkMode bulkMode) { public TerminatingBulkInsertOperation<T> withBulkMode(BulkMode bulkMode) {
Assert.notNull(bulkMode, "BulkMode must not be null!"); 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() { private String getCollectionName() {
return StringUtils.hasText(collection) ? collection : template.determineCollectionName(domainType); return StringUtils.hasText(collection) ? collection : template.determineCollectionName(domainType);
} }
} }
} }

View File

@@ -73,7 +73,6 @@ public interface ExecutableRemoveOperation {
} }
/** /**
* @param <T>
* @author Christoph Strobl * @author Christoph Strobl
* @since 2.0 * @since 2.0
*/ */
@@ -82,7 +81,7 @@ public interface ExecutableRemoveOperation {
/** /**
* Remove all documents matching. * Remove all documents matching.
* *
* @return * @return the {@link DeleteResult}. Never {@literal null}.
*/ */
DeleteResult all(); DeleteResult all();
@@ -98,7 +97,6 @@ public interface ExecutableRemoveOperation {
} }
/** /**
* @param <T>
* @author Christoph Strobl * @author Christoph Strobl
* @since 2.0 * @since 2.0
*/ */
@@ -115,11 +113,8 @@ public interface ExecutableRemoveOperation {
} }
/** /**
* @param <T>
* @author Christoph Strobl * @author Christoph Strobl
* @since 2.0 * @since 2.0
*/ */
interface RemoveOperation<T> extends RemoveOperationWithCollection<T> { interface RemoveOperation<T> extends RemoveOperationWithCollection<T> {}
}
} }

View File

@@ -46,6 +46,7 @@ class ExecutableRemoveOperationSupport implements ExecutableRemoveOperation {
ExecutableRemoveOperationSupport(MongoTemplate template) { ExecutableRemoveOperationSupport(MongoTemplate template) {
Assert.notNull(template, "Template must not be null!"); Assert.notNull(template, "Template must not be null!");
this.tempate = template; this.tempate = template;
} }
@@ -53,11 +54,11 @@ class ExecutableRemoveOperationSupport implements ExecutableRemoveOperation {
public <T> RemoveOperation<T> remove(Class<T> domainType) { public <T> RemoveOperation<T> remove(Class<T> domainType) {
Assert.notNull(domainType, "DomainType must not be null!"); Assert.notNull(domainType, "DomainType must not be null!");
return new RemoveOperationSupport<>(tempate, null, domainType, null); return new RemoveOperationSupport<>(tempate, null, domainType, null);
} }
/** /**
* @param <T>
* @author Christoph Strobl * @author Christoph Strobl
* @since 2.0 * @since 2.0
*/ */
@@ -73,6 +74,7 @@ class ExecutableRemoveOperationSupport implements ExecutableRemoveOperation {
public RemoveOperationWithQuery<T> inCollection(String collection) { public RemoveOperationWithQuery<T> inCollection(String collection) {
Assert.hasText(collection, "Collection must not be null nor empty!"); Assert.hasText(collection, "Collection must not be null nor empty!");
return new RemoveOperationSupport<>(template, query, domainType, collection); return new RemoveOperationSupport<>(template, query, domainType, collection);
} }
@@ -80,26 +82,32 @@ class ExecutableRemoveOperationSupport implements ExecutableRemoveOperation {
public TerminatingRemoveOperation<T> matching(Query query) { public TerminatingRemoveOperation<T> matching(Query query) {
Assert.notNull(query, "Query must not be null!"); Assert.notNull(query, "Query must not be null!");
return new RemoveOperationSupport<>(template, query, domainType, collection); return new RemoveOperationSupport<>(template, query, domainType, collection);
} }
@Override @Override
public DeleteResult all() { public DeleteResult all() {
String collectionName = StringUtils.hasText(collection) ? collection String collectionName = getCollectionName();
: template.determineCollectionName(domainType);
return template.doRemove(collectionName, query != null ? query : new BasicQuery(new Document()), domainType); return template.doRemove(collectionName, getQuery(), domainType);
} }
@Override @Override
public List<T> findAndRemove() { public List<T> findAndRemove() {
String collectionName = StringUtils.hasText(collection) ? collection String collectionName = getCollectionName();
: template.determineCollectionName(domainType);
return template.doFindAndDelete(collectionName, query != null ? query : new BasicQuery(new Document()), return template.doFindAndDelete(collectionName, getQuery(), domainType);
domainType); }
private String getCollectionName() {
return StringUtils.hasText(collection) ? collection : template.determineCollectionName(domainType);
}
private Query getQuery() {
return query != null ? query : new BasicQuery(new Document());
} }
} }
} }

View File

@@ -50,20 +50,21 @@ public interface ExecutableUpdateOperation {
* Start creating an update operation for the given {@literal domainType}. * Start creating an update operation for the given {@literal domainType}.
* *
* @param domainType must not be {@literal null}. * @param domainType must not be {@literal null}.
* @return * @return new instance of {@link UpdateOperation}.
* @throws IllegalArgumentException if domainType is {@literal null}. * @throws IllegalArgumentException if domainType is {@literal null}.
*/ */
<T> UpdateOperation<T> update(Class<T> domainType); <T> UpdateOperation<T> update(Class<T> domainType);
/**
* @author Christoph Strobl
* @since 2.0
*/
interface UpdateOperation<T> interface UpdateOperation<T>
extends UpdateOperationWithCollection<T>, UpdateOperationWithQuery<T>, UpdateOperationWithUpdate<T> { extends UpdateOperationWithCollection<T>, UpdateOperationWithQuery<T>, UpdateOperationWithUpdate<T> {}
}
/** /**
* Declare the {@link Update} to apply. * Declare the {@link Update} to apply.
* *
* @param <T>
* @author Christoph Strobl * @author Christoph Strobl
* @since 2.0 * @since 2.0
*/ */
@@ -82,7 +83,6 @@ public interface ExecutableUpdateOperation {
/** /**
* Explicitly define the name of the collection to perform operation in. * Explicitly define the name of the collection to perform operation in.
* *
* @param <T>
* @author Christoph Strobl * @author Christoph Strobl
* @since 2.0 * @since 2.0
*/ */
@@ -102,7 +102,6 @@ public interface ExecutableUpdateOperation {
/** /**
* Define a filter query for the {@link Update}. * Define a filter query for the {@link Update}.
* *
* @param <T>
* @author Christoph Strobl * @author Christoph Strobl
* @since 2.0 * @since 2.0
*/ */
@@ -121,7 +120,6 @@ public interface ExecutableUpdateOperation {
/** /**
* Define {@link FindAndModifyOptions}. * Define {@link FindAndModifyOptions}.
* *
* @param <T>
* @author Christoph Strobl * @author Christoph Strobl
* @since 2.0 * @since 2.0
*/ */
@@ -139,8 +137,6 @@ public interface ExecutableUpdateOperation {
/** /**
* Trigger findAndModify execution by calling one of the terminating methods. * Trigger findAndModify execution by calling one of the terminating methods.
*
* @param <T>
*/ */
interface TerminatingFindAndModifyOperation<T> { interface TerminatingFindAndModifyOperation<T> {
@@ -155,7 +151,6 @@ public interface ExecutableUpdateOperation {
/** /**
* Trigger update execution by calling one of the terminating methods. * Trigger update execution by calling one of the terminating methods.
* *
* @param <T>
* @author Christoph Strobl * @author Christoph Strobl
* @since 2.0 * @since 2.0
*/ */

View File

@@ -46,6 +46,7 @@ class ExecutableUpdateOperationSupport implements ExecutableUpdateOperation {
ExecutableUpdateOperationSupport(MongoTemplate template) { ExecutableUpdateOperationSupport(MongoTemplate template) {
Assert.notNull(template, "Template must not be null!"); Assert.notNull(template, "Template must not be null!");
this.template = template; this.template = template;
} }
@@ -53,11 +54,11 @@ class ExecutableUpdateOperationSupport implements ExecutableUpdateOperation {
public <T> UpdateOperation<T> update(Class<T> domainType) { public <T> UpdateOperation<T> update(Class<T> domainType) {
Assert.notNull(domainType, "DomainType must not be null!"); 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 * @author Christoph Strobl
* @since 2.0 * @since 2.0
*/ */
@@ -76,14 +77,16 @@ class ExecutableUpdateOperationSupport implements ExecutableUpdateOperation {
public TerminatingUpdateOperation<T> apply(Update update) { public TerminatingUpdateOperation<T> apply(Update update) {
Assert.notNull(update, "Update must not be null!"); 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 @Override
public UpdateOperationWithQuery<T> inCollection(String collection) { public UpdateOperationWithQuery<T> inCollection(String collection) {
Assert.hasText(collection, "Collection must not be null nor empty!"); 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 @Override
@@ -99,8 +102,7 @@ class ExecutableUpdateOperationSupport implements ExecutableUpdateOperation {
@Override @Override
public Optional<T> findAndModify() { public Optional<T> findAndModify() {
String collectionName = StringUtils.hasText(collection) ? collection String collectionName = getCollectionName();
: template.determineCollectionName(domainType);
return Optional.ofNullable(template.findAndModify(query != null ? query : new BasicQuery(new Document()), update, return Optional.ofNullable(template.findAndModify(query != null ? query : new BasicQuery(new Document()), update,
options, domainType, collectionName)); options, domainType, collectionName));
@@ -110,7 +112,8 @@ class ExecutableUpdateOperationSupport implements ExecutableUpdateOperation {
public UpdateOperationWithUpdate<T> matching(Query query) { public UpdateOperationWithUpdate<T> matching(Query query) {
Assert.notNull(query, "Query must not be null!"); 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 @Override
@@ -122,17 +125,21 @@ class ExecutableUpdateOperationSupport implements ExecutableUpdateOperation {
public TerminatingFindAndModifyOperation<T> withOptions(FindAndModifyOptions options) { public TerminatingFindAndModifyOperation<T> withOptions(FindAndModifyOptions options) {
Assert.notNull(options, "Options must not be null!"); 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) { private UpdateResult doUpdate(boolean multi, boolean upsert) {
String collectionName = StringUtils.hasText(collection) ? collection String collectionName = getCollectionName();
: template.determineCollectionName(domainType);
Query query = this.query != null ? this.query : new BasicQuery(new Document()); Query query = this.query != null ? this.query : new BasicQuery(new Document());
return template.doUpdate(collectionName, query, update, domainType, upsert, multi); return template.doUpdate(collectionName, query, update, domainType, upsert, multi);
} }
private String getCollectionName() {
return StringUtils.hasText(collection) ? collection : template.determineCollectionName(domainType);
}
} }
} }

View File

@@ -22,6 +22,4 @@ package org.springframework.data.mongodb.core;
* @since 2.0 * @since 2.0
*/ */
public interface FluentMongoOperations extends ExecutableFindOperation, ExecutableInsertOperation, public interface FluentMongoOperations extends ExecutableFindOperation, ExecutableInsertOperation,
ExecutableUpdateOperation, ExecutableRemoveOperation, ExecutableAggregationOperation { ExecutableUpdateOperation, ExecutableRemoveOperation, ExecutableAggregationOperation {}
}

View File

@@ -1646,8 +1646,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware,
return aggregateStream(aggregation, collectionName, outputType, null); 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) * @see org.springframework.data.mongodb.core.MongoOperations#findAllAndRemove(org.springframework.data.mongodb.core.query.Query, java.lang.String)
*/ */
@Override @Override
@@ -1655,8 +1654,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware,
return findAndRemove(query, null, collectionName); 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) * @see org.springframework.data.mongodb.core.MongoOperations#findAllAndRemove(org.springframework.data.mongodb.core.query.Query, java.lang.Class)
*/ */
@Override @Override
@@ -1664,8 +1662,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware,
return findAllAndRemove(query, entityClass, determineCollectionName(entityClass)); 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) * @see org.springframework.data.mongodb.core.MongoOperations#findAllAndRemove(org.springframework.data.mongodb.core.query.Query, java.lang.Class, java.lang.String)
*/ */
@Override @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 @Override
public <T> FindOperation<T> query(Class<T> domainType) { public <T> FindOperation<T> query(Class<T> domainType) {
return new ExecutableFindOperationSupport(this).query(domainType); return new ExecutableFindOperationSupport(this).query(domainType);
} }
/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.core.ExecutableUpdateOperation#update(java.lang.Class)
*/
@Override @Override
public <T> UpdateOperation<T> update(Class<T> domainType) { public <T> UpdateOperation<T> update(Class<T> domainType) {
return new ExecutableUpdateOperationSupport(this).update(domainType); return new ExecutableUpdateOperationSupport(this).update(domainType);
} }
/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.core.ExecutableRemoveOperation#remove(java.lang.Class)
*/
@Override @Override
public <T> RemoveOperation<T> remove(Class<T> domainType) { public <T> RemoveOperation<T> remove(Class<T> domainType) {
return new ExecutableRemoveOperationSupport(this).remove(domainType); return new ExecutableRemoveOperationSupport(this).remove(domainType);
} }
/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.core.ExecutableAggregationOperation#aggregateAndReturn(java.lang.Class)
*/
@Override @Override
public <T> AggregationOperation<T> aggregateAndReturn(Class<T> domainType) { public <T> AggregationOperation<T> aggregateAndReturn(Class<T> domainType) {
return new ExecutableAggregationOperationSupport(this).aggregateAndReturn(domainType); return new ExecutableAggregationOperationSupport(this).aggregateAndReturn(domainType);
} }
/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.core.ExecutableInsertOperation#insert(java.lang.Class)
*/
@Override @Override
public <T> InsertOperation<T> insert(Class<T> domainType) { public <T> InsertOperation<T> insert(Class<T> domainType) {
return new ExecutableInsertOperationSupport(this).insert(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 * 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. * 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 * @since 2.0
*/ */
<S, T> List<T> doFind(String collectionName, Document query, Document fields, Class<S> sourceClass, <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) { 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) { 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) { private <T> String determineEntityCollectionName(T obj) {

View File

@@ -30,6 +30,8 @@ import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.data.mongodb.core.aggregation.Aggregation; import org.springframework.data.mongodb.core.aggregation.Aggregation;
/** /**
* Unit tests for {@link ExecutableAggregationOperationSupport}.
*
* @author Christoph Strobl * @author Christoph Strobl
*/ */
@RunWith(MockitoJUnitRunner.class) @RunWith(MockitoJUnitRunner.class)
@@ -66,7 +68,7 @@ public class ExecutableAggregationOperationSupportUnitTests {
@Test // DATAMONGO-1563 @Test // DATAMONGO-1563
public void aggregateWithUntypedAggregationAndExplicitCollection() { 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); ArgumentCaptor<Class> captor = ArgumentCaptor.forClass(Class.class);
verify(template).aggregate(any(Aggregation.class), eq("star-wars"), captor.capture()); 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"); 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); ArgumentCaptor<Class> captor = ArgumentCaptor.forClass(Class.class);
@@ -93,7 +95,7 @@ public class ExecutableAggregationOperationSupportUnitTests {
when(template.determineCollectionName(any(Class.class))).thenReturn("person"); 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); ArgumentCaptor<Class> captor = ArgumentCaptor.forClass(Class.class);
@@ -143,10 +145,7 @@ public class ExecutableAggregationOperationSupportUnitTests {
assertThat(captor.getAllValues()).containsExactly(Person.class, Jedi.class); assertThat(captor.getAllValues()).containsExactly(Person.class, Jedi.class);
} }
static class Person { static class Person {}
}
static class Jedi {} static class Jedi {}
} }

View File

@@ -37,7 +37,10 @@ import org.springframework.data.util.CloseableIterator;
import com.mongodb.MongoClient; import com.mongodb.MongoClient;
/** /**
* Integration tests for {@link ExecutableFindOperationSupport}.
*
* @author Christoph Strobl * @author Christoph Strobl
* @author Mark Paluch
*/ */
public class ExecutableFindOperationSupportTests { public class ExecutableFindOperationSupportTests {
@@ -92,9 +95,7 @@ public class ExecutableFindOperationSupportTests {
@Test // DATAMONGO-1563 @Test // DATAMONGO-1563
public void findAllWithProjection() { 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 @Test // DATAMONGO-1563
@@ -107,8 +108,8 @@ public class ExecutableFindOperationSupportTests {
@Test // DATAMONGO-1563 @Test // DATAMONGO-1563
public void findAllByWithCollectionUsingMappingInformation() { public void findAllByWithCollectionUsingMappingInformation() {
assertThat(template.query(Jedi.class).inCollection(STAR_WARS).matching(query(where("name").is("luke"))).all()).hasSize(1) assertThat(template.query(Jedi.class).inCollection(STAR_WARS).matching(query(where("name").is("luke"))).all())
.hasOnlyElementsOfType(Jedi.class); .hasSize(1).hasOnlyElementsOfType(Jedi.class);
} }
@Test // DATAMONGO-1563 @Test // DATAMONGO-1563
@@ -166,8 +167,8 @@ public class ExecutableFindOperationSupportTests {
@Test // DATAMONGO-1563 @Test // DATAMONGO-1563
public void streamAllBy() { public void streamAllBy() {
try (CloseableIterator<Person> stream = template.query(Person.class) try (CloseableIterator<Person> stream = template.query(Person.class).matching(query(where("firstname").is("luke")))
.matching(query(where("firstname").is("luke"))).stream()) { .stream()) {
assertThat(stream).containsExactlyInAnyOrder(luke); assertThat(stream).containsExactlyInAnyOrder(luke);
} }
@@ -185,8 +186,8 @@ public class ExecutableFindOperationSupportTests {
template.save(alderan); template.save(alderan);
template.save(dantooine); template.save(dantooine);
GeoResults<Planet> results = template.query(Planet.class) GeoResults<Planet> results = template.query(Planet.class).near(NearQuery.near(-73.9667, 40.78).spherical(true))
.near(NearQuery.near(-73.9667, 40.78).spherical(true)).all(); .all();
assertThat(results.getContent()).hasSize(2); assertThat(results.getContent()).hasSize(2);
assertThat(results.getContent().get(0).getDistance()).isNotNull(); assertThat(results.getContent().get(0).getDistance()).isNotNull();
} }
@@ -238,5 +239,4 @@ public class ExecutableFindOperationSupportTests {
@Id String name; @Id String name;
Point coordinates; Point coordinates;
} }
} }

View File

@@ -35,10 +35,12 @@ import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.BulkOperations.BulkMode; import org.springframework.data.mongodb.core.BulkOperations.BulkMode;
/** /**
* Unit tests for {@link ExecutableInsertOperationSupport}.
*
* @author Christoph Strobl * @author Christoph Strobl
* @since 2017/06 * @author Mark Paluch
*/ */
@RunWith(MockitoJUnitRunner.Silent.class) @RunWith(MockitoJUnitRunner.class)
public class ExecutableInsertOperationSupportUnitTests { public class ExecutableInsertOperationSupportUnitTests {
private static final String STAR_WARS = "star-wars"; private static final String STAR_WARS = "star-wars";

View File

@@ -32,7 +32,10 @@ import com.mongodb.MongoClient;
import com.mongodb.client.result.DeleteResult; import com.mongodb.client.result.DeleteResult;
/** /**
* Integration tests for {@link ExecutableRemoveOperationSupport}.
*
* @author Christoph Strobl * @author Christoph Strobl
* @author Mark Paluch
*/ */
public class ExecutableRemoveOperationSupportTests { public class ExecutableRemoveOperationSupportTests {

View File

@@ -35,7 +35,10 @@ import com.mongodb.MongoClient;
import com.mongodb.client.result.UpdateResult; import com.mongodb.client.result.UpdateResult;
/** /**
* Integration tests for {@link ExecutableUpdateOperationSupport}.
*
* @author Christoph Strobl * @author Christoph Strobl
* @author Mark Paluch
*/ */
public class ExecutableUpdateOperationSupportTests { public class ExecutableUpdateOperationSupportTests {
@@ -186,5 +189,4 @@ public class ExecutableUpdateOperationSupportTests {
@Field("firstname") String name; @Field("firstname") String name;
} }
} }