package AWTBPEvents; 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; fireBPEvent(new BloodPressureEvent( this, BloodPressureEvent.WARNING_EVENT, systolic)); } else if (systolic < systolicTooHigh && systolic > systolicTooLow) warningFired = false; // Check if an ordinary systolic change is warranted. if (oldsystolic != systolic) { fireBPEvent(new BloodPressureEvent( this, BloodPressureEvent.SYSTOLIC_EVENT, 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) { fireBPEvent(new BloodPressureEvent ( this, BloodPressureEvent.DIASTOLIC_EVENT, diastolic)); } } // Firing the event requires enqueuing it public void fireBPEvent (BloodPressureEvent e) { Toolkit kit = java.awt.Toolkit.getDefaultToolkit(); EventQueue queue = kit.getSystemEventQueue(); queue.postEvent(e); } // Handle the blood pressure event when it comes out of the queue public void handleBPEvent (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()) { // The switch is needed to sort through the three types of // blood pressure events BloodPressureListener bpl = bplIterator.next(); switch (e.getID()){ case BloodPressureEvent.DIASTOLIC_EVENT: bpl.diastolicChange(e); break; case BloodPressureEvent.SYSTOLIC_EVENT: bpl.systolicChange(e); break; case BloodPressureEvent.WARNING_EVENT: 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); } }