FileDocCategorySizeDatePackage
Individual.javaAPI DocExample11928Tue Dec 08 01:21:00 GMT 1998ProblemDomain

Individual.java

// Copyright 1997 Object UK Ltd.
// Author Andrew Carmichael
// Updated Tim Shelley
//
// Date
// Version 0.2
// Email: objectuk@objectuk.co.uk
// Web: http://www.objectuk.co.uk

package ProblemDomain;

// Generated by Together/J

import java.security.InvalidParameterException;
import java.util.*;

/**@stereotype role
@note Individuals are the core components that make up the Family 
objects and record details about the Individual.*/
public class Individual {
  /**@shapeType AggregationLink
@name birth
@supplierCardinality 0..1
*/
  Birth birth;
  /**@shapeType AggregationLink
@supplierCardinality 0..1
*/
  Death death;
  /**@shapeType AssociationLink
@supplierRole Child of
@supplierCardinality 0..1*/
  Family childOf;

  //CONSTRUCTORS

  public Individual() {
    this(Individual.getNextReference());}

  public Individual (int reference) {
    this.reference = reference;
    //Put in hash table
    Individual.All.put(new Integer(reference), this);
    }

  public Individual(int reference, String FamilyName,
      String GivenNames, String sex, Birth birth, Death death){
    this(reference);
    this.setFamilyName(FamilyName);
    this.setGivenNames(GivenNames);
    this.setSex(sex);
    this.setBirth(birth);
    this.setDeath(death);
    ++lastReference;
    }

  // OTHER METHODS

/**@note Deletes itself from the main list of Individuals by removing 
the record identified by its own reference id.*/
  public void delete() {
    //Remove from hash table
    Individual.All.remove(new Integer(this.getReference()));}


/**@note Returns the details of the Individual as one string, with '\n' 
denoting carriage returnss - this method is used to display the 
Individual to the screen,*/
  public String getDetails() {
    String s = new String();
    s = s + "Reference No.: " + Integer.toString(this.reference) + "\n";
    if ((this.familyName + this.givenNames) != "")
      s = s + " NAME " + this.givenNames + " " + this.familyName + "\n";
    if (this.birth != null) s = s + "\nBirth: \n     " + this.birth.getDetails();
    if (this.death != null) s = s + "\nDeath: \n     " + this.death.getDetails();
    if (this.childOf != null) s = s + "Child of Family: " + this.childOf.getDisplayName() + "\n";
    if (this.spouseInList != null) {
      Enumeration e = this.spouseInList.elements();
      Family f;
      s = s + "Parent of families: \n";
      while (e.hasMoreElements()) {
        f = (Family)(e.nextElement());
        s = s + f.getDisplayName() + "\n";}
    }
    return s;
  }

/**@note Returns the text record identifier required for the data 
field to be recognised when read back in from file.*/
  public String getTextRecordIdentifier() {
    return "@I" + java.lang.Integer.toString(this.reference) + "@ INDI";}
  
  public String toString() {
    return this.toString("");}

  /** @note Outputs string for the Individual. Each line begins with a String
  defined by the parameter. If this string converts to an integer,
  the prefix for contained objects will be incremented by 1. */
  public String toString(String sn) {
    String s = sn + " " + getTextRecordIdentifier() + "\n";
    try {
      sn = Integer.toString(Integer.parseInt(sn)+1);}
    catch (NumberFormatException e) {
      sn = sn + "  ";}
    s = s + sn + " " + "REFN P" + Integer.toString(this.reference) + "\n";
    if ((this.familyName + this.givenNames) != "")
      s = s + sn + " " + "NAME " + this.givenNames + " /" + this.familyName + "/\n";
    s = s + sn + " " + "SEX " + getSex() + "\n";
    if (this.birth != null) s = s + this.birth.toString(sn);
    if (this.death != null) s = s + this.death.toString(sn);
    if (this.childOf != null) s = s + sn + " " + "FAMC @F" + Integer.toString(this.childOf.getReference()) + "@\n";
    if (this.spouseInList != null) {
      Enumeration e = this.spouseInList.elements();
      Family f;
      while (e.hasMoreElements()) {
        f = (Family)(e.nextElement());
        s = s + sn + " " + "FAMS @F" + Integer.toString(f.getReference()) + "@\n";}
    }
    return s;
  }

  // PROPERTIES

  private int reference;
/**@note Returns the reference number of this Individual.*/
  public int getReference() {return this.reference;}

  private String givenNames;
/**@note Returns the given names of the individual - a string of names 
they are or were known by.*/
  public String getGivenNames() {return this.givenNames;}
/**@note Set the given names for this Individual, the different names 
are seperated by spaces.*/
  public void setGivenNames(String s) {this.givenNames = s; return;}

  private String familyName;
/**@note Return the family name - the family name is usually the surname 
of the husband of the family.*/
  public String getFamilyName() {return this.familyName;}
/**@note Set the family name.*/
  public void setFamilyName(String s) {this.familyName = s; return;}

/**@note Returns the Individuals name.*/
  public String getName() {
    return givenNames + " " + familyName;
  }

  private String sex;
  public static final String MALE = "M";
  public static final String FEMALE = "F";
  public static final String INDETERMINATE = "I";
/**@note Returns the sex of the Individual.*/
  public String getSex() {return this.sex;}
/**@note Set the sex of the Inidividual.  There is the flexibility that 
if you do not know the sex and you canot tell the sex by the 
name, you can enter I for Indeterminate.*/
  public void setSex(String s) throws InvalidParameterException {
    if (s.equals("M") || s.equals("F") || s.equals("I"))
      this.sex = s;
    else
      throw new InvalidParameterException();
    return;}

  // RELATIONSHIPS

/**@note Return the Birth object that records the birth of the Individual.*/
  public Birth getBirth() {return this.birth;}
/**@note Set up the Birth object for this Individual.*/
  public void setBirth(ProblemDomain.Birth b) {this.birth = b; return;}

/**@note Return the Death object that records the death of the Individual.*/  
  public Death getDeath() {return this.death;}
/**@note Set up the Death object for this Individual.*/  
  public void setDeath(ProblemDomain.Death d) {this.death = d; return;}

  private Vector notes;
/**@note Add a note to the Individual.*/
  public void addNote(ProblemDomain.Note comments){
    if (this.notes == null) notes = new Vector(1);
    notes.addElement(comments); return;}
/**@note Remove the note if you know the actual object to remove.*/
  public void removeNote(ProblemDomain.Note comments) {
    notes.removeElement(comments); return;}
/**@note Remove the note at a specific position.*/
  public void removeNoteAt(int i) {
    notes.removeElementAt(i); return;}
/**@note Add/change the note in the comments at the index passed to the 
method.*/
  public void setNoteAt(int i, ProblemDomain.Note s) {
    notes.setElementAt(s,i); return;}
/**@note Return all the notes for this Individual as an Enumeration*/
  public Enumeration getNotes() {
    return notes.elements();}

  // Relationships with Family

//  private Family childOf;
/**@note Return the Family to which this Individual is a child of.*/
  public Family getChildOf() {return this.childOf;}

/**@note Link this Individual to a Family.  Must be called after 
Individual added as child to Family*/
  protected void linkAsChild(ProblemDomain.Family f) throws InvalidParameterException {
    // check it has already been added at other end
    if (f.hasAsChild(this)) {
      // Remove this from current family if there is one
      if (this.childOf != null) this.childOf.removeChild(this);
      // link
      this.childOf = f;}
    else {
      // 'this' is not a child of the family f.
      throw new InvalidParameterException();}
    return;}

/**@note Remove the link between this Individual and the Family it is a 
child of. Must be called after Individual removed as child of Family*/
  protected void unlinkAsChild(ProblemDomain.Family f) throws InvalidParameterException {
    if (!f.hasAsChild(this)) {
      if (this.childOf == f) {
        this.childOf = null;}
      else
        // childOf references different family
        throw new InvalidParameterException();}
    else {
      // 'this' is still a child of the family f.
      throw new InvalidParameterException();}
    return;}

  private Vector spouseInList = null;

/**@note Return the Families that this Individual is or has been a 
spouse of.*/
  public Enumeration getSpouseInList() {
    return this.spouseInList.elements();
  }

/**@note Return whether or not the Individual is a parent of a Family.*/
  public boolean isParent() {
    if(spouseInList == null) return false;
    else return true;
  }

/**@note Remove the Individual from the Family that they are a spouse of.
Must be called after Individual removed as spouse to Family*/
  protected void unlinkAsSpouse(ProblemDomain.Family f) throws InvalidParameterException {
    // check 'this' is not still a spouse
    if (!f.hasAsSpouse(this)) {
      this.spouseInList.removeElement(f);}
    else {
      // 'this' is still a spouse of the family f.
      throw new InvalidParameterException();}
    return;}

  /** 
@note Link the Inidividual as a spouse of the Family that is passed 
to the method. Must be called after Individual added as spouse 
to Family*/
  protected void linkAsSpouse(ProblemDomain.Family f) throws InvalidParameterException {
    if (f.hasAsSpouse(this)) {
      if (this.spouseInList == null) this.spouseInList = new Vector(1);
      this.spouseInList.addElement(f);}
    else {
      // 'this' is not a spouse of the family f.
      throw new InvalidParameterException();}
    return;}



  // STATIC CLASS VARIABLES AND METHODS

  private static int lastReference = 0;
/**@note Static method to return a number to be used as the reference 
number for the new Individual.*/
  protected static int getNextReference() {return ++lastReference;}
/**@note Static method to reset or change the reference number.*/
  protected static void setNextReference(int n) {
    if (lastReference < n) lastReference = n-1; return;}

  public void reset() { lastReference = 0; }
    
  // HASH TABLE FOR INDIVIDUALS

  private static Hashtable All = new Hashtable();
/**@note A static method to return all the Individual records as an 
Enumeration.*/
  public static Enumeration getAll() {return All.elements();}
/**@note Static method to return an Individual at the specific place in 
the Individual hashtable.*/
  public static Individual getIndividual(int reference) {
    return (Individual)(Individual.All.get(new Integer(reference)));}

  // TEST OBJECT

  public static Individual testObject(){
    return new Individual(
      1,
      "Bloggs",
      "Joe",
      MALE,
      Birth.testObject(),
      null);
  }

  public static Individual testObject2(){
    return new Individual(
      2,
      "Smith",
      "Mary",
      FEMALE,
      Birth.testObject(),
      null);
  }

  public static Individual testObject3(){
    return new Individual(
      3,
      "Bloggs",
      "John",
      MALE,
      Birth.testObject(),
      null);
  }

  public static Individual testObject4(){
    return new Individual(
      4,
      "Bloggs",
      "Jane",
      FEMALE,
      Birth.testObject(),
      null);
  }

  public static Individual testObject5(){
    return new Individual(
      5,
      "Winters",
      "Susan",
      FEMALE,
      Birth.testObject(),
      null);
  }

  public static Individual testObject6(){
    return new Individual(
      6,
      "Bloggs",
      "Robert",
      MALE,
      Birth.testObject(),
      null);
  }

}