DATAMONGO-2388 - Polishing.

Use StringJoiner to create comma-delimited String.

Original pull request: #797.
This commit is contained in:
Mark Paluch
2019-10-28 10:47:14 +01:00
parent 5c80ee0087
commit 004e7f01b2

View File

@@ -19,7 +19,7 @@ import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.StringJoiner;
import java.util.stream.StreamSupport;
import org.bson.BsonValue;
@@ -30,7 +30,6 @@ import org.bson.json.JsonParseException;
import org.springframework.core.convert.converter.Converter;
import org.springframework.lang.Nullable;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
@@ -120,7 +119,7 @@ public class BsonUtils {
*
* @param source
* @return
* @since 2.1.1
* @since 2.1.12
*/
@Nullable
public static String toJson(@Nullable Document source) {
@@ -181,12 +180,13 @@ public class BsonUtils {
return iterableToDelimitedString(source, "[ ", " ]", BsonUtils::toJson);
}
private static <T> String iterableToDelimitedString(Iterable<T> source, String prefix, String postfix,
Converter<? super T, Object> transformer) {
private static <T> String iterableToDelimitedString(Iterable<T> source, String prefix, String suffix,
Converter<? super T, String> transformer) {
return prefix
+ StringUtils.collectionToCommaDelimitedString(
StreamSupport.stream(source.spliterator(), false).map(transformer::convert).collect(Collectors.toList()))
+ postfix;
StringJoiner joiner = new StringJoiner(", ", prefix, suffix);
StreamSupport.stream(source.spliterator(), false).map(transformer::convert).forEach(joiner::add);
return joiner.toString();
}
}