1111import org .apache .commons .lang3 .ArrayUtils ;
1212
1313final class PojoBuilder <PropertiesT extends Record > implements PropertiesBuilder <PropertiesT > {
14+ private static final Map <Class <?>, Object > PRIMITIVE_DEFAULTS ;
15+
16+ static {
17+ PRIMITIVE_DEFAULTS = Map .of (
18+ boolean .class , false ,
19+ short .class , (short ) 0 ,
20+ int .class , 0 ,
21+ long .class , 0L ,
22+ float .class , 0f ,
23+ double .class , 0d );
24+ }
25+
1426 private final PojoDescriptor <PropertiesT > descriptor ;
1527 private final Constructor <PropertiesT > ctor ;
1628 private final Map <String , Arg > ctorArgs ;
1729
1830 static record Arg (Class <?> type , Object value ) {
19- Arg withValue (Object value ) {
20- return new Arg (this .type , value );
21- }
22-
23- public Object value () {
24- if (value != null ) {
25- return value ;
26- }
27-
28- if (type == boolean .class ) {
29- return false ;
30- } else if (type == short .class ) {
31- return (short ) 0 ;
32- } else if (type == int .class ) {
33- return 0 ;
34- } else if (type == long .class ) {
35- return 0L ;
36- } else if (type == float .class ) {
37- return 0f ;
38- } else if (type == double .class ) {
39- return 0d ;
31+ /**
32+ * Create a new Arg, replacing a null value with
33+ * default if the type is a known primitive class.
34+ */
35+ static Arg withPrimitiveDefault (Class <?> type , Object value ) {
36+ if (PRIMITIVE_DEFAULTS .containsKey (type )) {
37+ return new Arg (type , PRIMITIVE_DEFAULTS .get (type ));
4038 }
39+ return new Arg (type , value );
40+ }
4141
42- throw new IllegalArgumentException (type .getName () + " property data type is not supported" );
42+ Arg withValue (Object value ) {
43+ return new Arg (this .type , value );
4344 }
4445 }
4546
@@ -53,7 +54,7 @@ public Object value() {
5354 .map (arg -> {
5455 // LinkedHashMap allows null values.
5556 var type = arg .getType ();
56- ctorArgs .put (arg .getName (), new Arg (type , null ));
57+ ctorArgs .put (arg .getName (), Arg . withPrimitiveDefault (type , null ));
5758 return type ;
5859 })
5960 .toArray (Class <?>[]::new );
0 commit comments