乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      [Jakarta Commons筆記](méi) org.apache.commons.lang.builder

       moonboat 2008-11-12

      在前面的專(zhuān)題文章中,我們一起過(guò)了一遍org.apache.commons.lang包,接下來(lái)我們繼續(xù)看org.apache.commons.lang.builder這個(gè)包。在這里面我們可以找到7個(gè)類(lèi),用于幫助我們實(shí)現(xiàn)Java對(duì)象的一些基礎(chǔ)的共有方法。這7個(gè)類(lèi)分別是:

       

      CompareToBuilder – 用于輔助實(shí)現(xiàn)Comparable.compareTo(Object)方法;

      EqualsBuilder – 用于輔助實(shí)現(xiàn)Object.equals()方法;

      HashCodeBuilder – 用于輔助實(shí)現(xiàn)Object.hashCode()方法;

      ToStringBuilder – 用于輔助實(shí)現(xiàn)Object.toString()方法;

      ReflectionToStringBuilder – 使用反射機(jī)制輔助實(shí)現(xiàn)Object.toString()方法;

      ToStringStyle – 輔助ToStringBuilder控制輸出格式;

      StandardToStringStyle – 輔助ToStringBuilder控制標(biāo)準(zhǔn)格式。

       

      我們知道,在實(shí)際應(yīng)用中,其實(shí)經(jīng)常需要在運(yùn)行過(guò)程中判定對(duì)象的知否相等、比較、取hash、和獲取對(duì)象基本信息(一般是產(chǎn)生log日志)。然而實(shí)現(xiàn)這些compareTo、equals、hashCodetoString其實(shí)并非那么直截了當(dāng),甚至稍有不注意就可能造成難以追蹤的bug,而且這些方法手工維護(hù)的話,比較繁瑣,也容易出錯(cuò)。于是Commons Langbuilder這個(gè)包中提供了上述輔助類(lèi),為我們簡(jiǎn)化這些方法的實(shí)現(xiàn)和維護(hù)。

       

      來(lái)看一個(gè)例子:

       

      package sean.study.jakarta.commons.lang;

       

      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.ToStringBuilder;

      import org.apache.commons.lang.builder.ToStringStyle;

       

      public class BuilderUsage {

       

          public static void main(String[] args) {

             

              Staff staff1 = new Staff(123, "John Smith", new Date());

              Staff staff2 = new Staff(456, "Jane Smith", new Date());

             

              System.out.println("staff1's info: " + staff1);

              System.out.println("staff2's info: " + staff2);

              System.out.println("staff1's hash code: " + staff1.hashCode());

              System.out.println("staff2's hash code: " + staff2.hashCode());

              System.out.println("staff1 equals staff2? " + staff1.equals(staff2));

       

          }

       

      }

       

      class Staff implements Comparable {

       

          private long staffId;

          private String staffName;

          private Date dateJoined;

       

          public Staff() {

          }

       

          public Staff(long staffId, String staffName, Date dateJoined) {

              this.staffId = staffId;

              this.staffName = staffName;

              this.dateJoined = dateJoined;

          }

       

          public int compareTo(Object o) {

              int res = -1;

              if (o != null && Staff.class.isAssignableFrom(o.getClass())) {

                  Staff s = (Staff) o;

                  res = new CompareToBuilder()

                          .append(dateJoined, s.getDateJoined())

                          .append(staffName, s.getStaffName()).toComparison();

              }

              return res;

          }

       

          public boolean equals(Object o) {

              boolean res = false;

              if (o != null && Staff.class.isAssignableFrom(o.getClass())) {

                  Staff s = (Staff) o;

                  res = new EqualsBuilder()

                          .append(staffId, s.getStaffId())

                          .isEquals();

              }

              return res;

          }

       

          public int hashCode() {

              return new HashCodeBuilder(11, 23).append(staffId).toHashCode();

          }

       

          public String toString() {

              return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)

                      .append("staffId", staffId)

                      .append("staffName", staffName)

                      .append("dateJoined", dateJoined)

                      .toString();

          }

       

          public Date getDateJoined() {

              return dateJoined;

          }

       

          public void setDateJoined(Date dateJoined) {

              this.dateJoined = dateJoined;

          }

       

          public long getStaffId() {

              return staffId;

          }

       

          public void setStaffId(long staffId) {

              this.staffId = staffId;

          }

       

          public String getStaffName() {

              return staffName;

          }

       

          public void setStaffName(String staffName) {

              this.staffName = staffName;

          }

      }

       

      以下是運(yùn)行結(jié)果:

       

      staff1's info: sean.study.jakarta.commons.lang.Staff@190d11[

        staffId=123

        staffName=John Smith

        dateJoined=Sat Jul 30 13:18:45 CST 2005

      ]

      staff2's info: sean.study.jakarta.commons.lang.Staff@1fb8ee3[

        staffId=456

        staffName=Jane Smith

        dateJoined=Sat Jul 30 13:18:45 CST 2005

      ]

      staff1's hash code: 376

      staff2's hash code: 709

      staff1 equals staff2? false

       

      這些builder使用起來(lái)都很簡(jiǎn)單,new一個(gè)實(shí)例,append需要參與的信息,最后加上toComparison、isEquals、toHashCode、toString這樣的結(jié)尾即可。相應(yīng)的,如果你不需要這樣級(jí)別的控制,也可以使用利用反射機(jī)制的版本自動(dòng)化實(shí)現(xiàn)需要的功能,如:

       

          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);

          }

       

      尤其當(dāng)我們?cè)陧?xiàng)目中不希望過(guò)多的參與到對(duì)這些對(duì)象方法的維護(hù)時(shí),采用Commons提供的利用反射的這些API就成了方便而相對(duì)安全的一個(gè)方案。

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶(hù)發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶(hù) 評(píng)論公約

        類(lèi)似文章 更多