Skip to content

Instantly share code, notes, and snippets.

@edalquist
Created October 12, 2011 16:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edalquist/1281709 to your computer and use it in GitHub Desktop.
Save edalquist/1281709 to your computer and use it in GitHub Desktop.
Bean Builder
public class MyBean {
private Object foo;
private Object bar;
public MyBean() {
}
private MyBean(MyBean myBean) {
this.setFoo(myBean.getBar());
this.setBar(myBean.getBar());
}
public Object getFoo() {
return foo;
}
public void setFoo(Object foo) {
this.foo = foo;
}
public Object getBar() {
return bar;
}
public void setBar(Object bar) {
this.bar = bar;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((bar == null) ? 0 : bar.hashCode());
result = prime * result + ((foo == null) ? 0 : foo.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
MyBean other = (MyBean) obj;
if (bar == null) {
if (other.bar != null)
return false;
}
else if (!bar.equals(other.bar))
return false;
if (foo == null) {
if (other.foo != null)
return false;
}
else if (!foo.equals(other.foo))
return false;
return true;
}
@Override
public String toString() {
return "MyBean [foo=" + foo + ", bar=" + bar + "]";
}
public static class Builder {
private final MyBean myBean;
public Builder() {
myBean = new MyBean();
}
public Builder setFoo(Object foo) {
this.myBean.setFoo(foo);
return this;
}
public Builder setBar(Object bar) {
this.myBean.setBar(bar);
return this;
}
public MyBean build() {
return new MyBean(this.myBean);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment