43
43
import java .io .IOException ;
44
44
import java .util .Arrays ;
45
45
import java .util .Objects ;
46
+ import java .util .function .Consumer ;
46
47
47
48
public class PercentilesAggregationBuilder extends LeafOnly <ValuesSource .Numeric , PercentilesAggregationBuilder > {
48
49
public static final String NAME = Percentiles .TYPE_NAME ;
@@ -76,7 +77,7 @@ private static class HDROptions {
76
77
NUMBER_SIGNIFICANT_DIGITS_FIELD );
77
78
}
78
79
79
- private static final ObjectParser <PercentilesAggregationBuilder , Void > PARSER ;
80
+ private static final ObjectParser <InternalBuilder , Void > PARSER ;
80
81
static {
81
82
PARSER = new ObjectParser <>(PercentilesAggregationBuilder .NAME );
82
83
ValuesSourceParserHelper .declareNumericFields (PARSER , true , true , false );
@@ -103,7 +104,26 @@ private static class HDROptions {
103
104
}
104
105
105
106
public static AggregationBuilder parse (String aggregationName , XContentParser parser ) throws IOException {
106
- return PARSER .parse (parser , new PercentilesAggregationBuilder (aggregationName ), null );
107
+ InternalBuilder internal = PARSER .parse (parser , new InternalBuilder (aggregationName ), null );
108
+ // we need to return a PercentilesAggregationBuilder for equality checks to work
109
+ PercentilesAggregationBuilder returnedAgg = new PercentilesAggregationBuilder (internal .name );
110
+ setIfNotNull (returnedAgg ::valueType , internal .valueType ());
111
+ setIfNotNull (returnedAgg ::format , internal .format ());
112
+ setIfNotNull (returnedAgg ::missing , internal .missing ());
113
+ setIfNotNull (returnedAgg ::field , internal .field ());
114
+ setIfNotNull (returnedAgg ::script , internal .script ());
115
+ setIfNotNull (returnedAgg ::method , internal .method ());
116
+ setIfNotNull (returnedAgg ::percentiles , internal .percentiles ());
117
+ returnedAgg .keyed (internal .keyed ());
118
+ returnedAgg .compression (internal .compression ());
119
+ returnedAgg .numberOfSignificantValueDigits (internal .numberOfSignificantValueDigits ());
120
+ return returnedAgg ;
121
+ }
122
+
123
+ private static <T > void setIfNotNull (Consumer <T > consumer , T value ) {
124
+ if (value != null ) {
125
+ consumer .accept (value );
126
+ }
107
127
}
108
128
109
129
private double [] percents = DEFAULT_PERCENTS ;
@@ -144,6 +164,9 @@ public PercentilesAggregationBuilder percentiles(double... percents) {
144
164
if (percents == null ) {
145
165
throw new IllegalArgumentException ("[percents] must not be null: [" + name + "]" );
146
166
}
167
+ if (percents .length == 0 ) {
168
+ throw new IllegalArgumentException ("[percents] must not be empty: [" + name + "]" );
169
+ }
147
170
double [] sortedPercents = Arrays .copyOf (percents , percents .length );
148
171
Arrays .sort (sortedPercents );
149
172
this .percents = sortedPercents ;
@@ -293,4 +316,29 @@ protected int innerHashCode() {
293
316
public String getType () {
294
317
return NAME ;
295
318
}
319
+
320
+ /**
321
+ * Private specialization of this builder that should only be used by the parser, this enables us to
322
+ * overwrite {@link #method()} to check that it is not defined twice in xContent and throw
323
+ * an error, while the Java API should allow to overwrite the method
324
+ */
325
+ private static class InternalBuilder extends PercentilesAggregationBuilder {
326
+
327
+ private boolean setOnce = false ;
328
+
329
+ private InternalBuilder (String name ) {
330
+ super (name );
331
+ }
332
+
333
+ @ Override
334
+ public InternalBuilder method (PercentilesMethod method ) {
335
+ if (setOnce == false ) {
336
+ super .method (method );
337
+ setOnce = true ;
338
+ return this ;
339
+ } else {
340
+ throw new IllegalStateException ("Only one percentiles method should be declared." );
341
+ }
342
+ }
343
+ }
296
344
}
0 commit comments