FileDocCategorySizeDatePackage
Person.javaAPI DocExample4602Wed Sep 14 21:59:48 BST 2005com.oreilly.jent.people

Person.java

package com.oreilly.jent.people;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;

/**
 * A JavaBean containing basic information about a person.
 */
public class Person implements Serializable {

    // Data members
    private String mFirstName;
    private String mLastName;
    private Collection mEmailAddresses = new ArrayList();
    private String mID = null;
    
    public Person() {
        mID = null;
        mFirstName = null;
        mLastName = null;
    }

    /** Create a person with just name information */
    public Person(String fname, String lname) {
        setFirstName(fname);
        setLastName(lname);
        mID = getFirstName() + " " + getLastName();
    }
    
    /** Create a person with name and a unique identifier. */
    public Person(String fname, String lname, String id) {
        mID = id;
        setFirstName(fname);
        setLastName(lname);
    }
    
    /** Constructor with all data defined */
    public Person(String fname, String lname, String[] eAddrs) {
        setFirstName(fname);
        setLastName(lname);
        setEmailAddresses(eAddrs);
    }

    //------
    // Accessors for the data associated with a person
    //------
    
    public String getFirstName() {
        return mFirstName;
    }

    public void setFirstName(String fName) {
        mFirstName = fName;
    }

    public String getLastName() {
        return mLastName;
    }

    public void setLastName(String lName) {
        mLastName = lName;
    }

    public String[] getEmailAddresses() {
        String[] addrs = new String[mEmailAddresses.size()];
        Object[] origAddrs = mEmailAddresses.toArray();
        for (int i = 0; i < addrs.length; i++) {
            addrs[i] = (String)origAddrs[i];
        }
        return addrs;
    }

    public void setEmailAddresses(String[] addresses) {
        mEmailAddresses = new ArrayList();
        for (int i = 0; i < addresses.length; i++) {
            mEmailAddresses.add(addresses[i]);
        }
    }
    
    public void addEmailAddress(String address) {
        // Make sure the address isn't there already
        if (!mEmailAddresses.contains(address)) {
            mEmailAddresses.add(address);
        }
    }
    /**
     * @return Returns the id for this person.
     */
    public String getId() {
        return mID;
    }
    
    public boolean equals(Object other) {
        boolean eq = false;
        if (other instanceof Person) {
            Person p2 = (Person)other;
            // If both references point to the same object, then 
            // shortcut the equality check below
            if (p2 == this) {
                return true;
            }
            // The id and name attributes should either be both null, or
            // both non-null and equal to each other
            eq = ((p2.getId() == null && this.getId() == null) ||
                  (p2.getId() != null && this.getId() != null &&
                   p2.getId().equals(this.getId())));
            eq = eq &&
                 ((p2.getFirstName() == null && this.getFirstName() == null) ||
                  (p2.getFirstName() != null && this.getFirstName() != null &&
                   p2.getFirstName().equals(this.getFirstName())));
            eq = eq && 
                 ((p2.getLastName() == null && this.getFirstName() == null) ||
                  (p2.getLastName() != null && this.getLastName() != null &&
                   p2.getLastName().equals(this.getLastName())));
            // Both should have the same number of email addresses
            eq = eq && 
                 (p2.getEmailAddresses().length ==
                  this.getEmailAddresses().length);
            if (eq) {
                // For each of our email addresses, confirm that the 
                // other has the address on its list
                // We do this by putting one set of addresses in a collection
                // and iterating through the other using contains() to check
                // for its presence on the other list. 
                // This isn't the most efficient approach, but it's easy to
                // implement.
                ArrayList tmpList = new ArrayList();
                for (int i = 0; i < p2.getEmailAddresses().length; i++) {
                    tmpList.add(p2.getEmailAddresses()[i]);
                }
                for (int i = 0; i < this.getEmailAddresses().length; i++) {
                    eq = eq && tmpList.contains(this.getEmailAddresses()[i]);
                }
            }
        }
        return eq;
    }
}