|
Space Map
|
BaseObject.java와의 첫만남.
BaseObject.java import java.io.Serializable; import org.apache.commons.lang.builder.EqualsBuilder; import org.apache.commons.lang.builder.HashCodeBuilder; import org.apache.commons.lang.builder.ToStringBuilder; import org.apache.commons.lang.builder.ToStringStyle; public class BaseObject implements Serializable { public String toString() { return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE); } public boolean equals(Object o) { return EqualsBuilder.reflectionEquals(this, o); } public int hashCode() { return HashCodeBuilder.reflectionHashCode(this); } }
BaseObject.java와의 두번째 만남
Equinox 1.7의 BaseObject.java import org.apache.commons.lang.builder.ToStringBuilder; import org.apache.commons.lang.builder.ToStringStyle; import java.io.Serializable; /** * Base class for Model objects. This is basically for the toString method. * * @author Matt Raible */ public class BaseObject implements Serializable { private static final long serialVersionUID = 3256446889040622647L; public String toString() { return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE); } } Appfuse 1.9.4의 BaseObject.java import java.io.Serializable; /** * Base class for Model objects. Child objects should implement toString(), * equals() and hashCode(); * * @author <a href="mailto:matt@raibledesigns.com">Matt Raible</a> */ public abstract class BaseObject implements Serializable { public abstract String toString(); public abstract boolean equals(Object o); public abstract int hashCode(); }
equals(), hashCode(), toString() 메써드 구현
equals() 메써드equals() 메써드를 수동으로 구현 public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof Address)) return false; final Address address1 = (Address) o; if (address != null ? !address.equals(address1.address) : address1.address != null) return false; if (city != null ? !city.equals(address1.city) : address1.city != null) return false; if (country != null ? !country.equals(address1.country) : address1.country != null) return false; if (postalCode != null ? !postalCode.equals(address1.postalCode) : address1.postalCode != null) return false; if (province != null ? !province.equals(address1.province) : address1.province != null) return false; return true; } hashCode() 메써드hashCode() 메써드를 수동으로 구현 public int hashCode() { int result; result = (address != null ? address.hashCode() : 0); result = 29 * result + (city != null ? city.hashCode() : 0); result = 29 * result + (province != null ? province.hashCode() : 0); result = 29 * result + (country != null ? country.hashCode() : 0); result = 29 * result + (postalCode != null ? postalCode.hashCode() : 0); return result; } toString() 메써드ToStringBuilder를 이용하여 toString() 메써드 구현 public String toString() { return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE) .append("country", this.country) .append("address", this.address).append("province", this.province).append("postalCode", this.postalCode) .append("city", this.city).toString(); } |
|