3232import java .util .List ;
3333import java .util .Objects ;
3434
35- import static org .elasticsearch .common .xcontent .ConstructingObjectParser .constructorArg ;
3635import static org .elasticsearch .common .xcontent .ConstructingObjectParser .optionalConstructorArg ;
3736
3837/**
@@ -103,23 +102,22 @@ public static class Result implements EvaluationMetric.Result {
103102 @ SuppressWarnings ("unchecked" )
104103 private static final ConstructingObjectParser <Result , Void > PARSER =
105104 new ConstructingObjectParser <>(
106- "multiclass_confusion_matrix_result" , true , a -> new Result ((List <ActualClass >) a [0 ], (long ) a [1 ]));
105+ "multiclass_confusion_matrix_result" , true , a -> new Result ((List <ActualClass >) a [0 ], (Long ) a [1 ]));
107106
108107 static {
109- PARSER .declareObjectArray (constructorArg (), ActualClass .PARSER , CONFUSION_MATRIX );
110- PARSER .declareLong (constructorArg (), OTHER_ACTUAL_CLASS_COUNT );
108+ PARSER .declareObjectArray (optionalConstructorArg (), ActualClass .PARSER , CONFUSION_MATRIX );
109+ PARSER .declareLong (optionalConstructorArg (), OTHER_ACTUAL_CLASS_COUNT );
111110 }
112111
113112 public static Result fromXContent (XContentParser parser ) {
114113 return PARSER .apply (parser , null );
115114 }
116115
117- // Immutable
118116 private final List <ActualClass > confusionMatrix ;
119- private final long otherActualClassCount ;
117+ private final Long otherActualClassCount ;
120118
121- public Result (List <ActualClass > confusionMatrix , long otherActualClassCount ) {
122- this .confusionMatrix = Collections .unmodifiableList (Objects .requireNonNull (confusionMatrix ));
119+ public Result (@ Nullable List <ActualClass > confusionMatrix , @ Nullable Long otherActualClassCount ) {
120+ this .confusionMatrix = confusionMatrix != null ? Collections .unmodifiableList (Objects .requireNonNull (confusionMatrix )) : null ;
123121 this .otherActualClassCount = otherActualClassCount ;
124122 }
125123
@@ -132,15 +130,19 @@ public List<ActualClass> getConfusionMatrix() {
132130 return confusionMatrix ;
133131 }
134132
135- public long getOtherActualClassCount () {
133+ public Long getOtherActualClassCount () {
136134 return otherActualClassCount ;
137135 }
138136
139137 @ Override
140138 public XContentBuilder toXContent (XContentBuilder builder , Params params ) throws IOException {
141139 builder .startObject ();
142- builder .field (CONFUSION_MATRIX .getPreferredName (), confusionMatrix );
143- builder .field (OTHER_ACTUAL_CLASS_COUNT .getPreferredName (), otherActualClassCount );
140+ if (confusionMatrix != null ) {
141+ builder .field (CONFUSION_MATRIX .getPreferredName (), confusionMatrix );
142+ }
143+ if (otherActualClassCount != null ) {
144+ builder .field (OTHER_ACTUAL_CLASS_COUNT .getPreferredName (), otherActualClassCount );
145+ }
144146 builder .endObject ();
145147 return builder ;
146148 }
@@ -151,7 +153,7 @@ public boolean equals(Object o) {
151153 if (o == null || getClass () != o .getClass ()) return false ;
152154 Result that = (Result ) o ;
153155 return Objects .equals (this .confusionMatrix , that .confusionMatrix )
154- && this .otherActualClassCount == that .otherActualClassCount ;
156+ && Objects . equals ( this .otherActualClassCount , that .otherActualClassCount ) ;
155157 }
156158
157159 @ Override
@@ -172,35 +174,45 @@ public static class ActualClass implements ToXContentObject {
172174 new ConstructingObjectParser <>(
173175 "multiclass_confusion_matrix_actual_class" ,
174176 true ,
175- a -> new ActualClass ((String ) a [0 ], (long ) a [1 ], (List <PredictedClass >) a [2 ], (long ) a [3 ]));
177+ a -> new ActualClass ((String ) a [0 ], (Long ) a [1 ], (List <PredictedClass >) a [2 ], (Long ) a [3 ]));
176178
177179 static {
178- PARSER .declareString (constructorArg (), ACTUAL_CLASS );
179- PARSER .declareLong (constructorArg (), ACTUAL_CLASS_DOC_COUNT );
180- PARSER .declareObjectArray (constructorArg (), PredictedClass .PARSER , PREDICTED_CLASSES );
181- PARSER .declareLong (constructorArg (), OTHER_PREDICTED_CLASS_DOC_COUNT );
180+ PARSER .declareString (optionalConstructorArg (), ACTUAL_CLASS );
181+ PARSER .declareLong (optionalConstructorArg (), ACTUAL_CLASS_DOC_COUNT );
182+ PARSER .declareObjectArray (optionalConstructorArg (), PredictedClass .PARSER , PREDICTED_CLASSES );
183+ PARSER .declareLong (optionalConstructorArg (), OTHER_PREDICTED_CLASS_DOC_COUNT );
182184 }
183185
184186 private final String actualClass ;
185- private final long actualClassDocCount ;
187+ private final Long actualClassDocCount ;
186188 private final List <PredictedClass > predictedClasses ;
187- private final long otherPredictedClassDocCount ;
189+ private final Long otherPredictedClassDocCount ;
188190
189- public ActualClass (
190- String actualClass , long actualClassDocCount , List <PredictedClass > predictedClasses , long otherPredictedClassDocCount ) {
191+ public ActualClass (@ Nullable String actualClass ,
192+ @ Nullable Long actualClassDocCount ,
193+ @ Nullable List <PredictedClass > predictedClasses ,
194+ @ Nullable Long otherPredictedClassDocCount ) {
191195 this .actualClass = actualClass ;
192196 this .actualClassDocCount = actualClassDocCount ;
193- this .predictedClasses = Collections .unmodifiableList (predictedClasses );
197+ this .predictedClasses = predictedClasses != null ? Collections .unmodifiableList (predictedClasses ) : null ;
194198 this .otherPredictedClassDocCount = otherPredictedClassDocCount ;
195199 }
196200
197201 @ Override
198202 public XContentBuilder toXContent (XContentBuilder builder , Params params ) throws IOException {
199203 builder .startObject ();
200- builder .field (ACTUAL_CLASS .getPreferredName (), actualClass );
201- builder .field (ACTUAL_CLASS_DOC_COUNT .getPreferredName (), actualClassDocCount );
202- builder .field (PREDICTED_CLASSES .getPreferredName (), predictedClasses );
203- builder .field (OTHER_PREDICTED_CLASS_DOC_COUNT .getPreferredName (), otherPredictedClassDocCount );
204+ if (actualClass != null ) {
205+ builder .field (ACTUAL_CLASS .getPreferredName (), actualClass );
206+ }
207+ if (actualClassDocCount != null ) {
208+ builder .field (ACTUAL_CLASS_DOC_COUNT .getPreferredName (), actualClassDocCount );
209+ }
210+ if (predictedClasses != null ) {
211+ builder .field (PREDICTED_CLASSES .getPreferredName (), predictedClasses );
212+ }
213+ if (otherPredictedClassDocCount != null ) {
214+ builder .field (OTHER_PREDICTED_CLASS_DOC_COUNT .getPreferredName (), otherPredictedClassDocCount );
215+ }
204216 builder .endObject ();
205217 return builder ;
206218 }
@@ -211,9 +223,9 @@ public boolean equals(Object o) {
211223 if (o == null || getClass () != o .getClass ()) return false ;
212224 ActualClass that = (ActualClass ) o ;
213225 return Objects .equals (this .actualClass , that .actualClass )
214- && this .actualClassDocCount == that .actualClassDocCount
226+ && Objects . equals ( this .actualClassDocCount , that .actualClassDocCount )
215227 && Objects .equals (this .predictedClasses , that .predictedClasses )
216- && this .otherPredictedClassDocCount == that .otherPredictedClassDocCount ;
228+ && Objects . equals ( this .otherPredictedClassDocCount , that .otherPredictedClassDocCount ) ;
217229 }
218230
219231 @ Override
@@ -235,26 +247,30 @@ public static class PredictedClass implements ToXContentObject {
235247 @ SuppressWarnings ("unchecked" )
236248 private static final ConstructingObjectParser <PredictedClass , Void > PARSER =
237249 new ConstructingObjectParser <>(
238- "multiclass_confusion_matrix_predicted_class" , true , a -> new PredictedClass ((String ) a [0 ], (long ) a [1 ]));
250+ "multiclass_confusion_matrix_predicted_class" , true , a -> new PredictedClass ((String ) a [0 ], (Long ) a [1 ]));
239251
240252 static {
241- PARSER .declareString (constructorArg (), PREDICTED_CLASS );
242- PARSER .declareLong (constructorArg (), COUNT );
253+ PARSER .declareString (optionalConstructorArg (), PREDICTED_CLASS );
254+ PARSER .declareLong (optionalConstructorArg (), COUNT );
243255 }
244256
245257 private final String predictedClass ;
246258 private final Long count ;
247259
248- public PredictedClass (String predictedClass , Long count ) {
260+ public PredictedClass (@ Nullable String predictedClass , @ Nullable Long count ) {
249261 this .predictedClass = predictedClass ;
250262 this .count = count ;
251263 }
252264
253265 @ Override
254266 public XContentBuilder toXContent (XContentBuilder builder , Params params ) throws IOException {
255267 builder .startObject ();
256- builder .field (PREDICTED_CLASS .getPreferredName (), predictedClass );
257- builder .field (COUNT .getPreferredName (), count );
268+ if (predictedClass != null ) {
269+ builder .field (PREDICTED_CLASS .getPreferredName (), predictedClass );
270+ }
271+ if (count != null ) {
272+ builder .field (COUNT .getPreferredName (), count );
273+ }
258274 builder .endObject ();
259275 return builder ;
260276 }
0 commit comments