CompareToBuilder,EqualsBuilder,ToStringBuilder 很好用
|
|
|
在编码过程中,我们可能经常用到两个对象比较,将对象中的元素
转化成字符串等等。现在这些工作已经有相应的包了
import org.apache.commons.lang.builder.CompareToBuilder import org.apache.commons.lang.builder.EqualsBuilder import org.apache.commons.lang.builder.HashCodeBuilder import org.apache.commons.lang.builder.ToStringBuilder
class LavaLamp implements Comparable{ @Property model @Property baseColor @Property liquidColor @Property lavaColor
def String toString() { return new ToStringBuilder(this). append(this.model). append(this.baseColor). append(this.liquidColor). append(this.lavaColor). toString() }
def boolean equals(obj) { if (!(obj instanceof LavaLamp)) { return false } LavaLamp rhs = (LavaLamp) obj return new EqualsBuilder(). append(this.model, rhs.model). append(this.baseColor, rhs.baseColor). append(this.liquidColor, rhs.liquidColor). append(this.lavaColor,rhs.lavaColor).isEquals() }
def int compareTo(obj) { LavaLamp lmp = (LavaLamp)obj return new CompareToBuilder(). append(lmp.model, this.model).append(lmp.lavaColor, this.lavaColor).append(lmp.baseColor, this.baseColor). append(lmp.liquidColor, this.liquidColor). toComparison() } }
|
|
|