Skip to content

Commit

Permalink
replace stream api usage on Map with dedicated for loop
Browse files Browse the repository at this point in the history
  • Loading branch information
christophstrobl committed Dec 14, 2023
1 parent a682ef9 commit 75959f6
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -674,12 +674,13 @@ public <T> T getPropertyValue(MongoPersistentProperty property) {
}

if (!documentField.getProperty().isMap() && sourceValue instanceof Document document) {
return new Document(document.entrySet().stream().collect(Collectors.toMap(Entry::getKey, entry -> {
if (isKeyword(entry.getKey())) {
return getMappedValue(documentField, entry.getValue());

return BsonUtils.mapValues(document, (key, val) -> {
if (isKeyword(key)) {
return getMappedValue(documentField, val);
}
return entry.getValue();
})));
return val;
});
}

return valueConverter.write(value, conversionContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.StringJoiner;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.stream.StreamSupport;

Expand Down Expand Up @@ -716,6 +719,23 @@ public static Collection<?> asCollection(Object source) {
return source.getClass().isArray() ? CollectionUtils.arrayToList(source) : Collections.singleton(source);
}

public static Document mapValues(Document source, BiFunction<String, Object, Object> valueMapper) {
return mapEntries(source, Entry::getKey, entry -> valueMapper.apply(entry.getKey(), entry.getValue()));
}

public static Document mapEntries(Document source, Function<Entry<String,Object>,String> keyMapper, Function<Entry<String,Object>,Object> valueMapper) {

if(source.isEmpty()) {
return source;
}

Map<String, Object> target = new LinkedHashMap<>(source.size(), 1f);
for(Entry<String,Object> entry : source.entrySet()) {
target.put(keyMapper.apply(entry), valueMapper.apply(entry));
}
return new Document(target);
}

@Nullable
private static String toJson(@Nullable Object value) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Stream;

import org.bson.BsonArray;
Expand Down Expand Up @@ -180,6 +181,52 @@ void resolveValueForField(FieldName fieldName, boolean exists) {
}
}

@Test
void retainsOrderWhenMappingValues() {

Document source = new Document();
source.append("z", "first-entry");
source.append("a", "second-entry");
source.append("0", "third-entry");
source.append("9", "fourth-entry");

Document target = BsonUtils.mapValues(source, (key, value) -> value);
assertThat(source).isNotSameAs(target).containsExactlyEntriesOf(source);
}

@Test
void retainsOrderWhenMappingKeys() {

Document source = new Document();
source.append("z", "first-entry");
source.append("a", "second-entry");

Document target = BsonUtils.mapEntries(source, entry -> entry.getKey().toUpperCase(), Entry::getValue);
assertThat(target).containsExactly(Map.entry("Z", "first-entry"), Map.entry("A", "second-entry"));
}

@Test
void appliesValueMapping() {
Document source = new Document();
source.append("z", "first-entry");
source.append("a", "second-entry");

Document target = BsonUtils.mapValues(source,
(key, value) -> new StringBuilder(value.toString()).reverse().toString());
assertThat(target).containsValues("yrtne-tsrif", "yrtne-dnoces");
}

@Test
void appliesKeyMapping() {

Document source = new Document();
source.append("z", "first-entry");
source.append("a", "second-entry");

Document target = BsonUtils.mapEntries(source, entry -> entry.getKey().toUpperCase(), Entry::getValue);
assertThat(target).containsKeys("Z", "A");
}

static Stream<Arguments> fieldNames() {
return Stream.of(//
Arguments.of(FieldName.path("a"), true), //
Expand Down

0 comments on commit 75959f6

Please sign in to comment.