Created
October 12, 2011 16:21
-
-
Save edalquist/1281709 to your computer and use it in GitHub Desktop.
Bean Builder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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