2929import java .util .ArrayList ;
3030import java .util .Collections ;
3131import java .util .HashMap ;
32- import java .util .Iterator ;
3332import java .util .List ;
3433import java .util .Map ;
35- import java .util .stream .Collectors ;
3634
37- import static java .util .Collections .emptyMap ;
38- import static java .util .Collections .unmodifiableMap ;
3935/**
4036 * An internal implementation of {@link Aggregations}.
4137 */
42- public class InternalAggregations implements Aggregations , ToXContent , Streamable {
38+ public final class InternalAggregations extends Aggregations implements ToXContent , Streamable {
4339
4440 public static final InternalAggregations EMPTY = new InternalAggregations ();
4541
46- private List <InternalAggregation > aggregations = Collections .emptyList ();
47-
48- private Map <String , Aggregation > aggregationsAsMap ;
49-
5042 private InternalAggregations () {
5143 }
5244
5345 /**
5446 * Constructs a new addAggregation.
5547 */
5648 public InternalAggregations (List <InternalAggregation > aggregations ) {
57- this .aggregations = aggregations ;
58- }
59-
60- /**
61- * Iterates over the {@link Aggregation}s.
62- */
63- @ Override
64- public Iterator <Aggregation > iterator () {
65- return aggregations .stream ().map ((p ) -> (Aggregation ) p ).iterator ();
66- }
67-
68- /**
69- * The list of {@link Aggregation}s.
70- */
71- @ Override
72- public List <Aggregation > asList () {
73- return aggregations .stream ().map ((p ) -> (Aggregation ) p ).collect (Collectors .toList ());
74- }
75-
76- /**
77- * Returns the {@link Aggregation}s keyed by map.
78- */
79- @ Override
80- public Map <String , Aggregation > asMap () {
81- return getAsMap ();
82- }
83-
84- /**
85- * Returns the {@link Aggregation}s keyed by map.
86- */
87- @ Override
88- public Map <String , Aggregation > getAsMap () {
89- if (aggregationsAsMap == null ) {
90- Map <String , InternalAggregation > newAggregationsAsMap = new HashMap <>();
91- for (InternalAggregation aggregation : aggregations ) {
92- newAggregationsAsMap .put (aggregation .getName (), aggregation );
93- }
94- this .aggregationsAsMap = unmodifiableMap (newAggregationsAsMap );
95- }
96- return aggregationsAsMap ;
97- }
98-
99- /**
100- * @return the aggregation of the specified name.
101- */
102- @ SuppressWarnings ("unchecked" )
103- @ Override
104- public <A extends Aggregation > A get (String name ) {
105- return (A ) asMap ().get (name );
49+ super (aggregations );
10650 }
10751
10852 /**
@@ -117,21 +61,16 @@ public static InternalAggregations reduce(List<InternalAggregations> aggregation
11761 }
11862
11963 // first we collect all aggregations of the same type and list them together
120-
12164 Map <String , List <InternalAggregation >> aggByName = new HashMap <>();
12265 for (InternalAggregations aggregations : aggregationsList ) {
123- for (InternalAggregation aggregation : aggregations .aggregations ) {
124- List <InternalAggregation > aggs = aggByName .get (aggregation .getName ());
125- if (aggs == null ) {
126- aggs = new ArrayList <>(aggregationsList .size ());
127- aggByName .put (aggregation .getName (), aggs );
128- }
129- aggs .add (aggregation );
66+ for (Aggregation aggregation : aggregations .aggregations ) {
67+ List <InternalAggregation > aggs = aggByName .computeIfAbsent (
68+ aggregation .getName (), k -> new ArrayList <>(aggregationsList .size ()));
69+ aggs .add ((InternalAggregation )aggregation );
13070 }
13171 }
13272
13373 // now we can use the first aggregation of each list to handle the reduce of its list
134-
13574 List <InternalAggregation > reducedAggregations = new ArrayList <>();
13675 for (Map .Entry <String , List <InternalAggregation >> entry : aggByName .entrySet ()) {
13776 List <InternalAggregation > aggregations = entry .getValue ();
@@ -141,31 +80,27 @@ public static InternalAggregations reduce(List<InternalAggregations> aggregation
14180 return new InternalAggregations (reducedAggregations );
14281 }
14382
144- /** The fields required to write this addAggregation to xcontent */
145- static class Fields {
146- public static final String AGGREGATIONS = "aggregations" ;
147- }
148-
14983 @ Override
15084 public XContentBuilder toXContent (XContentBuilder builder , Params params ) throws IOException {
15185 if (aggregations .isEmpty ()) {
15286 return builder ;
15387 }
154- builder .startObject (Fields . AGGREGATIONS );
88+ builder .startObject ("aggregations" );
15589 toXContentInternal (builder , params );
15690 return builder .endObject ();
15791 }
15892
15993 /**
160- * Directly write all the addAggregation without their bounding object. Used by sub-addAggregation (non top level addAggregation )
94+ * Directly write all the aggregations without their bounding object. Used by sub-aggregations (non top level aggs )
16195 */
16296 public XContentBuilder toXContentInternal (XContentBuilder builder , Params params ) throws IOException {
16397 for (Aggregation aggregation : aggregations ) {
164- ((InternalAggregation ) aggregation ).toXContent (builder , params );
98+ ((InternalAggregation )aggregation ).toXContent (builder , params );
16599 }
166100 return builder ;
167101 }
168102
103+
169104 public static InternalAggregations readAggregations (StreamInput in ) throws IOException {
170105 InternalAggregations result = new InternalAggregations ();
171106 result .readFrom (in );
@@ -180,13 +115,13 @@ public static InternalAggregations readOptionalAggregations(StreamInput in) thro
180115 public void readFrom (StreamInput in ) throws IOException {
181116 aggregations = in .readList (stream -> in .readNamedWriteable (InternalAggregation .class ));
182117 if (aggregations .isEmpty ()) {
183- aggregationsAsMap = emptyMap ();
118+ aggregationsAsMap = Collections . emptyMap ();
184119 }
185120 }
186121
187122 @ Override
123+ @ SuppressWarnings ("unchecked" )
188124 public void writeTo (StreamOutput out ) throws IOException {
189- out .writeNamedWriteableList (aggregations );
125+ out .writeNamedWriteableList (( List < InternalAggregation >) aggregations );
190126 }
191-
192127}
0 commit comments