Skip to content

Commit 9721cf3

Browse files
christophstroblmp911de
authored andcommitted
Polishing.
Remove unused imports. Simplify if & switch statements. Use Set.of, List.of for unmodifiable collections. Remove redundant usage of semicolon. Fix broken references in Javadoc. Fix Typos. Extend value caching for MongoPersistentProperties. See: #4555 Original pull request: #4556
1 parent 9016b4e commit 9721cf3

File tree

100 files changed

+295
-550
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

100 files changed

+295
-550
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/SessionAwareMethodInterceptor.java

+2-6
Original file line numberDiff line numberDiff line change
@@ -135,12 +135,8 @@ protected Object decorate(Object target) {
135135

136136
private static boolean requiresSession(Method method) {
137137

138-
if (method.getParameterCount() == 0
139-
|| !ClassUtils.isAssignable(ClientSession.class, method.getParameterTypes()[0])) {
140-
return true;
141-
}
142-
143-
return false;
138+
return method.getParameterCount() == 0
139+
|| !ClassUtils.isAssignable(ClientSession.class, method.getParameterTypes()[0]);
144140
}
145141

146142
private static Object[] prependSessionToArguments(ClientSession session, MethodInvocation invocation) {

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/SessionSynchronization.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,5 @@ public enum SessionSynchronization {
4848
*
4949
* @since 3.2.5
5050
*/
51-
NEVER;
51+
NEVER
5252
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/aot/MongoAotPredicates.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
public class MongoAotPredicates {
3232

3333
public static final Predicate<Class<?>> IS_SIMPLE_TYPE = (type) -> MongoSimpleTypes.HOLDER.isSimpleType(type) || TypeUtils.type(type).isPartOf("org.bson");
34-
public static final Predicate<ReactiveLibrary> IS_REACTIVE_LIBARARY_AVAILABLE = (lib) -> ReactiveWrappers.isAvailable(lib);
34+
public static final Predicate<ReactiveLibrary> IS_REACTIVE_LIBARARY_AVAILABLE = ReactiveWrappers::isAvailable;
3535
public static final Predicate<ClassLoader> IS_SYNC_CLIENT_PRESENT = (classLoader) -> ClassUtils.isPresent("com.mongodb.client.MongoClient", classLoader);
3636

3737
public static boolean isReactorPresent() {

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/MappingMongoConverterParser.java

+2-5
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
import static org.springframework.data.mongodb.config.BeanNames.*;
1919

2020
import java.io.IOException;
21-
import java.util.Arrays;
22-
import java.util.HashSet;
2321
import java.util.List;
2422
import java.util.Set;
2523

@@ -63,7 +61,6 @@
6361
import org.springframework.util.ClassUtils;
6462
import org.springframework.util.StringUtils;
6563
import org.springframework.util.xml.DomUtils;
66-
6764
import org.w3c.dom.Element;
6865

6966
/**
@@ -97,7 +94,7 @@ public BeanDefinition parse(Element element, ParserContext parserContext) {
9794
id = StringUtils.hasText(id) ? id : DEFAULT_CONVERTER_BEAN_NAME;
9895

9996
String autoIndexCreation = element.getAttribute("auto-index-creation");
100-
boolean autoIndexCreationEnabled = StringUtils.hasText(autoIndexCreation) && Boolean.valueOf(autoIndexCreation);
97+
boolean autoIndexCreationEnabled = StringUtils.hasText(autoIndexCreation) && Boolean.parseBoolean(autoIndexCreation);
10198

10299
parserContext.pushContainingComponent(new CompositeComponentDefinition("Mapping Mongo Converter", element));
103100

@@ -371,7 +368,7 @@ public NegatingFilter(TypeFilter... filters) {
371368

372369
Assert.notNull(filters, "TypeFilters must not be null");
373370

374-
this.delegates = new HashSet<>(Arrays.asList(filters));
371+
this.delegates = Set.of(filters);
375372
}
376373

377374
public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory)

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/MongoCredentialPropertyEditor.java

+2-6
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
package org.springframework.data.mongodb.config;
1717

1818
import java.beans.PropertyEditorSupport;
19-
import java.io.UnsupportedEncodingException;
2019
import java.lang.reflect.Method;
2120
import java.net.URLDecoder;
21+
import java.nio.charset.StandardCharsets;
2222
import java.util.ArrayList;
2323
import java.util.Arrays;
2424
import java.util.List;
@@ -228,10 +228,6 @@ private static void verifyUserNamePresent(String[] source) {
228228
}
229229

230230
private static String decodeParameter(String it) {
231-
try {
232-
return URLDecoder.decode(it, "UTF-8");
233-
} catch (UnsupportedEncodingException e) {
234-
throw new IllegalArgumentException("o_O UTF-8 not supported", e);
235-
}
231+
return URLDecoder.decode(it, StandardCharsets.UTF_8);
236232
}
237233
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/MongoDbFactoryParser.java

+1-12
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
import static org.springframework.data.config.ParsingUtils.*;
1919
import static org.springframework.data.mongodb.config.MongoParsingUtils.*;
2020

21-
import java.util.Collections;
22-
import java.util.HashSet;
2321
import java.util.Set;
2422

2523
import org.springframework.beans.factory.BeanDefinitionStoreException;
@@ -51,16 +49,7 @@
5149
*/
5250
public class MongoDbFactoryParser extends AbstractBeanDefinitionParser {
5351

54-
private static final Set<String> MONGO_URI_ALLOWED_ADDITIONAL_ATTRIBUTES;
55-
56-
static {
57-
58-
Set<String> mongoUriAllowedAdditionalAttributes = new HashSet<String>();
59-
mongoUriAllowedAdditionalAttributes.add("id");
60-
mongoUriAllowedAdditionalAttributes.add("write-concern");
61-
62-
MONGO_URI_ALLOWED_ADDITIONAL_ATTRIBUTES = Collections.unmodifiableSet(mongoUriAllowedAdditionalAttributes);
63-
}
52+
private static final Set<String> MONGO_URI_ALLOWED_ADDITIONAL_ATTRIBUTES = Set.of("id", "write-concern");
6453

6554
@Override
6655
protected String resolveId(Element element, AbstractBeanDefinition definition, ParserContext parserContext)

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@
4343
*/
4444
class AggregationUtil {
4545

46-
QueryMapper queryMapper;
47-
MappingContext<? extends MongoPersistentEntity<?>, MongoPersistentProperty> mappingContext;
48-
Lazy<AggregationOperationContext> untypedMappingContext;
46+
final QueryMapper queryMapper;
47+
final MappingContext<? extends MongoPersistentEntity<?>, MongoPersistentProperty> mappingContext;
48+
final Lazy<AggregationOperationContext> untypedMappingContext;
4949

5050
AggregationUtil(QueryMapper queryMapper,
5151
MappingContext<? extends MongoPersistentEntity<?>, MongoPersistentProperty> mappingContext) {

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ enum BulkMode {
6060

6161
/** Perform bulk operations in parallel. Processing will continue on errors. */
6262
UNORDERED
63-
};
63+
}
6464

6565
/**
6666
* Add a single insert to the bulk operation.

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public static ChangeStreamOptions empty() {
132132

133133
/**
134134
* Obtain a shiny new {@link ChangeStreamOptionsBuilder} and start defining options in this fancy fluent way. Just
135-
* don't forget to call {@link ChangeStreamOptionsBuilder#build() build()} when your're done.
135+
* don't forget to call {@link ChangeStreamOptionsBuilder#build() build()} when done.
136136
*
137137
* @return new instance of {@link ChangeStreamOptionsBuilder}.
138138
*/

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public interface CursorPreparer extends ReadPreferenceAware {
4949
FindIterable<Document> prepare(FindIterable<Document> iterable);
5050

5151
/**
52-
* Apply query specific settings to {@link MongoCollection} and initate a find operation returning a
52+
* Apply query specific settings to {@link MongoCollection} and initiate a find operation returning a
5353
* {@link FindIterable} via the given {@link Function find} function.
5454
*
5555
* @param collection must not be {@literal null}.

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
/**
2424
* An interface used by {@link MongoTemplate} for processing documents returned from a MongoDB query on a per-document
25-
* basis. Implementations of this interface perform the actual work of prcoessing each document but don't need to worry
25+
* basis. Implementations of this interface perform the actual work of processing each document but don't need to worry
2626
* about exception handling. {@link MongoException}s will be caught and translated by the calling MongoTemplate An
2727
* DocumentCallbackHandler is typically stateful: It keeps the result state within the object, to be available later for
2828
* later inspection.

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,7 @@ private Object resolveValue(String key, @Nullable MongoPersistentEntity<?> sourc
642642
PropertyPath from = PropertyPath.from(key, sourceEntity.getTypeInformation());
643643
PersistentPropertyPath<MongoPersistentProperty> persistentPropertyPath = entityOperations.context
644644
.getPersistentPropertyPath(from);
645-
return BsonUtils.resolveValue(map, persistentPropertyPath.toDotPath(p -> p.getFieldName()));
645+
return BsonUtils.resolveValue(map, persistentPropertyPath.toDotPath(MongoPersistentProperty::getFieldName));
646646
}
647647
}
648648

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public interface FindPublisherPreparer extends ReadPreferenceAware {
4949
FindPublisher<Document> prepare(FindPublisher<Document> findPublisher);
5050

5151
/**
52-
* Apply query specific settings to {@link MongoCollection} and initate a find operation returning a
52+
* Apply query specific settings to {@link MongoCollection} and initiate a find operation returning a
5353
* {@link FindPublisher} via the given {@link Function find} function.
5454
*
5555
* @param collection must not be {@literal null}.

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public void updateId(Object value) {
115115
* @author Christoph Strobl
116116
* @since 2.2
117117
*/
118-
class MappedUpdate implements UpdateDefinition {
118+
static class MappedUpdate implements UpdateDefinition {
119119

120120
private final Update delegate;
121121

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public PropertySpecifier property(String path) {
9999
}
100100

101101
/**
102-
* Specify additional types to be considered wehen rendering the schema for the given path.
102+
* Specify additional types to be considered when rendering the schema for the given path.
103103
*
104104
* @param path path the path using {@literal dot '.'} notation.
105105
* @param types must not be {@literal null}.

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,5 @@
2626
*/
2727
public enum MongoActionOperation {
2828

29-
REMOVE, UPDATE, INSERT, INSERT_LIST, SAVE, BULK, REPLACE;
29+
REMOVE, UPDATE, INSERT, INSERT_LIST, SAVE, BULK, REPLACE
3030
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public WriteConcernResult getWriteResult() {
6161
}
6262

6363
/**
64-
* Returns the {@link MongoActionOperation} in which the current exception occured.
64+
* Returns the {@link MongoActionOperation} in which the current exception occurred.
6565
*
6666
* @return the actionOperation
6767
*/

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

+8-13
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,9 @@
1515
*/
1616
package org.springframework.data.mongodb.core;
1717

18-
import java.util.Arrays;
19-
import java.util.Collections;
20-
import java.util.HashSet;
2118
import java.util.Set;
2219

2320
import org.bson.BsonInvalidOperationException;
24-
2521
import org.springframework.dao.DataAccessException;
2622
import org.springframework.dao.DataAccessResourceFailureException;
2723
import org.springframework.dao.DataIntegrityViolationException;
@@ -55,18 +51,17 @@
5551
*/
5652
public class MongoExceptionTranslator implements PersistenceExceptionTranslator {
5753

58-
private static final Set<String> DUPLICATE_KEY_EXCEPTIONS = new HashSet<>(
59-
Arrays.asList("MongoException.DuplicateKey", "DuplicateKeyException"));
54+
private static final Set<String> DUPLICATE_KEY_EXCEPTIONS = Set.of("MongoException.DuplicateKey",
55+
"DuplicateKeyException");
6056

61-
private static final Set<String> RESOURCE_FAILURE_EXCEPTIONS = new HashSet<>(
62-
Arrays.asList("MongoException.Network", "MongoSocketException", "MongoException.CursorNotFound",
63-
"MongoCursorNotFoundException", "MongoServerSelectionException", "MongoTimeoutException"));
57+
private static final Set<String> RESOURCE_FAILURE_EXCEPTIONS = Set.of("MongoException.Network",
58+
"MongoSocketException", "MongoException.CursorNotFound", "MongoCursorNotFoundException",
59+
"MongoServerSelectionException", "MongoTimeoutException");
6460

65-
private static final Set<String> RESOURCE_USAGE_EXCEPTIONS = new HashSet<>(
66-
Collections.singletonList("MongoInternalException"));
61+
private static final Set<String> RESOURCE_USAGE_EXCEPTIONS = Set.of("MongoInternalException");
6762

68-
private static final Set<String> DATA_INTEGRITY_EXCEPTIONS = new HashSet<>(
69-
Arrays.asList("WriteConcernException", "MongoWriteException", "MongoBulkWriteException"));
63+
private static final Set<String> DATA_INTEGRITY_EXCEPTIONS = Set.of("WriteConcernException", "MongoWriteException",
64+
"MongoBulkWriteException");
7065

7166
private static final Set<String> SECURITY_EXCEPTIONS = Set.of("MongoCryptException");
7267

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

+5-16
Original file line numberDiff line numberDiff line change
@@ -1379,12 +1379,7 @@ protected <T> Collection<T> doInsertAll(Collection<? extends T> listToSave, Mong
13791379
}
13801380

13811381
String collection = getCollectionName(ClassUtils.getUserClass(element));
1382-
List<T> collectionElements = elementsByCollection.get(collection);
1383-
1384-
if (null == collectionElements) {
1385-
collectionElements = new ArrayList<>();
1386-
elementsByCollection.put(collection, collectionElements);
1387-
}
1382+
List<T> collectionElements = elementsByCollection.computeIfAbsent(collection, k -> new ArrayList<>());
13881383

13891384
collectionElements.add(element);
13901385
}
@@ -2315,11 +2310,9 @@ public <T> ExecutableInsert<T> insert(Class<T> domainType) {
23152310

23162311
protected String replaceWithResourceIfNecessary(String function) {
23172312

2318-
String func = function;
2319-
23202313
if (this.resourceLoader != null && ResourceUtils.isUrl(function)) {
23212314

2322-
Resource functionResource = resourceLoader.getResource(func);
2315+
Resource functionResource = resourceLoader.getResource(function);
23232316

23242317
if (!functionResource.exists()) {
23252318
throw new InvalidDataAccessApiUsageException(String.format("Resource %s not found", function));
@@ -2339,7 +2332,7 @@ protected String replaceWithResourceIfNecessary(String function) {
23392332
}
23402333
}
23412334

2342-
return func;
2335+
return function;
23432336
}
23442337

23452338
@Override
@@ -2698,8 +2691,6 @@ Document getMappedValidator(Validator validator, Class<?> domainType) {
26982691
protected <T> T doFindAndRemove(CollectionPreparer collectionPreparer, String collectionName, Document query,
26992692
Document fields, Document sort, @Nullable Collation collation, Class<T> entityClass) {
27002693

2701-
EntityReader<? super T, Bson> readerToUse = this.mongoConverter;
2702-
27032694
if (LOGGER.isDebugEnabled()) {
27042695
LOGGER.debug(String.format("findAndRemove using query: %s fields: %s sort: %s for class: %s in collection: %s",
27052696
serializeToJsonSafely(query), fields, sort, entityClass, collectionName));
@@ -2709,16 +2700,14 @@ protected <T> T doFindAndRemove(CollectionPreparer collectionPreparer, String co
27092700

27102701
return executeFindOneInternal(new FindAndRemoveCallback(collectionPreparer,
27112702
queryMapper.getMappedObject(query, entity), fields, sort, collation),
2712-
new ReadDocumentCallback<>(readerToUse, entityClass, collectionName), collectionName);
2703+
new ReadDocumentCallback<>(this.mongoConverter, entityClass, collectionName), collectionName);
27132704
}
27142705

27152706
@SuppressWarnings("ConstantConditions")
27162707
protected <T> T doFindAndModify(CollectionPreparer collectionPreparer, String collectionName, Document query,
27172708
Document fields, Document sort, Class<T> entityClass, UpdateDefinition update,
27182709
@Nullable FindAndModifyOptions options) {
27192710

2720-
EntityReader<? super T, Bson> readerToUse = this.mongoConverter;
2721-
27222711
if (options == null) {
27232712
options = new FindAndModifyOptions();
27242713
}
@@ -2742,7 +2731,7 @@ protected <T> T doFindAndModify(CollectionPreparer collectionPreparer, String co
27422731
return executeFindOneInternal(
27432732
new FindAndModifyCallback(collectionPreparer, mappedQuery, fields, sort, mappedUpdate,
27442733
update.getArrayFilters().stream().map(ArrayFilter::asDocument).collect(Collectors.toList()), options),
2745-
new ReadDocumentCallback<>(readerToUse, entityClass, collectionName), collectionName);
2734+
new ReadDocumentCallback<>(this.mongoConverter, entityClass, collectionName), collectionName);
27462735
}
27472736

27482737
/**

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ Document getMappedSort(@Nullable MongoPersistentEntity<?> entity) {
433433
* @param consumer must not be {@literal null}.
434434
*/
435435
void applyCollation(@Nullable Class<?> domainType, Consumer<com.mongodb.client.model.Collation> consumer) {
436-
getCollation(domainType).ifPresent(consumer::accept);
436+
getCollation(domainType).ifPresent(consumer);
437437
}
438438

439439
/**
@@ -526,7 +526,7 @@ <T> Class<T> getDriverCompatibleClass(Class<T> type) {
526526
}
527527

528528
/**
529-
* Get the most speficic read target type based on the user {@literal requestedTargetType} an the property type
529+
* Get the most specific read target type based on the user {@literal requestedTargetType} an the property type
530530
* based on meta information extracted from the {@literal domainType}.
531531
*
532532
* @param requestedTargetType must not be {@literal null}.

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -1810,8 +1810,7 @@ protected Mono<UpdateResult> doUpdate(String collectionName, Query query, @Nulla
18101810

18111811
Document updateObj = updateContext.getMappedUpdate(entity);
18121812
if (containsVersionProperty(queryObj, entity))
1813-
throw new OptimisticLockingFailureException("Optimistic lock exception on saving entity: "
1814-
+ updateObj.toString() + " to collection " + collectionName);
1813+
throw new OptimisticLockingFailureException("Optimistic lock exception on saving entity %s to collection %s".formatted(entity.getName(), collectionName));
18151814
}
18161815
}
18171816
});

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,18 @@ class ReactiveRemoveOperationSupport implements ReactiveRemoveOperation {
3535

3636
private static final Query ALL_QUERY = new Query();
3737

38-
private final ReactiveMongoTemplate tempate;
38+
private final ReactiveMongoTemplate template;
3939

40-
ReactiveRemoveOperationSupport(ReactiveMongoTemplate tempate) {
41-
this.tempate = tempate;
40+
ReactiveRemoveOperationSupport(ReactiveMongoTemplate template) {
41+
this.template = template;
4242
}
4343

4444
@Override
4545
public <T> ReactiveRemove<T> remove(Class<T> domainType) {
4646

4747
Assert.notNull(domainType, "DomainType must not be null");
4848

49-
return new ReactiveRemoveSupport<>(tempate, domainType, ALL_QUERY, null);
49+
return new ReactiveRemoveSupport<>(template, domainType, ALL_QUERY, null);
5050
}
5151

5252
static class ReactiveRemoveSupport<T> implements ReactiveRemove<T>, RemoveWithCollection<T> {

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
public interface ScriptOperations {
3535

3636
/**
37-
* Store given {@link ExecutableMongoScript} generating a syntheitcal name so that it can be called by it
37+
* Store given {@link ExecutableMongoScript} generating a synthetic name so that it can be called by it
3838
* subsequently.
3939
*
4040
* @param script must not be {@literal null}.

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ protected <T> List<T> postPostProcessResults(List<T> list, int limit) {
199199
}
200200

201201
/**
202-
* Reverse scrolling director variant applying {@link KeysetScrollPosition.Direction#Backward}. In reverse scrolling,
202+
* Reverse scrolling director variant applying {@link KeysetScrollPosition.Direction#BACKWARD}. In reverse scrolling,
203203
* we need to flip directions for the actual query so that we do not get everything from the top position and apply
204204
* the limit but rather flip the sort direction, apply the limit and then reverse the result to restore the actual
205205
* sort order.

0 commit comments

Comments
 (0)