Java/Apache Common/Class Helper — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
 
м (1 версия)
 
(нет различий)

Текущая версия на 07:23, 1 июня 2010

Implement equals method using commons-lang

 
 
import org.apache.rumons.lang.builder.HashCodeBuilder;
import org.apache.rumons.lang.builder.EqualsBuilder;
import java.io.Serializable;
public class Main implements Serializable {
  private Long id;
  private String title;
  private String author;
  public boolean equals(Object object) {
    if (!(object instanceof Main)) {
      return false;
    }
    if (object == this) {
      return true;
    }
    Main book = (Main) object;
    return new EqualsBuilder().append(this.id, book.id).append(this.title, book.title).append(
        this.author, book.author).isEquals();
    // return EqualsBuilder.reflectionEquals(this, book);
  }
}





Implement hashCode using commons-lang

 
 
import org.apache.rumons.lang.builder.HashCodeBuilder;
import org.apache.rumons.lang.builder.EqualsBuilder;
import java.io.Serializable;
public class Main implements Serializable {
  private Long id;
  private String title;
  private String author;
  public int hashCode() {
    return new HashCodeBuilder().append(id).append(title).append(author).toHashCode();
    // return HashCodeBuilder.reflectionHashCode(this);
  }
}





Jakarta Commons toString Builder

 
 
import org.apache.rumons.lang.builder.ToStringBuilder;
import org.apache.rumons.lang.builder.ToStringStyle;
public class Main {
  private String id;
  private String firstName;
  private String lastName;
  public Main() {
  }
  public String toString() {
    return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE).append("id", id).append(
        "firstName", firstName).append("lastName", lastName).toString();
  }
  public static void main(String[] args) {
    Main example = new Main();
    example.id = "1";
    example.firstName = "First Name";
    example.lastName = "Last Name";
    System.out.println("example = " + example);
  }
}





Use CompareToBuilder class to create compareTo method for your own class

 
import org.apache.rumons.lang.builder.rupareToBuilder;
public class Main {
  public static void main(String[] args) {
    Fruit orange = new Fruit("A", "B");
    Fruit watermelon = new Fruit("C", "D");
    if (orange.rupareTo(watermelon) == 0) {
      System.out.println(orange.getName() + " == " + watermelon.getName());
    } else {
      System.out.println(orange.getName() + " != " + watermelon.getName());
    }
  }
}
class Fruit {
  private String name;
  private String colour;
  public Fruit(String name, String colour) {
    this.name = name;
    this.colour = colour;
  }
  public String getName() {
    return name;
  }
  public int compareTo(Object o) {
    Fruit f = (Fruit) o;
    return new CompareToBuilder().append(this.name, f.name).append(this.colour, f.colour)
        .toComparison();
  }
}





Use Reflection To build toString method

 
import org.apache.rumons.lang.builder.ReflectionToStringBuilder;
import org.apache.rumons.lang.builder.ToStringStyle;
public class Main {
  private Integer id;
  private String name;
  private String description;
  public static final String KEY = "APP-KEY";
  private transient String secretKey;
  public Main(Integer id, String name, String description, String secretKey) {
    this.id = id;
    this.name = name;
    this.description = description;
    this.secretKey = secretKey;
  }
  public String toString() {
    return ReflectionToStringBuilder.toString(this, ToStringStyle.SIMPLE_STYLE, true, true);
  }
  public static void main(String[] args) {
    Main demo = new Main (1, "A", "B", "C");
    System.out.println("Demo = " + demo);
  }
}