32
32
import java .util .List ;
33
33
import java .util .Objects ;
34
34
35
- import static org .elasticsearch .common .xcontent .ConstructingObjectParser .constructorArg ;
36
35
import static org .elasticsearch .common .xcontent .ConstructingObjectParser .optionalConstructorArg ;
37
36
38
37
/**
@@ -103,23 +102,22 @@ public static class Result implements EvaluationMetric.Result {
103
102
@ SuppressWarnings ("unchecked" )
104
103
private static final ConstructingObjectParser <Result , Void > PARSER =
105
104
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 ]));
107
106
108
107
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 );
111
110
}
112
111
113
112
public static Result fromXContent (XContentParser parser ) {
114
113
return PARSER .apply (parser , null );
115
114
}
116
115
117
- // Immutable
118
116
private final List <ActualClass > confusionMatrix ;
119
- private final long otherActualClassCount ;
117
+ private final Long otherActualClassCount ;
120
118
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 ;
123
121
this .otherActualClassCount = otherActualClassCount ;
124
122
}
125
123
@@ -132,15 +130,19 @@ public List<ActualClass> getConfusionMatrix() {
132
130
return confusionMatrix ;
133
131
}
134
132
135
- public long getOtherActualClassCount () {
133
+ public Long getOtherActualClassCount () {
136
134
return otherActualClassCount ;
137
135
}
138
136
139
137
@ Override
140
138
public XContentBuilder toXContent (XContentBuilder builder , Params params ) throws IOException {
141
139
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
+ }
144
146
builder .endObject ();
145
147
return builder ;
146
148
}
@@ -151,7 +153,7 @@ public boolean equals(Object o) {
151
153
if (o == null || getClass () != o .getClass ()) return false ;
152
154
Result that = (Result ) o ;
153
155
return Objects .equals (this .confusionMatrix , that .confusionMatrix )
154
- && this .otherActualClassCount == that .otherActualClassCount ;
156
+ && Objects . equals ( this .otherActualClassCount , that .otherActualClassCount ) ;
155
157
}
156
158
157
159
@ Override
@@ -172,35 +174,45 @@ public static class ActualClass implements ToXContentObject {
172
174
new ConstructingObjectParser <>(
173
175
"multiclass_confusion_matrix_actual_class" ,
174
176
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 ]));
176
178
177
179
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 );
182
184
}
183
185
184
186
private final String actualClass ;
185
- private final long actualClassDocCount ;
187
+ private final Long actualClassDocCount ;
186
188
private final List <PredictedClass > predictedClasses ;
187
- private final long otherPredictedClassDocCount ;
189
+ private final Long otherPredictedClassDocCount ;
188
190
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 ) {
191
195
this .actualClass = actualClass ;
192
196
this .actualClassDocCount = actualClassDocCount ;
193
- this .predictedClasses = Collections .unmodifiableList (predictedClasses );
197
+ this .predictedClasses = predictedClasses != null ? Collections .unmodifiableList (predictedClasses ) : null ;
194
198
this .otherPredictedClassDocCount = otherPredictedClassDocCount ;
195
199
}
196
200
197
201
@ Override
198
202
public XContentBuilder toXContent (XContentBuilder builder , Params params ) throws IOException {
199
203
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
+ }
204
216
builder .endObject ();
205
217
return builder ;
206
218
}
@@ -211,9 +223,9 @@ public boolean equals(Object o) {
211
223
if (o == null || getClass () != o .getClass ()) return false ;
212
224
ActualClass that = (ActualClass ) o ;
213
225
return Objects .equals (this .actualClass , that .actualClass )
214
- && this .actualClassDocCount == that .actualClassDocCount
226
+ && Objects . equals ( this .actualClassDocCount , that .actualClassDocCount )
215
227
&& Objects .equals (this .predictedClasses , that .predictedClasses )
216
- && this .otherPredictedClassDocCount == that .otherPredictedClassDocCount ;
228
+ && Objects . equals ( this .otherPredictedClassDocCount , that .otherPredictedClassDocCount ) ;
217
229
}
218
230
219
231
@ Override
@@ -235,26 +247,30 @@ public static class PredictedClass implements ToXContentObject {
235
247
@ SuppressWarnings ("unchecked" )
236
248
private static final ConstructingObjectParser <PredictedClass , Void > PARSER =
237
249
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 ]));
239
251
240
252
static {
241
- PARSER .declareString (constructorArg (), PREDICTED_CLASS );
242
- PARSER .declareLong (constructorArg (), COUNT );
253
+ PARSER .declareString (optionalConstructorArg (), PREDICTED_CLASS );
254
+ PARSER .declareLong (optionalConstructorArg (), COUNT );
243
255
}
244
256
245
257
private final String predictedClass ;
246
258
private final Long count ;
247
259
248
- public PredictedClass (String predictedClass , Long count ) {
260
+ public PredictedClass (@ Nullable String predictedClass , @ Nullable Long count ) {
249
261
this .predictedClass = predictedClass ;
250
262
this .count = count ;
251
263
}
252
264
253
265
@ Override
254
266
public XContentBuilder toXContent (XContentBuilder builder , Params params ) throws IOException {
255
267
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
+ }
258
274
builder .endObject ();
259
275
return builder ;
260
276
}
0 commit comments