Java by API/org.apache.commons.lang.builder/ToStringBuilder

Материал из Java эксперт
Версия от 14:23, 31 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

new ToStringBuilder(Object object, ToStringStyle style)

/*
 One>>>BuilderTrial@1d1acd3[
  Name=Becker
  Age=35
]
Two>>>BuilderTrial@743399[
  Name=Becker
  Age=35
]
Three>>>BuilderTrial@e7b241[
  Name=Agassi
  Age=33
]
one equals two? true
one equals three? false
One HashCode>>> -923455822
Two HashCode>>> -923455822
Three HashCode>>> -1433293806
 */
import org.apache.rumons.lang.builder.EqualsBuilder;
import org.apache.rumons.lang.builder.HashCodeBuilder;
import org.apache.rumons.lang.builder.ToStringBuilder;
import org.apache.rumons.lang.builder.ToStringStyle;
public class BuilderTrial {
  private String name;
  private int age;
  public BuilderTrial(String name, int age) {
    this.name = name;
    this.age = age;
  }
  public static void main(String[] args) {
    // Create new BuilderTrial instances
    BuilderTrial one = new BuilderTrial("Becker", 35);
    BuilderTrial two = new BuilderTrial("Becker", 35);
    BuilderTrial three = new BuilderTrial("Agassi", 33);
    // one and two hold the same data in different objects
    // three holds different data
    System.out.println("One>>>" + one);
    System.out.println("Two>>>" + two);
    System.out.println("Three>>>" + three);
    System.out.println("one equals two? " + one.equals(two));
    System.out.println("one equals three? " + one.equals(three));
    // As one and two hold the same data, the same hashcode is returned.
    System.out.println("One HashCode>>> " + one.hashCode());
    System.out.println("Two HashCode>>> " + two.hashCode());
    System.out.println("Three HashCode>>> " + three.hashCode());
  }
  public boolean equals(Object objCompared) {
    if (!(objCompared instanceof BuilderTrial)) {
      return false;
    }
    BuilderTrial rhs = (BuilderTrial) objCompared;
    return new EqualsBuilder().append(name, rhs.name).append(age, rhs.age).isEquals();
  }
  public String toString() {
    return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE).append("Name", name).append(
        "Age", age).toString();
  }
  public int hashCode() {
    return new HashCodeBuilder(15, 19).append(name).append(age).toHashCode();
  }
}





ToStringBuilder: reflectionToString(Object object, ToStringStyle style)

/*
One>>>ReflectionBuilderTrial@1a1c887[
  name=Becker
  age=35
]
Two>>>ReflectionBuilderTrial@a32b[
  name=Becker
  age=35
]
Three>>>ReflectionBuilderTrial@1d8957f[
  name=Agassi
  age=33
]
one equals two? true
one equals three? false
One HashCode>>> 462213092
Two HashCode>>> 462213092
Three HashCode>>> -530629296
*/
import org.apache.rumons.lang.builder.EqualsBuilder;
import org.apache.rumons.lang.builder.HashCodeBuilder;
import org.apache.rumons.lang.builder.ToStringBuilder;
import org.apache.rumons.lang.builder.ToStringStyle;
public class ReflectionBuilderTrial {
    private String name = null;
    private int age = 0;
    public ReflectionBuilderTrial(String name, int age) {
        this.name = name;
        this.age = age;
    }
    public static void main(String[] args) {
        ReflectionBuilderTrial one = new ReflectionBuilderTrial("Becker", 35);
        ReflectionBuilderTrial two = new ReflectionBuilderTrial("Becker", 35);
        ReflectionBuilderTrial three = new ReflectionBuilderTrial("Agassi", 33);
        System.out.println("One>>>" + one);
        System.out.println("Two>>>" + two);
        System.out.println("Three>>>" + three);
        System.out.println("one equals two? " + one.equals(two));
        System.out.println("one equals three? " + one.equals(three));
        System.out.println("One HashCode>>> " + one.hashCode());
        System.out.println("Two HashCode>>> " + two.hashCode());
        System.out.println("Three HashCode>>> " + three.hashCode());
    }
    public boolean equals(Object obj) {
        return EqualsBuilder.reflectionEquals(this, obj);
    }
    public String toString() {
        return ToStringBuilder.reflectionToString(this,
            ToStringStyle.MULTI_LINE_STYLE);
    }
    public int hashCode() {
        return HashCodeBuilder.reflectionHashCode(this);
    }
}





ToStringStyle.MULTI_LINE_STYLE

/*
One>>>ReflectionBuilderTrial@1a1c887[
  name=Becker
  age=35
]
Two>>>ReflectionBuilderTrial@a32b[
  name=Becker
  age=35
]
Three>>>ReflectionBuilderTrial@1d8957f[
  name=Agassi
  age=33
]
one equals two? true
one equals three? false
One HashCode>>> 462213092
Two HashCode>>> 462213092
Three HashCode>>> -530629296
*/
import org.apache.rumons.lang.builder.EqualsBuilder;
import org.apache.rumons.lang.builder.HashCodeBuilder;
import org.apache.rumons.lang.builder.ToStringBuilder;
import org.apache.rumons.lang.builder.ToStringStyle;
public class ReflectionBuilderTrial {
    private String name = null;
    private int age = 0;
    public ReflectionBuilderTrial(String name, int age) {
        this.name = name;
        this.age = age;
    }
    public static void main(String[] args) {
        ReflectionBuilderTrial one = new ReflectionBuilderTrial("Becker", 35);
        ReflectionBuilderTrial two = new ReflectionBuilderTrial("Becker", 35);
        ReflectionBuilderTrial three = new ReflectionBuilderTrial("Agassi", 33);
        System.out.println("One>>>" + one);
        System.out.println("Two>>>" + two);
        System.out.println("Three>>>" + three);
        System.out.println("one equals two? " + one.equals(two));
        System.out.println("one equals three? " + one.equals(three));
        System.out.println("One HashCode>>> " + one.hashCode());
        System.out.println("Two HashCode>>> " + two.hashCode());
        System.out.println("Three HashCode>>> " + three.hashCode());
    }
    public boolean equals(Object obj) {
        return EqualsBuilder.reflectionEquals(this, obj);
    }
    public String toString() {
        return ToStringBuilder.reflectionToString(this,
            ToStringStyle.MULTI_LINE_STYLE);
    }
    public int hashCode() {
        return HashCodeBuilder.reflectionHashCode(this);
    }
}