@@ -51,13 +51,13 @@ static Map<String, FieldSpecificMatcher> matchers(
5151 return new HashMap <>() {
5252 {
5353 put ("keyword" , new KeywordMatcher (actualMappings , actualSettings , expectedMappings , expectedSettings ));
54- put ("long" , new NumberMatcher ( "long" , actualMappings , actualSettings , expectedMappings , expectedSettings ));
54+ put ("long" , new LongMatcher ( actualMappings , actualSettings , expectedMappings , expectedSettings ));
5555 put ("unsigned_long" , new UnsignedLongMatcher (actualMappings , actualSettings , expectedMappings , expectedSettings ));
56- put ("integer" , new NumberMatcher ( "integer" , actualMappings , actualSettings , expectedMappings , expectedSettings ));
57- put ("short" , new NumberMatcher ( "short" , actualMappings , actualSettings , expectedMappings , expectedSettings ));
58- put ("byte" , new NumberMatcher ( "byte" , actualMappings , actualSettings , expectedMappings , expectedSettings ));
59- put ("double" , new NumberMatcher ( "double" , actualMappings , actualSettings , expectedMappings , expectedSettings ));
60- put ("float" , new NumberMatcher ( "float" , actualMappings , actualSettings , expectedMappings , expectedSettings ));
56+ put ("integer" , new IntegerMatcher ( actualMappings , actualSettings , expectedMappings , expectedSettings ));
57+ put ("short" , new ShortMatcher ( actualMappings , actualSettings , expectedMappings , expectedSettings ));
58+ put ("byte" , new ByteMatcher ( actualMappings , actualSettings , expectedMappings , expectedSettings ));
59+ put ("double" , new DoubleMatcher ( actualMappings , actualSettings , expectedMappings , expectedSettings ));
60+ put ("float" , new FloatMatcher ( actualMappings , actualSettings , expectedMappings , expectedSettings ));
6161 put ("half_float" , new HalfFloatMatcher (actualMappings , actualSettings , expectedMappings , expectedSettings ));
6262 put ("scaled_float" , new ScaledFloatMatcher (actualMappings , actualSettings , expectedMappings , expectedSettings ));
6363 put ("counted_keyword" , new CountedKeywordMatcher (actualMappings , actualSettings , expectedMappings , expectedSettings ));
@@ -362,21 +362,19 @@ Object convert(Object value, Object nullValue) {
362362 }
363363 }
364364
365- class NumberMatcher extends GenericMappingAwareMatcher {
365+ abstract class NumberMatcher extends GenericMappingAwareMatcher {
366366
367- private final FieldType fieldType ;
368367 private final NumberFieldMapper .NumberType numberType ;
369368
370- NumberMatcher (
371- String fieldType ,
369+ private NumberMatcher (
370+ FieldType fieldType ,
372371 XContentBuilder actualMappings ,
373372 Settings .Builder actualSettings ,
374373 XContentBuilder expectedMappings ,
375374 Settings .Builder expectedSettings
376375 ) {
377- super (fieldType , actualMappings , actualSettings , expectedMappings , expectedSettings );
378- this .fieldType = FieldType .tryParse (fieldType );
379- this .numberType = NumberFieldMapper .NumberType .valueOf (this .fieldType .name ());
376+ super (fieldType .toString (), actualMappings , actualSettings , expectedMappings , expectedSettings );
377+ this .numberType = NumberFieldMapper .NumberType .valueOf (fieldType .name ());
380378 }
381379
382380 @ Override
@@ -407,22 +405,75 @@ Object convert(Object value, Object nullValue) {
407405 // When a number mapping is coerced, the expected value will come from the above parser and will have the correct java type.
408406 // Whereas, if it fits, the actual value will be in an Integer or a Double. To correctly treat expected and actual values as
409407 // equal the actual value must be cast to the appropriate type.
410- private Object cast (Object value ) {
411- if (value instanceof Integer v ) {
412- return switch (fieldType ) {
413- case LONG -> v .longValue ();
414- case SHORT -> v .shortValue ();
415- case BYTE -> v .byteValue ();
416- default -> value ;
417- };
418- }
419- if (value instanceof Double v ) {
420- return fieldType == FieldType .FLOAT ? v .floatValue () : value ;
421- }
408+ abstract Object cast (Object value );
409+ }
410+
411+ class LongMatcher extends NumberMatcher {
412+ LongMatcher (XContentBuilder actualMappings , Settings .Builder actualSettings , XContentBuilder expectedMappings , Settings .Builder expectedSettings ) {
413+ super (FieldType .LONG , actualMappings , actualSettings , expectedMappings , expectedSettings );
414+ }
415+
416+ @ Override
417+ protected Object cast (Object value ) {
418+ return value instanceof Integer v ? v .longValue () : value ;
419+ }
420+ }
421+
422+ class IntegerMatcher extends NumberMatcher {
423+ IntegerMatcher (XContentBuilder actualMappings , Settings .Builder actualSettings , XContentBuilder expectedMappings , Settings .Builder expectedSettings ) {
424+ super (FieldType .INTEGER , actualMappings , actualSettings , expectedMappings , expectedSettings );
425+ }
426+
427+ @ Override
428+ protected Object cast (Object value ) {
429+ return value ;
430+ }
431+ }
432+
433+ class ShortMatcher extends NumberMatcher {
434+ ShortMatcher (XContentBuilder actualMappings , Settings .Builder actualSettings , XContentBuilder expectedMappings , Settings .Builder expectedSettings ) {
435+ super (FieldType .SHORT , actualMappings , actualSettings , expectedMappings , expectedSettings );
436+ }
437+
438+ @ Override
439+ protected Object cast (Object value ) {
440+ return value instanceof Integer v ? v .shortValue () : value ;
441+ }
442+ }
443+
444+ class ByteMatcher extends NumberMatcher {
445+ ByteMatcher (XContentBuilder actualMappings , Settings .Builder actualSettings , XContentBuilder expectedMappings , Settings .Builder expectedSettings ) {
446+ super (FieldType .BYTE , actualMappings , actualSettings , expectedMappings , expectedSettings );
447+ }
448+
449+ @ Override
450+ protected Object cast (Object value ) {
451+ return value instanceof Integer v ? v .byteValue () : value ;
452+ }
453+ }
454+
455+ class DoubleMatcher extends NumberMatcher {
456+ DoubleMatcher (XContentBuilder actualMappings , Settings .Builder actualSettings , XContentBuilder expectedMappings , Settings .Builder expectedSettings ) {
457+ super (FieldType .DOUBLE , actualMappings , actualSettings , expectedMappings , expectedSettings );
458+ }
459+
460+ @ Override
461+ protected Object cast (Object value ) {
422462 return value ;
423463 }
424464 }
425465
466+ class FloatMatcher extends NumberMatcher {
467+ FloatMatcher (XContentBuilder actualMappings , Settings .Builder actualSettings , XContentBuilder expectedMappings , Settings .Builder expectedSettings ) {
468+ super (FieldType .FLOAT , actualMappings , actualSettings , expectedMappings , expectedSettings );
469+ }
470+
471+ @ Override
472+ protected Object cast (Object value ) {
473+ return value instanceof Integer v ? v .floatValue () : value ;
474+ }
475+ }
476+
426477 class BooleanMatcher extends GenericMappingAwareMatcher {
427478 BooleanMatcher (
428479 XContentBuilder actualMappings ,
0 commit comments