Bean Utils: deal with collections : Bean Utils « Apache Common « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Collections Data Structure
8. Database SQL JDBC
9. Design Pattern
10. Development Class
11. Email
12. Event
13. File Input Output
14. Game
15. Hibernate
16. J2EE
17. J2ME
18. JDK 6
19. JSP
20. JSTL
21. Language Basics
22. Network Protocol
23. PDF RTF
24. Regular Expressions
25. Security
26. Servlets
27. Spring
28. Swing Components
29. Swing JFC
30. SWT JFace Eclipse
31. Threads
32. Tiny Application
33. Velocity
34. Web Services SOA
35. XML
Microsoft Office Word 2007 Tutorial
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Java » Apache Common » Bean UtilsScreenshots 
Bean Utils: deal with collections



import org.apache.commons.beanutils.BeanUtils;

import java.util.Map;
import java.util.Date;
import java.util.List;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.GregorianCalendar;

public class BeanUtilsExampleV3 {
  public static void main(String args[]) throws Exception {
    BeanUtilsExampleV3 diff = new BeanUtilsExampleV3();
    Actor actor = diff.prepareData();

    Map describedData = BeanUtils.describe(actor);

    // check the map
    System.err.println(describedData.get("name"));

    // change this value
    describedData.put("name""Robert Redford");

    // create a new Actor Bean
    Actor newActor = new Actor();
    BeanUtils.populate(newActor, describedData);

    System.err.println(BeanUtils.getProperty(newActor, "name"));

  }

  private Actor prepareData() {
    Actor actor = new Actor();
    actor.setName("Michael Caine");
    actor.setGender(1);
    actor.setWorth(10000000);
    return actor;
  }
}

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



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 > || gender < 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(!(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(751)
               .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;
}

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



import java.util.List;

public class Actor extends Person {
  public Actor() {
  }

  public List getMovieCredits() { return this.movieCredits; }
  public void setMovieCredits(List movieCredits) {
    this.movieCredits = movieCredits;
  }

  public long getWorth() { return this.worth; }
  public void setWorth(long worth) { this.worth = worth; }

  private List movieCredits;
  private long worth;
}
           
       
BeanUtilsExampleV3.zip( 879 k)
Related examples in the same category
1. Get Mapped Property
2. BeanUtil Array Utils Example
3. BeanUtils Collections 2
4. BeanUtils Date Utils
5. BeanUtils get Property from Dynamic Beans
6. Use Bean Utils to get property value from object
ww_w.ja___v_a__2s___.__c___o_m | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.