@@ -262,7 +262,8 @@ def printReadProperties(c):
262262 for f in c .fields :
263263 (jfName , jfType , jfClass ) = (java_field_name (f .name ), java_field_type (spec , f .domain ), java_class_name (f .domain ))
264264 if jfType in java_scalar_types :
265- print " if (this.%sIsSet) this.%s = reader.read%s();" % (jfName , jfName , jfClass )
265+ print " if (this.%sIsSet) { this.%s = reader.read%s(); }" % (jfName , jfName , jfClass )
266+ print " else { this.%s = %s; }" % (jfName , java_scalar_default (jfType ))
266267 else :
267268 print " this.%s = %s_present ? reader.read%s() : null;" % (jfName , jfName , jfClass )
268269
@@ -354,15 +355,25 @@ def ctorParm(field):
354355
355356 print " }"
356357
358+ def printPropertiesBuilder (c ):
359+ print
360+ print " public Builder builder() {"
361+ print " Builder builder = new Builder();"
362+ setFieldList = [ "%s(%s)" % (fn , fn )
363+ for fn in [ java_field_name (f .name )
364+ for f in c .fields if java_field_type (spec , f .domain ) not in java_scalar_types ] ]
365+ print " builder.%s;" % ("\n ." .join (setFieldList ))
366+ for f in c .fields :
367+ if java_field_type (spec , f .domain ) in java_scalar_types :
368+ fn = java_field_name (f .name )
369+ print " if (%sIsSet) { builder.%s(%s); }" % (fn , fn , fn )
370+ print " return builder;"
371+ print " }"
372+
357373 def printPropertiesClass (c ):
358- def printGetterAndSetter (fieldType , fieldName ):
374+ def printGetter (fieldType , fieldName ):
359375 capFieldName = fieldName [0 ].upper () + fieldName [1 :]
360376 print " public %s get%s() { return this.%s; }" % (fieldType , capFieldName , fieldName )
361- if fieldType in java_scalar_types :
362- print " public void set%s(%s %s)" % (capFieldName , fieldType , fieldName )
363- print " { this.%s = %s; this.%sIsSet = true; }" % (fieldName , fieldName , fieldName )
364- else :
365- print " public void set%s(%s %s) { this.%s = %s; }" % (capFieldName , fieldType , fieldName , fieldName , fieldName )
366377
367378 jClassName = java_class_name (c .name )
368379
@@ -372,8 +383,8 @@ def printGetterAndSetter(fieldType, fieldName):
372383 for f in c .fields :
373384 (fType , fName ) = (java_field_type (spec , f .domain ), java_field_name (f .name ))
374385 if fType in java_scalar_types :
375- print " private boolean %sIsSet = false ;" % (fName )
376- print " private %s %s;" % (fType , fName )
386+ print " private final boolean %sIsSet;" % (fName )
387+ print " private final %s %s;" % (fType , fName )
377388
378389 #explicit constructor
379390 if c .fields :
@@ -409,10 +420,12 @@ def printGetterAndSetter(fieldType, fieldName):
409420 print " public int getClassId() { return %i; }" % (c .index )
410421 print " public String getClassName() { return \" %s\" ; }" % (c .name )
411422
423+ printPropertiesBuilder (c )
424+
412425 #accessor methods
413426 print
414427 for f in c .fields :
415- printGetterAndSetter (java_field_type (spec , f .domain ), java_field_name (f .name ))
428+ printGetter (java_field_type (spec , f .domain ), java_field_name (f .name ))
416429
417430 printWritePropertiesTo (c )
418431 printAppendArgumentDebugStringTo (c )
0 commit comments