<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    最愛Java

    書山有路勤為徑,學海無涯苦作舟

    org.apache.commons.lang.builder學習筆記

        在org.apache.commons.lang.builder包中一共有7個類,用于幫助實現Java對象的一些基礎的方法,如compareTo(), equals(), hashCode(), toString()等。他們分別是:
        
        CompareToBuilder – 用于輔助實現Comparable.compareTo(Object)方法;
        EqualsBuilder – 用于輔助實現Object.equals()方法;
        HashCodeBuilder – 用于輔助實現Object.hashCode()方法;
        ReflectionToStringBuilder – 使用反射機制輔助實現Object.toString()方法;
        ToStringBuilder – 用于輔助實現Object.toString()方法;
        StandardToStringStyle – 輔助ToStringBuilder控制標準格式;
        ToStringStyle – 輔助ToStringBuilder控制輸出格式。
        
        我們來看下面的例子來學習這些類的使用:
    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一個實例,append需要參與的信息,然后加上toComparison、isEquals、toHashCode、toString結尾就可以了。如果不需要使用append指定信息,則可直接使用反射機制進行自動化實現,如下面的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);
        }

    }

        這里需要補充一點,對于ToStringStyle類,代碼中已經內置了5種,分別為ToStringStyle.DEFAULT_STYLE、ToStringStyle.MULTI_LINE_STYLE、ToStringStyle.NO_FIELD_NAMES_STYLE、ToStringStyle.SHORT_PREFIX_STYLE、ToStringStyle.SIMPLE_STYLE。不知道為什么,這5種內置類的實現都被定義成了private static final類了。所以如果上述5種類不能滿足你的要求的話,想繼承他們是不可能的。所以你需要創建StandardToStringStyle一個實例,然后調用它的方法來實現自定義的格式。在StandardToStringStyle的單元測試代碼中,是這樣調用的:
        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 閱讀(1778) 評論(0)  編輯  收藏 所屬分類: Jakarta Commons筆記

    公告


    導航

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

    統計

    常用鏈接

    留言簿(4)

    隨筆分類

    隨筆檔案

    收藏夾

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 国产VA免费精品高清在线| 亚洲第一综合天堂另类专| 你是我的城池营垒免费看| 亚洲伊人成无码综合网| 免费人成网站在线播放| 亚洲精华液一二三产区| 色www永久免费视频| 羞羞网站免费观看| 免费人成年轻人电影| 国产在线播放线91免费| 亚洲av中文无码乱人伦在线播放 | 另类专区另类专区亚洲| 免费在线观看中文字幕| 一本一道dvd在线观看免费视频| 亚洲欧洲一区二区三区| 亚洲国产成人99精品激情在线| 青草草色A免费观看在线| 亚洲欧美日韩中文高清www777| 日本免费一区二区三区最新| 污污免费在线观看| 亚洲国产精品无码久久久秋霞2| 99精品视频免费在线观看| 亚洲AV无码一区二区三区人| 午夜免费福利小电影| 中文字幕在亚洲第一在线| 亚洲av午夜电影在线观看 | 成a人片亚洲日本久久| 亚洲色偷偷综合亚洲AVYP| 免费很黄无遮挡的视频毛片| 久久久青草青青国产亚洲免观 | 亚洲第一福利网站在线观看| 国产亚洲精品免费视频播放| 亚洲精品mv在线观看| 午夜免费福利在线观看| 久久不见久久见免费影院www日本| 亚洲天堂一区二区| 国产精品久久免费视频| 亚洲精品天堂无码中文字幕| 亚洲精品麻豆av| 黄页网站在线看免费| 72pao国产成视频永久免费|