2323import java .util .List ;
2424import java .util .Map ;
2525import java .util .Objects ;
26- import java .util .function .Function ;
2726
2827/**
2928 * Serialization and merge logic for {@link GeoCentroidAggregator}.
3029 */
3130public abstract class InternalCentroid extends InternalAggregation implements CentroidAggregation {
3231 protected final SpatialPoint centroid ;
3332 protected final long count ;
34- private final FieldExtractor firstField ;
35- private final FieldExtractor secondField ;
36-
37- public InternalCentroid (
38- String name ,
39- SpatialPoint centroid ,
40- long count ,
41- Map <String , Object > metadata ,
42- FieldExtractor firstField ,
43- FieldExtractor secondField
44- ) {
33+
34+ public InternalCentroid (String name , SpatialPoint centroid , long count , Map <String , Object > metadata ) {
4535 super (name , metadata );
4636 assert (centroid == null ) == (count == 0 );
4737 this .centroid = centroid ;
4838 assert count >= 0 ;
4939 this .count = count ;
50- this .firstField = firstField ;
51- this .secondField = secondField ;
5240 }
5341
5442 protected abstract SpatialPoint centroidFromStream (StreamInput in ) throws IOException ;
@@ -59,16 +47,14 @@ public InternalCentroid(
5947 * Read from a stream.
6048 */
6149 @ SuppressWarnings ("this-escape" )
62- protected InternalCentroid (StreamInput in , FieldExtractor firstField , FieldExtractor secondField ) throws IOException {
50+ protected InternalCentroid (StreamInput in ) throws IOException {
6351 super (in );
6452 count = in .readVLong ();
6553 if (in .readBoolean ()) {
6654 centroid = centroidFromStream (in );
6755 } else {
6856 centroid = null ;
6957 }
70- this .firstField = firstField ;
71- this .secondField = secondField ;
7258 }
7359
7460 @ Override
@@ -110,11 +96,11 @@ public void accept(InternalAggregation aggregation) {
11096 if (centroidAgg .count > 0 ) {
11197 totalCount += centroidAgg .count ;
11298 if (Double .isNaN (firstSum )) {
113- firstSum = centroidAgg .count * firstField . extractor . apply (centroidAgg .centroid );
114- secondSum = centroidAgg .count * secondField . extractor . apply (centroidAgg .centroid );
99+ firstSum = centroidAgg .count * extractFirst (centroidAgg .centroid );
100+ secondSum = centroidAgg .count * extractSecond (centroidAgg .centroid );
115101 } else {
116- firstSum += centroidAgg .count * firstField . extractor . apply (centroidAgg .centroid );
117- secondSum += centroidAgg .count * secondField . extractor . apply (centroidAgg .centroid );
102+ firstSum += centroidAgg .count * extractFirst (centroidAgg .centroid );
103+ secondSum += centroidAgg .count * extractSecond (centroidAgg .centroid );
118104 }
119105 }
120106 }
@@ -126,6 +112,14 @@ public InternalAggregation get() {
126112 };
127113 }
128114
115+ protected abstract String nameFirst ();
116+
117+ protected abstract double extractFirst (SpatialPoint point );
118+
119+ protected abstract String nameSecond ();
120+
121+ protected abstract double extractSecond (SpatialPoint point );
122+
129123 @ Override
130124 public InternalAggregation finalizeSampling (SamplingContext samplingContext ) {
131125 return copyWith (centroid , samplingContext .scaleUp (count ));
@@ -136,16 +130,6 @@ protected boolean mustReduceOnSingleInternalAgg() {
136130 return false ;
137131 }
138132
139- protected static class FieldExtractor {
140- private final String name ;
141- private final Function <SpatialPoint , Double > extractor ;
142-
143- public FieldExtractor (String name , Function <SpatialPoint , Double > extractor ) {
144- this .name = name ;
145- this .extractor = extractor ;
146- }
147- }
148-
149133 protected abstract double extractDouble (String name );
150134
151135 @ Override
@@ -174,8 +158,8 @@ public XContentBuilder doXContentBody(XContentBuilder builder, Params params) th
174158 if (centroid != null ) {
175159 builder .startObject (Fields .CENTROID .getPreferredName ());
176160 {
177- builder .field (firstField . name , firstField . extractor . apply (centroid ));
178- builder .field (secondField . name , secondField . extractor . apply (centroid ));
161+ builder .field (nameFirst (), extractFirst (centroid ));
162+ builder .field (nameSecond (), extractSecond (centroid ));
179163 }
180164 builder .endObject ();
181165 }
0 commit comments