DATAMONGO-1777 - Polishing.

This commit is contained in:
Christoph Strobl
2017-09-21 09:44:48 +02:00
committed by Oliver Gierke
parent 5bf03cfa70
commit f28d47b01b
3 changed files with 40 additions and 44 deletions

View File

@@ -17,10 +17,9 @@ package org.springframework.data.mongodb.core.query;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import org.bson.Document;
import org.springframework.core.convert.converter.Converter;
@@ -70,7 +69,7 @@ public abstract class SerializationUtils {
return Collections.emptyMap();
}
Map<String, Object> result = new HashMap<String, Object>();
Map<String, Object> result = new LinkedHashMap<>();
toFlatMap("", source, result);
return result;
}
@@ -80,12 +79,12 @@ public abstract class SerializationUtils {
if (source instanceof Document) {
Document document = (Document) source;
Iterator<Map.Entry<String, Object>> iter = document.entrySet().iterator();
String pathPrefix = currentPath.isEmpty() ? "" : currentPath + ".";
Iterator<Map.Entry<String, Object>> it = document.entrySet().iterator();
String pathPrefix = currentPath.isEmpty() ? "" : currentPath + '.';
while (iter.hasNext()) {
while (it.hasNext()) {
Map.Entry<String, Object> entry = iter.next();
Map.Entry<String, Object> entry = it.next();
if (entry.getKey().startsWith("$")) {
if (map.containsKey(currentPath)) {
@@ -133,19 +132,12 @@ public abstract class SerializationUtils {
}
private static String toString(Map<?, ?> source) {
return iterableToDelimitedString(source.entrySet(), "{ ", " }", new Converter<Entry<?, ?>, Object>() {
public Object convert(Entry<?, ?> source) {
return String.format("\"%s\" : %s", source.getKey(), serializeToJsonSafely(source.getValue()));
}
});
return iterableToDelimitedString(source.entrySet(), "{ ", " }",
entry -> String.format("\"%s\" : %s", entry.getKey(), serializeToJsonSafely(entry.getValue())));
}
private static String toString(Collection<?> source) {
return iterableToDelimitedString(source, "[ ", " ]", new Converter<Object, Object>() {
public Object convert(Object source) {
return serializeToJsonSafely(source);
}
});
return iterableToDelimitedString(source, "[ ", " ]", SerializationUtils::serializeToJsonSafely);
}
/**
@@ -166,6 +158,7 @@ public abstract class SerializationUtils {
Iterator<T> iterator = source.iterator();
while (iterator.hasNext()) {
builder.append(transformer.convert(iterator.next()));
if (iterator.hasNext()) {
builder.append(", ");

View File

@@ -24,6 +24,7 @@ import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import org.bson.Document;
@@ -55,9 +56,9 @@ public class Update {
}
private boolean isolated = false;
private Set<String> keysToUpdate = new HashSet<String>();
private Map<String, Object> modifierOps = new LinkedHashMap<String, Object>();
private Map<String, PushOperatorBuilder> pushCommandBuilders = new LinkedHashMap<String, PushOperatorBuilder>(1);
private Set<String> keysToUpdate = new HashSet<>();
private Map<String, Object> modifierOps = new LinkedHashMap<>();
private Map<String, PushOperatorBuilder> pushCommandBuilders = new LinkedHashMap<>(1);
/**
* Static factory method to create an Update using the provided key
@@ -122,7 +123,8 @@ public class Update {
* @param key
* @param value
* @return
* @see <a href="https://docs.mongodb.org/manual/reference/operator/update/setOnInsert/">MongoDB Update operator: $setOnInsert</a>
* @see <a href="https://docs.mongodb.org/manual/reference/operator/update/setOnInsert/">MongoDB Update operator:
* $setOnInsert</a>
*/
public Update setOnInsert(String key, Object value) {
addMultiFieldOperation("$setOnInsert", key, value);
@@ -193,7 +195,8 @@ public class Update {
* @param key
* @param values
* @return
* @see <a href="https://docs.mongodb.org/manual/reference/operator/update/pushAll/">MongoDB Update operator: $pushAll</a>
* @see <a href="https://docs.mongodb.org/manual/reference/operator/update/pushAll/">MongoDB Update operator:
* $pushAll</a>
*/
public Update pushAll(String key, Object[] values) {
addMultiFieldOperation("$pushAll", key, Arrays.asList(values));
@@ -218,7 +221,8 @@ public class Update {
* @param key
* @param value
* @return
* @see <a href="https://docs.mongodb.org/manual/reference/operator/update/addToSet/">MongoDB Update operator: $addToSet</a>
* @see <a href="https://docs.mongodb.org/manual/reference/operator/update/addToSet/">MongoDB Update operator:
* $addToSet</a>
*/
public Update addToSet(String key, Object value) {
addMultiFieldOperation("$addToSet", key, value);
@@ -257,7 +261,8 @@ public class Update {
* @param key
* @param values
* @return
* @see <a href="https://docs.mongodb.org/manual/reference/operator/update/pullAll/">MongoDB Update operator: $pullAll</a>
* @see <a href="https://docs.mongodb.org/manual/reference/operator/update/pullAll/">MongoDB Update operator:
* $pullAll</a>
*/
public Update pullAll(String key, Object[] values) {
addMultiFieldOperation("$pullAll", key, Arrays.asList(values));
@@ -270,7 +275,8 @@ public class Update {
* @param oldName
* @param newName
* @return
* @see <a href="https://docs.mongodb.org/manual/reference/operator/update/rename/">MongoDB Update operator: $rename</a>
* @see <a href="https://docs.mongodb.org/manual/reference/operator/update/rename/">MongoDB Update operator:
* $rename</a>
*/
public Update rename(String oldName, String newName) {
addMultiFieldOperation("$rename", oldName, newName);
@@ -283,7 +289,8 @@ public class Update {
* @param key
* @return
* @since 1.6
* @see <a href="https://docs.mongodb.org/manual/reference/operator/update/currentDate/">MongoDB Update operator: $currentDate</a>
* @see <a href="https://docs.mongodb.org/manual/reference/operator/update/currentDate/">MongoDB Update operator:
* $currentDate</a>
*/
public Update currentDate(String key) {
@@ -297,7 +304,8 @@ public class Update {
* @param key
* @return
* @since 1.6
* @see <a href="https://docs.mongodb.org/manual/reference/operator/update/currentDate/">MongoDB Update operator: $currentDate</a>
* @see <a href="https://docs.mongodb.org/manual/reference/operator/update/currentDate/">MongoDB Update operator:
* $currentDate</a>
*/
public Update currentTimestamp(String key) {
@@ -476,7 +484,7 @@ public class Update {
}
Update that = (Update) obj;
return this.getUpdateObject().equals(that.getUpdateObject());
return Objects.equals(this.getUpdateObject(), that.getUpdateObject());
}
/*
@@ -506,7 +514,7 @@ public class Update {
private Map<String, Modifier> modifiers;
public Modifiers() {
this.modifiers = new LinkedHashMap<String, Modifier>(1);
this.modifiers = new LinkedHashMap<>(1);
}
public Collection<Modifier> getModifiers() {
@@ -534,7 +542,8 @@ public class Update {
return nullSafeHashCode(modifiers);
}
/* (non-Javadoc)
/*
* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
@@ -550,7 +559,7 @@ public class Update {
Modifiers that = (Modifiers) obj;
return this.modifiers.equals(that.modifiers);
return Objects.equals(this.modifiers, that.modifiers);
}
@Override
@@ -958,7 +967,7 @@ public class Update {
PushOperatorBuilder that = (PushOperatorBuilder) obj;
if (!getOuterType().equals(that.getOuterType())) {
if (!Objects.equals(getOuterType(), that.getOuterType())) {
return false;
}

View File

@@ -425,8 +425,7 @@ public class UpdateTests {
Update update = new Update().max("key", 10);
assertThat(update.getUpdateObject(),
equalTo(new Document("$max", new Document("key", 10))));
assertThat(update.getUpdateObject(), equalTo(new Document("$max", new Document("key", 10))));
}
@Test // DATAMONGO-1404
@@ -434,8 +433,7 @@ public class UpdateTests {
Update update = new Update().min("key", 10);
assertThat(update.getUpdateObject(),
equalTo(new Document("$min", new Document("key", 10))));
assertThat(update.getUpdateObject(), equalTo(new Document("$min", new Document("key", 10))));
}
@Test // DATAMONGO-1404
@@ -444,8 +442,7 @@ public class UpdateTests {
Update update = new Update().max("key", 10);
update.max("key", 99);
assertThat(update.getUpdateObject(),
equalTo(new Document("$max", new Document("key", 99))));
assertThat(update.getUpdateObject(), equalTo(new Document("$max", new Document("key", 99))));
}
@Test // DATAMONGO-1404
@@ -454,8 +451,7 @@ public class UpdateTests {
Update update = new Update().min("key", 10);
update.min("key", 99);
assertThat(update.getUpdateObject(),
equalTo(new Document("$min", new Document("key", 99))));
assertThat(update.getUpdateObject(), equalTo(new Document("$min", new Document("key", 99))));
}
@Test // DATAMONGO-1404
@@ -464,8 +460,7 @@ public class UpdateTests {
Date date = new Date();
Update update = new Update().max("key", date);
assertThat(update.getUpdateObject(),
equalTo(new Document("$max", new Document("key", date))));
assertThat(update.getUpdateObject(), equalTo(new Document("$max", new Document("key", date))));
}
@Test // DATAMONGO-1404
@@ -474,8 +469,7 @@ public class UpdateTests {
Date date = new Date();
Update update = new Update().min("key", date);
assertThat(update.getUpdateObject(),
equalTo(new Document("$min", new Document("key", date))));
assertThat(update.getUpdateObject(), equalTo(new Document("$min", new Document("key", date))));
}
@Test // DATAMONGO-1777