package NonAWTBPEvents; import java.util.*; import java.awt.*; /** * The Patient class is the source for Blood Pressure Events * * @author Stuart Hansen * @version February 8, 2009 */ public class Patient extends Component { private static int SYSTOLIC_HIGH = 140; private static int SYSTOLIC_LOW = 90; // the vector of Blood Pressure Listeners // Note that we use Vector, rather than ArrayList, as Vectors are synchronized private Vector bpListeners; private String name; // the patient's name private int systolic; // the patient's systolic blood pressure private int diastolic; // the patient's diastolic blood pressure private int systolicTooHigh; // the high level where an alarm sounds private int systolicTooLow; // the low level where an alarm sounds private boolean warningFired; // keep track of whether a warning event // has recently fired. // the constructor public Patient (String name, int systolic, int diastolic) { bpListeners = new Vector(); this.name = name; setSystolic(systolic); setDiastolic(diastolic); setSystolicWarningLevels(SYSTOLIC_HIGH, SYSTOLIC_LOW); } // Get the current systolic blood pressure public int getSystolic () { return systolic; } // Set the levels that will generate warnings public void setSystolicWarningLevels(int tooHigh, int tooLow) { systolicTooHigh = tooHigh; systolicTooLow = tooLow; } // set the current systolic blood pressure public void setSystolic (int systolic) { int oldsystolic = this.systolic; this.systolic = systolic; // Check to see if a warning event is warranted if (!warningFired && (systolic >= systolicTooHigh || systolic <= systolicTooLow)) { warningFired = true; fireWarningEvent(new BloodPressureEvent( this, systolic)); } else if (systolic < systolicTooHigh && systolic > systolicTooLow) warningFired = false; // Check if an ordinary systolic change is warranted. if (oldsystolic != systolic) { fireSystolicEvent(new BloodPressureEvent( this, systolic)); } } // get the Patient's name public String getName() { return name; } // get the current diastolic blood pressure public int getDiastolic () { return diastolic; } // set the current diastolic blood pressure public void setDiastolic (int diastolic) { int olddiastolic = this.diastolic; this.diastolic = diastolic; if (olddiastolic != diastolic) { fireDiastolicEvent(new BloodPressureEvent ( this, diastolic)); } } // We call each of the handlers directly public void fireSystolicEvent (BloodPressureEvent e) { // clone the vector so registrations don't affect this event instance Vector temp = (Vector) bpListeners.clone(); // Walk through the vector firing events to each listener Iterator bplIterator = temp.iterator(); while (bplIterator.hasNext()) { BloodPressureListener bpl = bplIterator.next(); bpl.systolicChange(e); } } // We call each of the handlers directly public void fireDiastolicEvent (BloodPressureEvent e) { // clone the vector so registrations don't affect this event instance Vector temp = (Vector) bpListeners.clone(); // Walk through the vector firing events to each listener Iterator bplIterator = temp.iterator(); while (bplIterator.hasNext()) { BloodPressureListener bpl = bplIterator.next(); bpl.diastolicChange(e); } } // We call each of the handlers directly public void fireWarningEvent (BloodPressureEvent e) { // clone the vector so registrations don't affect this event instance Vector temp = (Vector) bpListeners.clone(); // Walk through the vector firing events to each listener Iterator bplIterator = temp.iterator(); while (bplIterator.hasNext()) { BloodPressureListener bpl = bplIterator.next(); bpl.warning(e); } } // add a blood pressure listener to this patient public void addBloodPressureListener (BloodPressureListener bpListener) { bpListeners.add(bpListener); } // remove a blood pressure listener from this patient public void removeBloodPressureListener (BloodPressureListener bpListener) { bpListeners.remove(bpListener); } }