public class BuilderGenerationHelper extends ClassGenerationHelper
You should consider to use a builder when you are faced with many constructor
parameters. Specially mandatory and optional parameters. Here comes the
BuilderGenerationHelper
into play.
Instead of making the desired object directly, the client calls a constructor (or static factory) with all of the required parameters and gets a builder object. Then the client calls setter-like methods on the builder object to set each optional parameter of interest. Finally, the client calls a parameterless build method to generate the object, which is immutable. The builder is a static member class of the class it builds. This class offers a method to generate a builder like this.
Rules: No optional fields - no Builder. Has required fields and optional fields - Constructor for required fields + Builder. Has optional fields - only Builder.
http://my.safaribooksonline.com/9780137150021/ch02lev1sec2
Constructor and Description |
---|
BuilderGenerationHelper(Writer sourceWriter) |
Modifier and Type | Method and Description |
---|---|
protected void |
generateBuilderBuildMethod(String builderObjectSimpleClassName)
Helper method to create the Builder#build() method which returns a concrete object instance.
|
void |
generateBuilderClass(String builderObjectSimpleClassName,
Collection<VariableElement> requiredFields,
Collection<VariableElement> optionalFields,
String... interfaces) |
protected void |
generateBuilderFieldAssignment(VariableElement fieldElement,
Object value) |
protected void |
generateBuilderHeader() |
protected void |
generateBuilderInvocationMethod(VariableElement fieldElement) |
void |
generateCustomBuilderConstructor(String customClassName,
Collection<VariableElement> fieldsToBePassedAndAssigned)
Creates a private object constructor which should only ever be called by the intern Builder class.
|
generateClassHeader, generateConstructorUsingFields, generateEmptyConstructor, generateEquals, generateFieldAccessors, generateFieldAssignment, generateHashCode, generateToString
close, determineFinalConstantValue, determineWrapperClass, firstCharToUpperCase, generateAnnotation, generateConstantFieldDeclaration, generateFieldDeclaration, generateFieldDeclaration, generateFieldDeclarations, generateFieldList, generateFooter, generateImports, generateImports, generateModifierList, generatePackageDeclaration, implode, isConstant, isFinal, isPrimitive, isStatic, manufactureAccessorName, manufactureField, manufactureIndentation, manufactureSetterName, manufactureSetterName, print, print, println, println, println, printWithoutSpaces, replaceParameters, resetWhitespaces, setWhitespaces
public BuilderGenerationHelper(Writer sourceWriter)
public void generateBuilderClass(String builderObjectSimpleClassName, Collection<VariableElement> requiredFields, Collection<VariableElement> optionalFields, String... interfaces)
public void generateCustomBuilderConstructor(String customClassName, Collection<VariableElement> fieldsToBePassedAndAssigned)
customClassName
- the object class namefieldsToBePassedAndAssigned
- the fieldsprotected void generateBuilderBuildMethod(String builderObjectSimpleClassName)
builderObjectSimpleClassName
- the object class nameprotected void generateBuilderFieldAssignment(VariableElement fieldElement, Object value)
fieldElement
- value
- protected void generateBuilderHeader()
protected void generateBuilderInvocationMethod(VariableElement fieldElement)
Copyright © 2010–2014 Arcbees. All rights reserved.