BeanUtils Collections 2 : Bean Utils « Apache Common « Java






BeanUtils Collections 2

import org.apache.commons.beanutils.BeanPredicate;
import org.apache.commons.collections.PredicateUtils;

public class BeanUtilsCollectionsV2 {
  public static void main(String args[]) {
    BeanPredicate predicate =
      new BeanPredicate("title", PredicateUtils.uniquePredicate());

    Movie movie = new Movie();
    movie.setTitle("The Italian Job");

    Movie movie1 = new Movie();
    movie1.setTitle("The Italian Job");

    System.err.println(predicate.evaluate(movie)); // evaluates true
    System.err.println(predicate.evaluate(movie1)); // evaluates false

  }
}

--------------------------------------------------------


import java.util.Map;
import java.util.List;
import java.util.Date;

public class Movie {
  public Movie() {
  }

  public Date getDateOfRelease() { return this.dateOfRelease; }
  public void setDateOfRelease(Date dateOfRelease) {
    this.dateOfRelease = dateOfRelease;
  }

  public String getTitle() { return this.title; }
  public void setTitle(String title) {this.title = title; }

  public Person getDirector() { return this.director; }
  public void setDirector(Person director) { this.director = director; }

  public List getActors() { return this.actors; }
  public void setActors(List actors) { this.actors= actors; }

  public String[] getKeywords() { return this.keywords; }
  public void setKeyWords(String[] keywords) { this.keywords = keywords; }

  public Map getGenre() { return this.genre; }
  public void setGenre(Map genre) { this.genre = genre; }

  private Date dateOfRelease;
  private String title;
  private Person director;

  private List actors;
  private String[] keywords;

  private Map genre;
}

--------------------------------------------------------------------


import java.util.Map;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;

public class Person {
  public Person() {
  }

  public String getName() {
    return this.name == null ? "NoName" : this.name; }
  public void setName(String name) { this.name = name; }

  public int getGender() { return this.gender; }
  public void setGender(int gender) {  // 0 - Indeterminate, 1 - Male, 2 - Female
    this.gender = (gender > 2 || gender < 0) ? 0 : gender; }

  public Map getContactNumber() { return this.contactNumber; }
  public void setContactNumber(Map contactNumber) {
    this.contactNumber = contactNumber;
  }

  /**public boolean equals(Object o) {
    if(o == this) return true;
    if(!(o instanceof Person)) return false;
    Person otherPerson = (Person)o;
    if(otherPerson.getName().equals(this.name) &&
       otherPerson.getGender() == this.gender) return true;

    return false;
  }*/

  public boolean equals(Object o) {
    if(!(o instanceof Person)) return false;

    Person otherPerson = (Person)o;
    return new EqualsBuilder()
               .append(name, otherPerson.getName())
               .append(gender, otherPerson.getGender())
               .isEquals();
  }

  public int hashCode() {
    return new HashCodeBuilder(7, 51)
               .append(name)
               .append(gender)
               .append(contactNumber)
               .toHashCode();
  }

  public String toString() {
    return new ToStringBuilder(this)
               .append("Name", name)
               .append("Gender", gender)
               .append("Contact Details", contactNumber)
               .toString();
  }

  private String name;
  private int gender;
  private Map contactNumber;
}
           
       








BeanUtilsCollectionsV2.zip( 878 k)

Related examples in the same category

1.Get Mapped Property
2.BeanUtil Array Utils Example
3.BeanUtils Date Utils
4.BeanUtils get Property from Dynamic Beans
5.Use Bean Utils to get property value from object
6.Bean Utils: deal with collections