Skip to content

Commit

Permalink
Replaced the constructor with a builder constructor.
Browse files Browse the repository at this point in the history
Because what could possibly be better advertisement for a plugin than to use its features in the plugin itself. :)
  • Loading branch information
ZeroOne3010 committed Feb 17, 2013
1 parent 03bd3aa commit 421d77b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
Expand Up @@ -31,14 +31,6 @@ public class BuilderGenerator implements Generator {
private final boolean createBuilderConstructor;
private final boolean createCopyConstructor;
private final boolean formatSource;
private final IJavaProject javaProject;

public BuilderGenerator(boolean createBuilderConstructor, boolean createCopyConstructor, boolean formatSource) {
this.createBuilderConstructor = createBuilderConstructor;
this.createCopyConstructor = createCopyConstructor;
this.formatSource = formatSource;
javaProject = JavaCore.create(ResourcesPlugin.getWorkspace().getRoot().getProject());
}

public void generate(ICompilationUnit cu, List<IField> fields) {

Expand Down Expand Up @@ -166,6 +158,7 @@ private void createBuilderMethods(PrintWriter pw, List<IField> fields) throws Ja
}

private String getFieldBaseName(String fieldName) {
IJavaProject javaProject = JavaCore.create(ResourcesPlugin.getWorkspace().getRoot().getProject());
return NamingConventions.getBaseName(NamingConventions.VK_INSTANCE_FIELD, fieldName, javaProject);
}

Expand All @@ -175,4 +168,34 @@ private void createFieldDeclarations(PrintWriter pw, List<IField> fields) throws
}
}

public static class Builder {
boolean createBuilderConstructor;
boolean createCopyConstructor;
boolean formatSource;

public Builder createBuilderConstructor(boolean createBuilderConstructorParam) {
this.createBuilderConstructor = createBuilderConstructorParam;
return this;
}

public Builder createCopyConstructor(boolean createCopyConstructorParam) {
this.createCopyConstructor = createCopyConstructorParam;
return this;
}

public Builder formatSource(boolean formatSourceParam) {
this.formatSource = formatSourceParam;
return this;
}

public BuilderGenerator build() {
return new BuilderGenerator(this);
}
}

BuilderGenerator(Builder builder) {
this.createBuilderConstructor = builder.createBuilderConstructor;
this.createCopyConstructor = builder.createCopyConstructor;
this.formatSource = builder.formatSource;
}
}
Expand Up @@ -85,7 +85,11 @@ public void handleEvent(Event event) {
}
}

Generator generator = new BuilderGenerator(createBuilderConstructor.getSelection(), createCopyConstructorButton.getSelection(), formatSourceButton.getSelection());
Generator generator = new BuilderGenerator.Builder() //
.createBuilderConstructor(createBuilderConstructor.getSelection()) //
.createCopyConstructor(createCopyConstructorButton.getSelection()) //
.formatSource(formatSourceButton.getSelection()) //
.build();
generator.generate(compilationUnit, selectedFields);
shell.dispose();
} else {
Expand Down

0 comments on commit 421d77b

Please sign in to comment.