For a college Assignment I need to implement part of a hospital patient waiting system.The system uses a Collection of Patients on a waiting list, and a set of Patients registered for an operation during a specified period, for example this year.
I have implemented the method required as below, using a HashSet and a LinkedList. The method is almost completely synchronized, so I am wondering if there is a more efficient implementation with less synchronization, or perhaps more granular read write synchronization using locks?
public Class OperationPeriod {
...
private Set<Patient> registeredPatients=new HashSet<Patient>();
private Collection<Patient> waitingListPatients=new LinkedList<Patient>();
private int capacity;
...
public boolean bookOperation(Patient patient){
if (!Operation.checkHasMetRequirements(patient)) {
return false;
}
//patient could already be registered
synchronized(this) {
if(registeredPatients.contains(patient)) {
return true;
}
if(waitingListPatients.contains(patient) ) {
return false;
}
//Not already registered so register or add to waiting list
return addPatient(patient);
}
}
private boolean addPatient(Patient patient) {
if(registeredPatients.size() < capacity) {
registeredPatients.add(patient);
return true;
}
else {
waitingListPatients.add(patient);
return false;
}
}