DATAMONGO-1834 - Polishing.

Increase visibility of Timezone factory methods. Add missing nullable annotation. Tweaked Javadoc. Add tests for Timezone using expressions/field references.

Original Pull Request: #539
This commit is contained in:
Mark Paluch
2018-03-27 11:04:30 +02:00
committed by Christoph Strobl
parent 82c9b0c662
commit f6ca0049b6
2 changed files with 33 additions and 12 deletions

View File

@@ -23,6 +23,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import org.bson.Document; import org.bson.Document;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils; import org.springframework.util.ObjectUtils;
/** /**
@@ -101,9 +102,7 @@ abstract class AbstractAggregationExpression implements AggregationExpression {
List<Object> clone = new ArrayList<Object>((List) this.value); List<Object> clone = new ArrayList<Object>((List) this.value);
if (value instanceof List) { if (value instanceof List) {
for (Object val : (List) value) { clone.addAll((List) value);
clone.add(val);
}
} else { } else {
clone.add(value); clone.add(value);
} }
@@ -116,9 +115,8 @@ abstract class AbstractAggregationExpression implements AggregationExpression {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
protected java.util.Map<String, Object> append(String key, Object value) { protected java.util.Map<String, Object> append(String key, Object value) {
if (!(this.value instanceof java.util.Map)) { Assert.isInstanceOf(Map.class, this.value, "Value must be a type of Map!");
throw new IllegalArgumentException("o_O");
}
java.util.Map<String, Object> clone = new LinkedHashMap<>((java.util.Map) this.value); java.util.Map<String, Object> clone = new LinkedHashMap<>((java.util.Map) this.value);
clone.put(key, value); clone.put(key, value);
return clone; return clone;
@@ -144,6 +142,7 @@ abstract class AbstractAggregationExpression implements AggregationExpression {
* @return * @return
* @since 2.1 * @since 2.1
*/ */
@SuppressWarnings("unchecked")
protected <T> T get(int index) { protected <T> T get(int index) {
return (T) values().get(index); return (T) values().get(index);
} }
@@ -156,11 +155,10 @@ abstract class AbstractAggregationExpression implements AggregationExpression {
* @return * @return
* @since 2.1 * @since 2.1
*/ */
@SuppressWarnings("unchecked")
protected <T> T get(Object key) { protected <T> T get(Object key) {
if (!(this.value instanceof java.util.Map)) { Assert.isInstanceOf(Map.class, this.value, "Value must be a type of Map!");
throw new IllegalArgumentException("o_O");
}
return (T) ((java.util.Map<String, Object>) this.value).get(key); return (T) ((java.util.Map<String, Object>) this.value).get(key);
} }
@@ -171,11 +169,10 @@ abstract class AbstractAggregationExpression implements AggregationExpression {
* @since 2.1 * @since 2.1
* @return * @return
*/ */
@SuppressWarnings("unchecked")
protected java.util.Map<String, Object> argumentMap() { protected java.util.Map<String, Object> argumentMap() {
if (!(this.value instanceof java.util.Map)) { Assert.isInstanceOf(Map.class, this.value, "Value must be a type of Map!");
throw new IllegalArgumentException("o_O");
}
return Collections.unmodifiableMap((java.util.Map) value); return Collections.unmodifiableMap((java.util.Map) value);
} }
@@ -187,6 +184,7 @@ abstract class AbstractAggregationExpression implements AggregationExpression {
* @return * @return
* @since 2.1 * @since 2.1
*/ */
@SuppressWarnings("unchecked")
protected boolean contains(Object key) { protected boolean contains(Object key) {
if (!(this.value instanceof java.util.Map)) { if (!(this.value instanceof java.util.Map)) {

View File

@@ -1078,6 +1078,29 @@ public class ProjectionOperationUnitTests {
"{ $project: { dayOfYear: { $dayOfYear: { \"date\" : \"$date\", \"timezone\" : \"America/Chicago\" } } } }")); "{ $project: { dayOfYear: { $dayOfYear: { \"date\" : \"$date\", \"timezone\" : \"America/Chicago\" } } } }"));
} }
@Test // DATAMONGO-1834
public void shouldRenderTimeZoneFromField() {
Document agg = project()
.and(DateOperators.dateOf("date").withTimezone(Timezone.ofField("tz")).dayOfYear()).as("dayOfYear")
.toDocument(Aggregation.DEFAULT_CONTEXT);
assertThat(agg).isEqualTo(Document.parse(
"{ $project: { dayOfYear: { $dayOfYear: { \"date\" : \"$date\", \"timezone\" : \"$tz\" } } } }"));
}
@Test // DATAMONGO-1834
public void shouldRenderTimeZoneFromExpression() {
Document agg = project()
.and(DateOperators.dateOf("date")
.withTimezone(Timezone.ofExpression(LiteralOperators.valueOf("America/Chicago").asLiteral())).dayOfYear())
.as("dayOfYear").toDocument(Aggregation.DEFAULT_CONTEXT);
assertThat(agg).isEqualTo(Document.parse(
"{ $project: { dayOfYear: { $dayOfYear: { \"date\" : \"$date\", \"timezone\" : { $literal: \"America/Chicago\"} } } } }"));
}
@Test // DATAMONGO-1536 @Test // DATAMONGO-1536
public void shouldRenderDayOfMonthAggregationExpression() { public void shouldRenderDayOfMonthAggregationExpression() {