Skip to content

Commit 2c29f20

Browse files
christophstroblmp911de
authored andcommitted
DATAMONGO-1564 - Split up AggregationExpressions.
Refactored to multiple smaller Aggregation Operator classes reflecting the grouping (array operators, string operators,…) predefined by MongoDB. Original pull request: spring-projects#429.
1 parent 9cd44fa commit 2c29f20

30 files changed

+9141
-8730
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
/*
2+
* Copyright 2016. the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.mongodb.core.aggregation;
17+
18+
import java.util.ArrayList;
19+
import java.util.Arrays;
20+
import java.util.Collections;
21+
import java.util.LinkedHashMap;
22+
import java.util.List;
23+
24+
import org.bson.Document;
25+
import org.springframework.util.ObjectUtils;
26+
27+
/**
28+
* @author Christoph Strobl
29+
* @since 1.10
30+
*/
31+
abstract class AbstractAggregationExpression implements AggregationExpression {
32+
33+
private final Object value;
34+
35+
protected AbstractAggregationExpression(Object value) {
36+
this.value = value;
37+
}
38+
39+
/* (non-Javadoc)
40+
* @see org.springframework.data.mongodb.core.aggregation.AggregationExpression#toDocument(org.springframework.data.mongodb.core.aggregation.AggregationOperationContext)
41+
*/
42+
@Override
43+
public Document toDocument(AggregationOperationContext context) {
44+
return toDocument(this.value, context);
45+
}
46+
47+
public Document toDocument(Object value, AggregationOperationContext context) {
48+
49+
Object valueToUse;
50+
if (value instanceof List) {
51+
52+
List<Object> arguments = (List<Object>) value;
53+
List<Object> args = new ArrayList<Object>(arguments.size());
54+
55+
for (Object val : arguments) {
56+
args.add(unpack(val, context));
57+
}
58+
valueToUse = args;
59+
} else if (value instanceof java.util.Map) {
60+
61+
Document dbo = new Document();
62+
for (java.util.Map.Entry<String, Object> entry : ((java.util.Map<String, Object>) value).entrySet()) {
63+
dbo.put(entry.getKey(), unpack(entry.getValue(), context));
64+
}
65+
valueToUse = dbo;
66+
} else {
67+
valueToUse = unpack(value, context);
68+
}
69+
70+
return new Document(getMongoMethod(), valueToUse);
71+
}
72+
73+
protected static List<Field> asFields(String... fieldRefs) {
74+
75+
if (ObjectUtils.isEmpty(fieldRefs)) {
76+
return Collections.emptyList();
77+
}
78+
79+
return Fields.fields(fieldRefs).asList();
80+
}
81+
82+
private Object unpack(Object value, AggregationOperationContext context) {
83+
84+
if (value instanceof AggregationExpression) {
85+
return ((AggregationExpression) value).toDocument(context);
86+
}
87+
88+
if (value instanceof Field) {
89+
return context.getReference((Field) value).toString();
90+
}
91+
92+
if (value instanceof List) {
93+
94+
List<Object> sourceList = (List<Object>) value;
95+
List<Object> mappedList = new ArrayList<Object>(sourceList.size());
96+
97+
for (Object item : sourceList) {
98+
mappedList.add(unpack(item, context));
99+
}
100+
return mappedList;
101+
}
102+
103+
return value;
104+
}
105+
106+
protected List<Object> append(Object value) {
107+
108+
if (this.value instanceof List) {
109+
110+
List<Object> clone = new ArrayList<Object>((List) this.value);
111+
112+
if (value instanceof List) {
113+
for (Object val : (List) value) {
114+
clone.add(val);
115+
}
116+
} else {
117+
clone.add(value);
118+
}
119+
return clone;
120+
}
121+
122+
return Arrays.asList(this.value, value);
123+
}
124+
125+
protected java.util.Map<String, Object> append(String key, Object value) {
126+
127+
if (!(this.value instanceof java.util.Map)) {
128+
throw new IllegalArgumentException("o_O");
129+
}
130+
java.util.Map<String, Object> clone = new LinkedHashMap<String, Object>(
131+
(java.util.Map<String, Object>) this.value);
132+
clone.put(key, value);
133+
return clone;
134+
135+
}
136+
137+
protected List<Object> values() {
138+
139+
if (value instanceof List) {
140+
return new ArrayList<Object>((List) value);
141+
}
142+
if (value instanceof java.util.Map) {
143+
return new ArrayList<Object>(((java.util.Map) value).values());
144+
}
145+
return new ArrayList<Object>(Arrays.asList(value));
146+
}
147+
148+
protected abstract String getMongoMethod();
149+
}

0 commit comments

Comments
 (0)