1212import org .elasticsearch .common .io .stream .StreamInput ;
1313import org .elasticsearch .common .io .stream .StreamOutput ;
1414import org .elasticsearch .common .io .stream .Writeable ;
15- import org .elasticsearch .common .xcontent .ObjectParser ;
16- import org .elasticsearch .common .xcontent .ToXContentFragment ;
15+ import org .elasticsearch .common .xcontent .ConstructingObjectParser ;
16+ import org .elasticsearch .common .xcontent .ToXContentObject ;
1717import org .elasticsearch .common .xcontent .XContentBuilder ;
18+ import org .elasticsearch .common .xcontent .XContentParser ;
1819import org .elasticsearch .search .aggregations .metrics .avg .AvgAggregationBuilder ;
1920import org .elasticsearch .search .aggregations .metrics .max .MaxAggregationBuilder ;
2021import org .elasticsearch .search .aggregations .metrics .min .MinAggregationBuilder ;
3233import java .util .Objects ;
3334import java .util .stream .Collectors ;
3435
36+ import static org .elasticsearch .common .xcontent .ConstructingObjectParser .constructorArg ;
37+
3538/**
3639 * The configuration object for the metrics portion of a rollup job config
3740 *
4851 * ]
4952 * }
5053 */
51- public class MetricConfig implements Writeable , ToXContentFragment {
52- private static final String NAME = "metric_config" ;
53-
54- private String field ;
55- private List <String > metrics ;
56-
57- private static final ParseField FIELD = new ParseField ("field" );
58- private static final ParseField METRICS = new ParseField ("metrics" );
54+ public class MetricConfig implements Writeable , ToXContentObject {
5955
6056 // TODO: replace these with an enum
6157 private static final ParseField MIN = new ParseField ("min" );
@@ -64,27 +60,54 @@ public class MetricConfig implements Writeable, ToXContentFragment {
6460 private static final ParseField AVG = new ParseField ("avg" );
6561 private static final ParseField VALUE_COUNT = new ParseField ("value_count" );
6662
67- public static final ObjectParser <MetricConfig .Builder , Void > PARSER = new ObjectParser <>(NAME , MetricConfig .Builder ::new );
68-
63+ private static final String NAME = "metrics" ;
64+ private static final String FIELD = "field" ;
65+ private static final String METRICS = "metrics" ;
66+ private static final ConstructingObjectParser <MetricConfig , Void > PARSER ;
6967 static {
70- PARSER .declareString (MetricConfig .Builder ::setField , FIELD );
71- PARSER .declareStringArray (MetricConfig .Builder ::setMetrics , METRICS );
68+ PARSER = new ConstructingObjectParser <>(NAME , args -> {
69+ @ SuppressWarnings ("unchecked" ) List <String > metrics = (List <String >) args [1 ];
70+ return new MetricConfig ((String ) args [0 ], metrics );
71+ });
72+ PARSER .declareString (constructorArg (), new ParseField (FIELD ));
73+ PARSER .declareStringArray (constructorArg (), new ParseField (METRICS ));
7274 }
7375
74- MetricConfig (String name , List <String > metrics ) {
75- this .field = name ;
76+ private final String field ;
77+ private final List <String > metrics ;
78+
79+ public MetricConfig (final String field , final List <String > metrics ) {
80+ if (field == null || field .isEmpty ()) {
81+ throw new IllegalArgumentException ("Field must be a non-null, non-empty string" );
82+ }
83+ if (metrics == null || metrics .isEmpty ()) {
84+ throw new IllegalArgumentException ("Metrics must be a non-null, non-empty array of strings" );
85+ }
86+ metrics .forEach (m -> {
87+ if (RollupField .SUPPORTED_METRICS .contains (m ) == false ) {
88+ throw new IllegalArgumentException ("Unsupported metric [" + m + "]. " +
89+ "Supported metrics include: " + RollupField .SUPPORTED_METRICS );
90+ }
91+ });
92+ this .field = field ;
7693 this .metrics = metrics ;
7794 }
7895
79- MetricConfig (StreamInput in ) throws IOException {
96+ MetricConfig (final StreamInput in ) throws IOException {
8097 field = in .readString ();
8198 metrics = in .readList (StreamInput ::readString );
8299 }
83100
101+ /**
102+ * @return the name of the field used in the metric configuration. Never {@code null}.
103+ */
84104 public String getField () {
85105 return field ;
86106 }
87107
108+ /**
109+ * @return the names of the metrics used in the metric configuration. Never {@code null}.
110+ */
88111 public List <String > getMetrics () {
89112 return metrics ;
90113 }
@@ -159,10 +182,13 @@ public void validateMappings(Map<String, Map<String, FieldCapabilities>> fieldCa
159182 }
160183
161184 @ Override
162- public XContentBuilder toXContent (XContentBuilder builder , Params params ) throws IOException {
163- builder .field (FIELD .getPreferredName (), field );
164- builder .field (METRICS .getPreferredName (), metrics );
165- return builder ;
185+ public XContentBuilder toXContent (final XContentBuilder builder , final Params params ) throws IOException {
186+ builder .startObject ();
187+ {
188+ builder .field (FIELD , field );
189+ builder .field (METRICS , metrics );
190+ }
191+ return builder .endObject ();
166192 }
167193
168194 @ Override
@@ -172,19 +198,16 @@ public void writeTo(StreamOutput out) throws IOException {
172198 }
173199
174200 @ Override
175- public boolean equals (Object other ) {
201+ public boolean equals (final Object other ) {
176202 if (this == other ) {
177203 return true ;
178204 }
179-
180205 if (other == null || getClass () != other .getClass ()) {
181206 return false ;
182207 }
183208
184- MetricConfig that = (MetricConfig ) other ;
185-
186- return Objects .equals (this .field , that .field )
187- && Objects .equals (this .metrics , that .metrics );
209+ final MetricConfig that = (MetricConfig ) other ;
210+ return Objects .equals (field , that .field ) && Objects .equals (metrics , that .metrics );
188211 }
189212
190213 @ Override
@@ -197,52 +220,7 @@ public String toString() {
197220 return Strings .toString (this , true , true );
198221 }
199222
200-
201- public static class Builder {
202- private String field ;
203- private List <String > metrics ;
204-
205- public Builder () {
206- }
207-
208- public Builder (MetricConfig config ) {
209- this .field = config .getField ();
210- this .metrics = config .getMetrics ();
211- }
212-
213- public String getField () {
214- return field ;
215- }
216-
217- public MetricConfig .Builder setField (String field ) {
218- this .field = field ;
219- return this ;
220- }
221-
222- public List <String > getMetrics () {
223- return metrics ;
224- }
225-
226- public MetricConfig .Builder setMetrics (List <String > metrics ) {
227- this .metrics = metrics ;
228- return this ;
229- }
230-
231- public MetricConfig build () {
232- if (Strings .isNullOrEmpty (field ) == true ) {
233- throw new IllegalArgumentException ("Parameter [" + FIELD .getPreferredName () + "] must be a non-null, non-empty string." );
234- }
235- if (metrics == null || metrics .isEmpty ()) {
236- throw new IllegalArgumentException ("Parameter [" + METRICS .getPreferredName ()
237- + "] must be a non-null, non-empty array of strings." );
238- }
239- metrics .forEach (m -> {
240- if (RollupField .SUPPORTED_METRICS .contains (m ) == false ) {
241- throw new IllegalArgumentException ("Unsupported metric [" + m + "]. " +
242- "Supported metrics include: " + RollupField .SUPPORTED_METRICS );
243- }
244- });
245- return new MetricConfig (field , metrics );
246- }
223+ public static MetricConfig fromXContent (final XContentParser parser ) throws IOException {
224+ return PARSER .parse (parser , null );
247225 }
248226}
0 commit comments