What about me?? I am just a young 2 years experienced Eclipse/Java developer working for a semiconductor company named STMicroelectronics. My main developments inside my favorite IDE are related to visualization tools and thus I am used to step deeply inside SWT and JFace code ;o). I am located in France in the wonderful city of Grenoble. Manuel has posted 6 posts at DZone. View Full User Profile

Effective Java 2nd Edition – Builder Pattern in Eclipse

04.10.2010
| 8502 views |
  • submit to reddit

I sometimes use the Builder pattern as defined in Effective Java 2nd Edition item 2. Incidentally, I recommend this book to everybody writing Java code. Because I am a lazy man, I love JDT’s Java templates and will be really happy to have such a template for the builder pattern.

Let’s take the following example of Builder in order to illustrate this post (I just pasted 3 of the many parameters of the circle renderer class for readability):

public class XYPointCircleRenderer extends AbstractPointRenderer {

/**
* Builder class for {@link XYPointCircleRenderer}.
*/
public static class Builder {

private boolean bordered = false;

private final int radius;

private boolean tooltipable = true;

/**
* Construct a new Builder with the given radius.
*
* @param radius
* the radius of the circle. Must be > 0.
* @throws IllegalArgumentException
* if radius <= 0
*/
public Builder(int radius) {
if (radius <= 0) {
throw new IllegalArgumentException(
"radius must be greater than 0");
}
this.radius = radius;
}

/**
* Set the bordered value of this builder.
*
* @param val
* the bordered value
* @return this for convenience
*/
public Builder bordered(boolean val) {
bordered = val;
return this;
}

/**
* Return a new {@link XYPointCircleRenderer}.
*
* @return a new {@link XYPointCircleRenderer}
*/
public XYPointCircleRenderer build() {
return new XYPointCircleRenderer(this);
}

/**
* Sets the tooltipable value of this builder.
*
* @param val
* the tooltipable value
* @return this for convenience
*/
public Builder tooltipable(boolean val) {
tooltipable = val;
return this;
}
}

/**
* The circle's diameter.
*/
private final int diam;

/**
* The circle's radius.
*/
private final int radius;

/**
* Private constructor use by Builder.
*
* @param builder
* the builder to use to set this attributes.
*/
private XYPointCircleRenderer(Builder builder) {
super(builder.bordered, builder.tooltipable);
radius = builder.radius;
diam = 2 * radius;
}

@Override
public ChartElement paintPoint(int x, int y, GC gc) {
gc.fillOval(x-radius, y-radius, diameter, diam);
if (bordered) {
gc.drawOval(x-radius, y-radius, diam, diam);
}
if (tooltipable) {
rect = new Rectangle(x-radius,y-radius,diam,diam);
return new RectChartEleme(null, rect);
} else {
return null;
}
}
}

I would like to create a builder template “asking for questions to the user”. I.e the builder template should ask the end user what are the required fields of the enclosing class, and what are the default values of non required fields. Is it possible to do that ? If yes, where can I get information to start ?

Note: I often use eclipse “equals and hashcode” generator, and I guess it’s implemented by Java code. Thus the only solution to my problem is may be to implement my Builder generator the same way ….

From http://manuelselva.wordpress.com/2010/04/09/effective-java-2nd-edition-builder-pattern-in-eclipse/

Published at DZone with permission of its author, Manuel Selva.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags:

Comments

Vladimír Oraný replied on Sat, 2010/04/10 - 4:11am

I've challaged the same problem. I've created this regex quick fix to made development of builder based classes easier. I would also prefer some fully automated solution, but I hope this can help untill somebody creates it.
First create base class with final fields with default values.

public class ClassWithBuilder {

	// 1st group
	private final int theNumber = 10;
	private final String theString = "default string";
	
}

than create public static class Builder within the class. Also create private constructor of ClassWithBuilder which takes the Builder as a parameter and build method whithin Builder class. Copy fields declaration twice into Builder class body and once in constructor and build method.

	public static class Builder {
		
		// 2nd group
		private final int theNumber = 10;
		private final String theString = "default string";

		// 3rd group		
		private final int theNumber = 10;
		private final String theString = "default string";

		public ClassWithBuilder build(){
			// 4th group
			private final int theNumber = 10;
			private final String theString = "default string";
			return new ClassWithBuilder(this);
		}
	}
	
	private ClassWithBuilder(Builder b) {
		// 5th group
		private final int theNumber = 10;
		private final String theString = "default string";
	}

And now comes the regex magic.
Select the first group of fields open the find/replace dialog and ensure that you've selected Scope - Selected lines and Options - Regular experssions.
Place

private\s+(final\s)?([\w[<>]]+)\s+(\w+)\s*=?\s*(.*);(.*)

search string in Find text box. And then 

private final $2 $3;

as Replace with and run Replace all command.
Keeping search string the same apply following replacement for the other groups.
2nd group - private $2 $3 = $4;
3rd group - public Builder $3($2 val){ $3 = val; return this; }
4th group - if ($3 == null) { $3 = $4; }
5th group - this.$3 = b.$3;
The 4th group checks objects for null, so you must delete lines for primitives and objects where null is allowed value. And voilà the class with builder is done:

public class ClassWithBuilder {

	private final int theNumber;
	private final String theString;
	
	public static class Builder {
		
		private int theNumber = 10;
		private String theString = "default string";
		
		public Builder theNumber(int val){ theNumber = val; return this; }
		public Builder theString(String val){ theString = val; return this; }

		public ClassWithBuilder build(){
			return new ClassWithBuilder(this);
		}
		
	}
	
	private ClassWithBuilder(Builder b) {
		this.theNumber = b.theNumber;
		this.theString = b.theString;
	}
	
}


You can now add any logic you need or just let Eclipse generate getters.

Manuel Selva replied on Mon, 2010/04/12 - 4:08am in response to: Vladimír Oraný

Thanks for this tip. I'll try it, nevertheless as you said: "I would also prefer some fully automated solution"

 Manu

 

 

Marcin Zajączkowski replied on Sat, 2010/11/20 - 2:02pm

There's a small library make-it-easy which helps create Data Builders mostly for usage in tests.

 

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.