Skip to content

Commit 6cc5bc6

Browse files
committed
DATAMONGO-2388 - Polishing.
Use StringJoiner to create comma-delimited String. Add nullability annotations. Original pull request: #797.
1 parent 84f7f43 commit 6cc5bc6

File tree

1 file changed

+15
-11
lines changed
  • spring-data-mongodb/src/main/java/org/springframework/data/mongodb/util

1 file changed

+15
-11
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/util/BsonUtils.java

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,15 @@
1919
import java.util.Collection;
2020
import java.util.Date;
2121
import java.util.Map;
22+
import java.util.StringJoiner;
2223
import java.util.function.Function;
23-
import java.util.stream.Collectors;
2424
import java.util.stream.StreamSupport;
2525

2626
import org.bson.BsonValue;
2727
import org.bson.Document;
2828
import org.bson.conversions.Bson;
2929
import org.bson.json.JsonParseException;
30+
3031
import org.springframework.core.convert.converter.Converter;
3132
import org.springframework.lang.Nullable;
3233
import org.springframework.util.ObjectUtils;
@@ -155,12 +156,13 @@ public static Document toDocumentOrElse(String source, Function<String, Document
155156

156157
/**
157158
* Serialize the given {@link Document} as Json applying default codecs if necessary.
158-
*
159+
*
159160
* @param source
160161
* @return
161-
* @since 2.1.1
162+
* @since 2.2.1
162163
*/
163-
public static String toJson(Document source) {
164+
@Nullable
165+
public static String toJson(@Nullable Document source) {
164166

165167
if (source == null) {
166168
return null;
@@ -173,7 +175,8 @@ public static String toJson(Document source) {
173175
}
174176
}
175177

176-
private static String toJson(Object value) {
178+
@Nullable
179+
private static String toJson(@Nullable Object value) {
177180

178181
if (value == null) {
179182
return null;
@@ -218,12 +221,13 @@ private static String toString(Collection<?> source) {
218221
return iterableToDelimitedString(source, "[ ", " ]", BsonUtils::toJson);
219222
}
220223

221-
private static <T> String iterableToDelimitedString(Iterable<T> source, String prefix, String postfix,
222-
Converter<? super T, Object> transformer) {
224+
private static <T> String iterableToDelimitedString(Iterable<T> source, String prefix, String suffix,
225+
Converter<? super T, String> transformer) {
226+
227+
StringJoiner joiner = new StringJoiner(", ", prefix, suffix);
228+
229+
StreamSupport.stream(source.spliterator(), false).map(transformer::convert).forEach(joiner::add);
223230

224-
return prefix
225-
+ StringUtils.collectionToCommaDelimitedString(
226-
StreamSupport.stream(source.spliterator(), false).map(transformer::convert).collect(Collectors.toList()))
227-
+ postfix;
231+
return joiner.toString();
228232
}
229233
}

0 commit comments

Comments
 (0)