1 package jalview.workers;
3 import java.util.ArrayList;
4 import java.util.HashSet;
5 import java.util.Hashtable;
9 import jalview.api.AlignCalcManagerI;
10 import jalview.api.AlignCalcWorkerI;
11 import jalview.datamodel.AlignmentAnnotation;
13 public class AlignCalcManager implements AlignCalcManagerI
15 private volatile List<AlignCalcWorkerI> restartable = new ArrayList<AlignCalcWorkerI>();
17 private List<Class> blackList = new ArrayList<Class>();
20 * global record of calculations in progress
22 private static Hashtable<Class, AlignCalcWorkerI> inProgress = new Hashtable<Class, AlignCalcWorkerI>();
25 * record of calculations pending or in progress in the current context
27 private Map<Class, List<AlignCalcWorkerI>> updating = new Hashtable<Class, List<AlignCalcWorkerI>>();
30 public void notifyStart(AlignCalcWorkerI worker)
32 List<AlignCalcWorkerI> upd = updating.get(worker.getClass());
35 updating.put(worker.getClass(),
36 upd = new ArrayList<AlignCalcWorkerI>());
42 public synchronized boolean alreadyDoing(AlignCalcWorkerI worker)
44 return inProgress.containsKey(worker.getClass());
48 public synchronized boolean notifyWorking(AlignCalcWorkerI worker)
50 // synchronized (inProgress)
52 // TODO: decide if we should throw exceptions here if multiple workers
54 if (inProgress.get(worker.getClass()) != null)
59 .println("Warning: Multiple workers are running of type "
64 inProgress.put(worker.getClass(), worker);
69 private HashSet<AlignCalcWorkerI> canUpdate=new HashSet<AlignCalcWorkerI>();
71 public synchronized void workerComplete(AlignCalcWorkerI worker)
73 inProgress.remove(worker.getClass());
74 List<AlignCalcWorkerI> upd = updating.get(worker.getClass());
78 canUpdate.add(worker);
84 public void workerCannotRun(AlignCalcWorkerI worker)
86 blackList.add(worker.getClass());
89 public boolean isBlackListed(Class workerType)
91 return blackList.contains(workerType);
95 public void startWorker(AlignCalcWorkerI worker)
97 new Thread(worker).start();
101 public synchronized boolean isWorking(AlignCalcWorkerI worker)
103 // System.err.println("isWorking : worker "+(worker!=null ?
104 // worker.getClass():"null")+ " "+hashCode());
105 return worker != null && inProgress.get(worker.getClass()) == worker;
109 public boolean isWorking()
111 // System.err.println("isWorking "+hashCode());
112 return inProgress.size() > 0;
116 public void registerWorker(AlignCalcWorkerI worker)
118 if (!restartable.contains(worker))
120 restartable.add(worker);
126 public void restartWorkers()
128 for (AlignCalcWorkerI worker : restartable)
135 public boolean workingInvolvedWith(AlignmentAnnotation alignmentAnnotation)
139 for (List<AlignCalcWorkerI> workers: updating.values())
141 for (AlignCalcWorkerI worker:workers)
142 if (worker.involves(alignmentAnnotation))
152 public void updateAnnotationFor(Class workerClass)
154 for (AlignCalcWorkerI worker:canUpdate.toArray(new AlignCalcWorkerI[0]))
156 if (workerClass.equals(worker.getClass()))
158 worker.updateAnnotation();
164 public List<AlignCalcWorkerI> getRegisteredWorkersOfClass(
167 List<AlignCalcWorkerI> workingClass=new ArrayList<AlignCalcWorkerI>();
168 for (AlignCalcWorkerI worker:canUpdate.toArray(new AlignCalcWorkerI[0]))
170 if (workerClass.equals(worker.getClass()))
172 workingClass.add(worker);
175 return (workingClass.size()==0) ? null : workingClass;
179 public boolean startRegisteredWorkersOfClass(Class workerClass)
181 List<AlignCalcWorkerI> workers=getRegisteredWorkersOfClass(workerClass);
186 for (AlignCalcWorkerI worker: workers) {
193 public void workerMayRun(AlignCalcWorkerI worker)
195 if (blackList.contains(worker.getClass()))
197 blackList.remove(worker.getClass());