最愛Java

          書山有路勤為徑,學(xué)海無涯苦作舟

          org.apache.commons.lang.builder學(xué)習(xí)筆記

              在org.apache.commons.lang.builder包中一共有7個類,用于幫助實(shí)現(xiàn)Java對象的一些基礎(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控制輸出格式。
              
              我們來看下面的例子來學(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("鄭致力"32new Date());
                  Person person2 
          = new Person("高婕"27new 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用起來很簡單,只要new一個實(shí)例,append需要參與的信息,然后加上toComparison、isEquals、toHashCode、toString結(jié)尾就可以了。如果不需要使用append指定信息,則可直接使用反射機(jī)制進(jìn)行自動化實(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),對于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種類不能滿足你的要求的話,想繼承他們是不可能的。所以你需要創(chuàng)建StandardToStringStyle一個實(shí)例,然后調(diào)用它的方法來實(shí)現(xiàn)自定義的格式。在StandardToStringStyle的單元測試代碼中,是這樣調(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(
          "%");
              }

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

          公告


          導(dǎo)航

          <2009年1月>
          28293031123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          統(tǒng)計(jì)

          常用鏈接

          留言簿(4)

          隨筆分類

          隨筆檔案

          收藏夾

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 当涂县| 阿坝县| 永胜县| 碌曲县| 泗水县| 阿城市| 湖口县| 宁国市| 呈贡县| 永寿县| 奉节县| 滦南县| 成武县| 遂川县| 山东| 荔波县| 奉节县| 云龙县| 云霄县| 车险| 虎林市| 泸西县| 上虞市| 盐津县| 邯郸县| 苏尼特左旗| 开化县| 柳州市| 南郑县| 汽车| 稷山县| 东山县| 龙泉市| 阿拉善右旗| 忻城县| 丹江口市| 板桥市| 航空| 嵩明县| 清新县| 平舆县|