org.apache.commons.lang.builder學(xué)習(xí)筆記
在org.apache.commons.lang.builder包中一共有7個(gè)類,用于幫助實(shí)現(xiàn)Java對(duì)象的一些基礎(chǔ)的方法,如compareTo(), equals(), hashCode(), toString()等。他們分別是:
CompareToBuilder – 用于輔助實(shí)現(xiàn)Comparable.compareTo(Object)方法;
EqualsBuilder – 用于輔助實(shí)現(xiàn)Object.equals()方法;
HashCodeBuilder – 用于輔助實(shí)現(xiàn)Object.hashCode()方法;
ReflectionToStringBuilder – 使用反射機(jī)制輔助實(shí)現(xiàn)Object.toString()方法;
ToStringBuilder – 用于輔助實(shí)現(xiàn)Object.toString()方法;
StandardToStringStyle – 輔助ToStringBuilder控制標(biāo)準(zhǔn)格式;
ToStringStyle – 輔助ToStringBuilder控制輸出格式。
我們來(lái)看下面的例子來(lái)學(xué)習(xí)這些類的使用:
package org.apache.commons.lang.builder;

import java.util.Date;

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.ReflectionToStringBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.apache.commons.lang.builder.StandardToStringStyle;

public class BuilderTest {

public static void main(String[] args) {
Person person1 = new Person("鄭致力", 32, new Date());
Person person2 = new Person("高婕", 27, new Date());

System.out.println("person1's info: " + person1);
System.out.println("person2's info: " + person2);
System.out.println("person1's hash code: " + person1.hashCode());
System.out.println("person2's hash code: " + person2.hashCode());
System.out.println("person1 equals person2? " + person1.equals(person2));
System.out.println("--------------More String Style of Object ------------------------------------");
System.out.println("person1's info: " + person1.toString2(ToStringStyle.MULTI_LINE_STYLE));
System.out.println("person1's info: " + person1.toString2(ToStringStyle.NO_FIELD_NAMES_STYLE));
System.out.println("person1's info: " + person1.toString2(ToStringStyle.SHORT_PREFIX_STYLE));
System.out.println("person1's info: " + person1.toString2(ToStringStyle.SIMPLE_STYLE));
System.out.println("person1's info: " + person1.toString2(new StandardToStringStyle()));
}
}

class Person implements Comparable {
private String name;
private int age;
private Date dateJoined;

public Person() {
};

public Person(String name, int age, Date dateJoined) {
this.name = name;
this.age = age;
this.dateJoined = dateJoined;
}

public int compareTo(Object o) {
if (!(o instanceof Person)) {
return -1;
}
Person other = (Person) o;
return new CompareToBuilder().append(name, other.getName()).append(age,
other.getAge()).append(dateJoined, other.getDateJoined())
.toComparison();
}

public boolean equals(Object o) {
if (!(o instanceof Person)) {
return false;
}
Person other = (Person) o;
return new EqualsBuilder().append(name, other.getName()).append(age,
other.getAge()).append(dateJoined, other.getDateJoined())
.isEquals();
}

public int hashCode() {
return new HashCodeBuilder().append(name).append(age)
.append(dateJoined).toHashCode();
}

public String toString() {
return new ToStringBuilder(this).append("name", name)
.append("age", age).append("dateJoined", dateJoined).toString();
}
public String toString2(ToStringStyle style) {
ToStringStyle _style = ToStringStyle.DEFAULT_STYLE;
if (null != style) {
_style = style;
}
return new ToStringBuilder(this, _style).append("name", name)
.append("age", age).append("dateJoined", dateJoined).toString();
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public Date getDateJoined() {
return dateJoined;
}

public void setDateJoined(Date dateJoined) {
this.dateJoined = dateJoined;
}

}
這些builder用起來(lái)很簡(jiǎn)單,只要new一個(gè)實(shí)例,append需要參與的信息,然后加上toComparison、isEquals、toHashCode、toString結(jié)尾就可以了。如果不需要使用append指定信息,則可直接使用反射機(jī)制進(jìn)行自動(dòng)化實(shí)現(xiàn),如下面的Student類:
class Student extends Person {
private int grad;
public Student() {super();}
public Student(String name, int age, Date dateJoined, int grad) {
super(name, age, dateJoined);
this.grad = grad;
}
public int compareTo(Object o) {
return CompareToBuilder.reflectionCompare(this, o);
}
public boolean equals(Object o) {
return EqualsBuilder.reflectionEquals(this, o);
}
public int hashCode() {
return HashCodeBuilder.reflectionHashCode(this);
}
public String toString() {
return ReflectionToStringBuilder.toString(this);
}
}
這里需要補(bǔ)充一點(diǎn),對(duì)于ToStringStyle類,代碼中已經(jīng)內(nèi)置了5種,分別為ToStringStyle.DEFAULT_STYLE、ToStringStyle.MULTI_LINE_STYLE、ToStringStyle.NO_FIELD_NAMES_STYLE、ToStringStyle.SHORT_PREFIX_STYLE、ToStringStyle.SIMPLE_STYLE。不知道為什么,這5種內(nèi)置類的實(shí)現(xiàn)都被定義成了private static final類了。所以如果上述5種類不能滿足你的要求的話,想繼承他們是不可能的。所以你需要?jiǎng)?chuàng)建StandardToStringStyle一個(gè)實(shí)例,然后調(diào)用它的方法來(lái)實(shí)現(xiàn)自定義的格式。在StandardToStringStyle的單元測(cè)試代碼中,是這樣調(diào)用的:
private static final StandardToStringStyle STYLE = new StandardToStringStyle();
static {
STYLE.setUseShortClassName(true);
STYLE.setUseIdentityHashCode(false);
STYLE.setArrayStart("[");
STYLE.setArraySeparator(", ");
STYLE.setArrayEnd("]");
STYLE.setNullText("%NULL%");
STYLE.setSizeStartText("%SIZE=");
STYLE.setSizeEndText("%");
STYLE.setSummaryObjectStartText("%");
STYLE.setSummaryObjectEndText("%");
}
CompareToBuilder – 用于輔助實(shí)現(xiàn)Comparable.compareTo(Object)方法;
EqualsBuilder – 用于輔助實(shí)現(xiàn)Object.equals()方法;
HashCodeBuilder – 用于輔助實(shí)現(xiàn)Object.hashCode()方法;
ReflectionToStringBuilder – 使用反射機(jī)制輔助實(shí)現(xiàn)Object.toString()方法;
ToStringBuilder – 用于輔助實(shí)現(xiàn)Object.toString()方法;
StandardToStringStyle – 輔助ToStringBuilder控制標(biāo)準(zhǔn)格式;
ToStringStyle – 輔助ToStringBuilder控制輸出格式。
我們來(lái)看下面的例子來(lái)學(xué)習(xí)這些類的使用:















































































































這些builder用起來(lái)很簡(jiǎn)單,只要new一個(gè)實(shí)例,append需要參與的信息,然后加上toComparison、isEquals、toHashCode、toString結(jié)尾就可以了。如果不需要使用append指定信息,則可直接使用反射機(jī)制進(jìn)行自動(dòng)化實(shí)現(xiàn),如下面的Student類:


























這里需要補(bǔ)充一點(diǎn),對(duì)于ToStringStyle類,代碼中已經(jīng)內(nèi)置了5種,分別為ToStringStyle.DEFAULT_STYLE、ToStringStyle.MULTI_LINE_STYLE、ToStringStyle.NO_FIELD_NAMES_STYLE、ToStringStyle.SHORT_PREFIX_STYLE、ToStringStyle.SIMPLE_STYLE。不知道為什么,這5種內(nèi)置類的實(shí)現(xiàn)都被定義成了private static final類了。所以如果上述5種類不能滿足你的要求的話,想繼承他們是不可能的。所以你需要?jiǎng)?chuàng)建StandardToStringStyle一個(gè)實(shí)例,然后調(diào)用它的方法來(lái)實(shí)現(xiàn)自定義的格式。在StandardToStringStyle的單元測(cè)試代碼中,是這樣調(diào)用的:














posted on 2009-01-04 17:34 Brian 閱讀(1782) 評(píng)論(0) 編輯 收藏 所屬分類: Jakarta Commons筆記