FileDocCategorySizeDatePackage
Family.javaAPI DocExample13691Tue Dec 08 01:21:00 GMT 1998ProblemDomain

Family.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.util.*;

/**@version 0.2
@author Tim Shelley
@note The Family class is the main class in the Problem Domain, and 
is made up of Individuals.*/
public class Family {
  /**@shapeType AggregationLink
@supplierCardinality 0..1
*/
  Marriage ceremony;
  /**@shapeType AggregationLink
@name decree
@supplierCardinality 0..1
*/
  Divorce decree;
/**@supplierRole wife
@supplierCardinality 0..1*/
  Individual wife;
/**@supplierRole husband
@supplierCardinality 0..1*/
  Individual husband;

  /** CONSTRUCTORS */
                      
  public Family() {
    this(Family.getNextReference());}

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

  /** CONSTRUCTORS */
  public Family(int reference, Individual husband,
      Individual wife, Marriage ceremony, Divorce decree){
    this(reference);
    this.setHusband(husband);
    this.setWife(wife);
    this.setMarriage(ceremony);
    this.setDivorce(decree);
    lastReference++;
    }

/** @note Method to remove itself from the Hashtable of Families*/
  public void delete() {
    // Remove from hash table
    Family.All.remove(new Integer(this.getReference()));}

/**@note Create a list of ancestors for the family.
@return Vector - compiled list of ancestors*/
    public Vector getAncestors(int spaces) {
    Vector storage = null;
    Vector temp;
    Enumeration list;
    Family family;
    String name;
    String gap = new String();

    for(int i=0;i<spaces;i++) gap = gap + "     ";
    if(spaces == 0) {
      if(storage == null) storage = new Vector();
      storage.addElement("Fathers Side:\n");
    }
    if(getHusband() != null) {
      if(storage == null) storage = new Vector();
      name = gap + getHusband().getName();
      if(getHusband().getName().equals("null null")) name = gap + "Unknown";
      storage.addElement(name);
      if(getHusband().getChildOf() != null) {
        temp = getHusband().getChildOf().getAncestors(spaces+1);
        if(temp != null) {
          list = temp.elements();
          while(list.hasMoreElements()) {
            storage.addElement(list.nextElement());
          }
        }
      }
    }
    if(spaces == 0) {
      if(storage ==  null) storage = new Vector();
      storage.addElement("\n\nMothers Side:\n");
    }
    if(getWife() != null) {
      if(storage == null) storage = new Vector();
      name = gap + getWife().getName();
      if(getWife().getName().equals("null null")) name = gap + "Unknown";
      storage.addElement(name);
      if(getWife().getChildOf() != null) {
        temp = getWife().getChildOf().getAncestors(spaces+1);
        if(temp != null) {
          list = temp.elements();
          while(list.hasMoreElements()) {
            storage.addElement(list.nextElement());
          }
        }
      }
    }
    return storage;
  }

/**@note Create a list of descendents for the family.
@return Vector - compiled list of ancestors*/  
  public Vector getDescendants(String parentsName,int spaces, boolean showSpouse) {
    Enumeration kids;
    Enumeration subkids;
    Individual person;
    Individual child;
    Vector descendantList = null;
    Vector subList = null;
    String individualsName = new String();
    String childsName = new String();
    String gaps = new String();

    for(int i=0;i<spaces;i++) {
      gaps = gaps + "    ";
    }

    if(children != null) {
      kids = getChildren();
      while(kids.hasMoreElements()) {
        if(descendantList == null) descendantList = new Vector();
        person = (Individual)kids.nextElement();
        individualsName = gaps + person.getGivenNames() + " " + person.getFamilyName();
        if(showSpouse) {
          individualsName = individualsName + " = " + parentsName;
        }
        descendantList.addElement(individualsName);
        if(person.isParent()) {
          subkids = person.getSpouseInList();
          if(subkids != null)
          {
            while(subkids.hasMoreElements()) {
              if(subList == null) subList = new Vector();
              subList = ((Family)subkids.nextElement()).getDescendants(person.getName(),spaces+1,showSpouse);
              if(subList != null) {
                Enumeration l = subList.elements();
                while(l.hasMoreElements()) {
                  childsName = (String)l.nextElement();
                  descendantList.addElement(childsName);
                }
              }
            }
          }
        }
      }
    }
    return descendantList;
  }
                       
/**@note Return the family details as a String with the /n character to 
show where a carriage return should be.
@return String - details of the family*/
  public String getDetails() {
    String s = new String();

    s = s + "Reference No.:" + Integer.toString(this.reference) + "\n";
    if (this.husband != null) s = s + "Huband: " + this.husband.getName() + "\n";
    if (this.wife != null) s = s + "Wife: " + this.wife.getName() + "\n";
    if(this.children != null) {
      Enumeration kids = this.getChildren();
      s = s + "Children: \n";
      while (kids.hasMoreElements()) {
        s = s + "     " + ((Individual)kids.nextElement()).getName() + "\n";
      }
    }
    if (this.ceremony != null) s = s + "\nMarriage: \n     " + this.ceremony.getDetails();
    if (this.decree != null) s = s + "\nDivorce: \n     " + this.decree.getDetails();
    return s;
  }

  // PROPERTIES

  private int reference;

/**@note Return the unique reference no. for the Family object.*/
  public int getReference() {
    return this.reference;
  }

  private String displayName;

/**@note Return the display name of the family - i.e. family name*/
  public String getDisplayName() {
    return this.displayName;
  }

  private boolean notSolemnised;

  public boolean isNotSolemnised() {
    return this.notSolemnised;
  }

  public void setNotSolemnised(boolean b) {
    this.notSolemnised = b;
  }

  // RELATIONSHIPS

/**@note Return the husband linked to this family.*/
  public Individual getHusband() {
    return this.husband;
  }

/**@note Link the individual passed as the husband to this family.*/  
  public void setHusband(ProblemDomain.Individual p) {
    // unlink other end
    if (husband != null) {
      husband.unlinkAsSpouse(this);}
    // set
    this.husband = p;
    // link other end
    husband.linkAsSpouse(this);
    // update display name
    this.displayName = this.husband.getFamilyName() + " = ";
    if (this.wife != null) this.displayName = this.displayName
      + this.wife.getFamilyName();
    return;
  }

/**@note Return the wife linked to this family.*/  
  public Individual getWife() {
    return this.wife;
  }

/**@note Link the individual passed as the wife to this family.
@param p - the Individual object that represents the wife.*/  
  public void setWife(ProblemDomain.Individual p) {
    // unlink other end
    if (wife != null) {
      wife.unlinkAsSpouse(this);}
    // set
    this.wife = p;
    // link other end
    wife.linkAsSpouse(this);
    // update display name
    this.displayName = " = " + this.wife.getFamilyName();
    if (this.husband != null) this.displayName = this.husband.getFamilyName() + this.displayName;
    return;
  }

/**@note List of children (Individuals) belonging to this family.*/
  private Vector children = null;

/**@note Return all the children linked to this family as an Enumeration.*/
  public Enumeration getChildren() {
    return this.children.elements();
  }

/**@note Add the passed Individual as a child to this family.
@param Individual - the child to be added*/
  public void addChild(ProblemDomain.Individual p) {
    if (this.children == null) children = new Vector(1);
    this.children.addElement(p);
    // link from other end
    p.linkAsChild(this);
  }

/**@note Remove the Individual, passed to this method, from the list of children.*/
  public void removeChild(ProblemDomain.Individual p) {
    this.children.removeElement(p);
    // unlink other end
    p.unlinkAsChild(this);
  }

/**@note Check to see if the passed individual is part of the children 
of this family.
@return boolean - true if individual is a child of this family, else false*/
  public boolean hasAsChild(ProblemDomain.Individual c) {
    return this.children.contains(c);
  }

/**@note Check to see if the passed individual is the spouse of this family.
@return boolean - true if individual is a spouse of this family, else false*/
  public boolean hasAsSpouse(ProblemDomain.Individual p) {
    return (this.husband == p | this.wife == p);
  }

/**@note Return the marriage associated with this family.
@return Marriage - associated marriage or null*/
  public Marriage getMarriage() {
    return this.ceremony;
  }

/**@note Set the marriage associated with this family.*/  
  public void setMarriage(ProblemDomain.Marriage p) {
  this.ceremony = p; return;
  }

/**@note Return the divorce associated with this family.
@return Divorce - associated divorce or null*/  
  public Divorce getDivorce() {
    return this.decree; 
  }

/**@note Set the divorce associated with this family.*/    
  public void setDivorce(ProblemDomain.Divorce p) {
    this.decree = p; return;
  }

/**@associates <b>Note</b>*/
  private Vector notes;

/**@note Add notes about the family*/
  public void addNote(ProblemDomain.Note comments){
    if (this.notes == null) notes = new Vector(1);
    notes.addElement(comments); return;
  }

/**@note Remove note from family, by locating the note object passed to 
it and removing it.*/
  public void removeNote(ProblemDomain.Note comments) {
    notes.removeElement(comments); return;
  }

/**@note Remove note from family, by using the index of the note passed to 
it and removing it.*/  
  public void removeNoteAt(int i) {
    notes.removeElementAt(i); return;
  }

/**@note Replace a specific note object with a new one.*/
  public void setNoteAt(int i, ProblemDomain.Note s) {
    notes.setElementAt(s,i); return;
  }

/**@note Return all the notes associated with this family as an 
Enumeration.*/
  public Enumeration getNotes() {
    return notes.elements();
  }

  // STATIC CLASS VARIABLES AND METHODS

  private static int lastReference = 0;

/**@note Static method to allow unique reference numbers to be 
gerenrated from the static variable - lastReference.*/
  protected static int getNextReference() {
    return ++lastReference;
  }

  protected static void setNextReference(int n) {
    if (lastReference < n) lastReference = n-1; return;
  }

  public void reset() { lastReference = 0; }

/**@note A Hashtable to store all families.*/
  private static Hashtable All = new Hashtable();

/**@note Return the family with the given reference, using the reference 
to directly access its location in the Hashtable.*/
  public static Family getFamily(int reference) {
    return (Family)(Family.All.get(new Integer(reference)));
  }

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

/** @note Outputs string for the LifeEvent. 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 F" + Integer.toString(this.reference) + "\n";
    if (this.husband != null) s = s + sn + " " + "HUSB @I" + Integer.toString(this.husband.getReference()) + "@\n";
    if (this.wife != null) s = s + sn + " " + "WIFE @I" + Integer.toString(this.wife.getReference()) + "@\n";
    if(this.children != null) {
      Enumeration kids = this.getChildren();
      while (kids.hasMoreElements()) {
        s = s + sn + " " + "CHIL @I" + Integer.toString(((Individual)kids.nextElement()).getReference()) + "@\n";
      }
    }
    if (this.ceremony != null) s = s + this.ceremony.toString(sn);
    if (this.decree != null) s = s + this.decree.toString(sn);
    s = s + sn + " " + "_PCODE F" + Integer.toString(this.reference) + "\n";
    s = s + sn + " " + "_PNAME " + this.displayName + "\n";
    s = s + sn + " " + "_FRECT " + "\n";    
    return s;
  }

/** @note Simple method to create test data for a family object.*/
  public static Family testObject() {
    Family f = new Family(1,
      Individual.testObject(), Individual.testObject2(),
      Marriage.testObject(), Divorce.testObject());
    f.addChild(Individual.testObject3());
    f.addChild(Individual.testObject4());
    f.addNote(new Note("A fine family there now."));
    f.addNote(new Note("And that's the truth!"));
    return f;
  }
}