001package edu.pdx.cs410J.family;
002
003import java.util.*;
004import java.io.Serializable;
005
006/**
007 * This class represents a family tree.  Essentially, it is a
008 * collection of people.  Family trees are always rooted at the person
009 * with id 1.
010 *
011 * @author David Whitlock
012 */
013public class FamilyTree implements Serializable {
014  
015  private Map<Integer, Person> people;     // Maps Integer ids to Persons
016
017  /**
018   * Creates an empty family tree.
019   */
020  public FamilyTree() {
021    this.people = new HashMap<Integer, Person>();
022  }
023
024  /**
025   * Returns a collection of <code>Person</code>s that are in this family
026   * tree.
027   */
028  public Collection<Person> getPeople() {
029    return this.people.values();
030  }
031
032  /**
033   * Returns whether or not this family tree contains a person with
034   * the given id.
035   */
036  public boolean containsPerson(int id) {
037    return this.people.containsKey(new Integer(id));
038  }
039
040  /**
041   * Returns a person in this family tree with a given id.  If no
042   * person with that id exists in this family tree, then
043   * <code>null</code> is returned.
044   */
045  public Person getPerson(int id) {
046    return this.people.get(new Integer(id));
047  }
048
049  /**
050   * Adds a person to this family tree.  If a person with the same id
051   * as the person being added already exists in this family tree, the
052   * old person is removed and replaced with the new.
053   */
054  public void addPerson(Person person) {
055    this.people.put(person.getId(), person);
056  }
057
058  //////////////////////// Utility Methods  ///////////////////////
059
060  public String toString() {
061    return "A FamilyTree with " + this.people.size() + " people";
062  }
063
064}