2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.workers;
23 import jalview.api.AlignCalcManagerI;
24 import jalview.api.AlignCalcWorkerI;
25 import jalview.datamodel.AlignmentAnnotation;
27 import java.util.ArrayList;
28 import java.util.Collections;
29 import java.util.HashSet;
30 import java.util.Hashtable;
31 import java.util.List;
35 public class AlignCalcManager implements AlignCalcManagerI
37 private volatile List<AlignCalcWorkerI> restartable = Collections
38 .synchronizedList(new ArrayList<AlignCalcWorkerI>());
40 private volatile List<Class> blackList = Collections
41 .synchronizedList(new ArrayList<Class>());
44 * global record of calculations in progress
46 private volatile Map<Class, AlignCalcWorkerI> inProgress = Collections
47 .synchronizedMap(new Hashtable<Class, AlignCalcWorkerI>());
50 * record of calculations pending or in progress in the current context
52 private volatile Map<Class, List<AlignCalcWorkerI>> updating = Collections
53 .synchronizedMap(new Hashtable<Class, List<AlignCalcWorkerI>>());
56 public void notifyStart(AlignCalcWorkerI worker)
58 synchronized (updating)
60 List<AlignCalcWorkerI> upd = updating.get(worker.getClass());
66 .synchronizedList(new ArrayList<AlignCalcWorkerI>()));
76 public boolean alreadyDoing(AlignCalcWorkerI worker)
78 synchronized (inProgress)
80 return inProgress.containsKey(worker.getClass());
87 * @see jalview.api.AlignCalcManagerI#isPending(jalview.api.AlignCalcWorkerI)
90 public boolean isPending(AlignCalcWorkerI workingClass)
92 List<AlignCalcWorkerI> upd;
93 synchronized (updating)
95 upd = updating.get(workingClass.getClass());
111 // TODO make into api method if needed ?
112 public int numberLive(AlignCalcWorkerI worker)
114 synchronized (updating)
116 List<AlignCalcWorkerI> upd = updating.get(worker.getClass());
127 public boolean notifyWorking(AlignCalcWorkerI worker)
129 synchronized (inProgress)
131 // TODO: decide if we should throw exceptions here if multiple workers
133 if (inProgress.get(worker.getClass()) != null)
138 .println("Warning: Multiple workers are running of type "
139 + worker.getClass());
143 inProgress.put(worker.getClass(), worker);
148 private final HashSet<AlignCalcWorkerI> canUpdate = new HashSet<AlignCalcWorkerI>();
151 public void workerComplete(AlignCalcWorkerI worker)
153 synchronized (inProgress)
155 // System.err.println("Worker "+worker.getClass()+" marked as complete.");
156 inProgress.remove(worker.getClass());
157 List<AlignCalcWorkerI> upd = updating.get(worker.getClass());
164 canUpdate.add(worker);
170 public void workerCannotRun(AlignCalcWorkerI worker)
172 synchronized (blackList)
174 blackList.add(worker.getClass());
178 public boolean isBlackListed(Class workerType)
180 synchronized (blackList)
182 return blackList.contains(workerType);
187 public void startWorker(AlignCalcWorkerI worker)
189 // System.err.println("Starting "+worker.getClass());
190 // new Exception("").printStackTrace();
191 Thread tw = new Thread(worker);
192 tw.setName(worker.getClass().toString());
197 public boolean isWorking(AlignCalcWorkerI worker)
199 synchronized (inProgress)
200 {// System.err.println("isWorking : worker "+(worker!=null ?
201 // worker.getClass():"null")+ " "+hashCode());
202 return worker != null && inProgress.get(worker.getClass()) == worker;
207 public boolean isWorking()
209 synchronized (inProgress)
211 // System.err.println("isWorking "+hashCode());
212 return inProgress.size() > 0;
217 public void registerWorker(AlignCalcWorkerI worker)
219 synchronized (restartable)
221 if (!restartable.contains(worker))
223 restartable.add(worker);
230 public void restartWorkers()
232 synchronized (restartable)
234 for (AlignCalcWorkerI worker : restartable)
242 public boolean workingInvolvedWith(AlignmentAnnotation alignmentAnnotation)
244 synchronized (inProgress)
246 for (AlignCalcWorkerI worker : inProgress.values())
248 if (worker.involves(alignmentAnnotation))
254 synchronized (updating)
256 for (List<AlignCalcWorkerI> workers : updating.values())
258 for (AlignCalcWorkerI worker : workers)
260 if (worker.involves(alignmentAnnotation))
271 public void updateAnnotationFor(Class workerClass)
274 AlignCalcWorkerI[] workers;
275 synchronized (canUpdate)
277 workers = canUpdate.toArray(new AlignCalcWorkerI[0]);
279 for (AlignCalcWorkerI worker : workers)
281 if (workerClass.equals(worker.getClass()))
283 worker.updateAnnotation();
289 public List<AlignCalcWorkerI> getRegisteredWorkersOfClass(
292 List<AlignCalcWorkerI> workingClass = new ArrayList<AlignCalcWorkerI>();
293 AlignCalcWorkerI[] workers;
294 synchronized (canUpdate)
296 workers = canUpdate.toArray(new AlignCalcWorkerI[canUpdate.size()]);
298 for (AlignCalcWorkerI worker : workers)
300 if (workerClass.equals(worker.getClass()))
302 workingClass.add(worker);
305 return (workingClass.size() == 0) ? null : workingClass;
309 public boolean startRegisteredWorkersOfClass(Class workerClass)
311 List<AlignCalcWorkerI> workers = getRegisteredWorkersOfClass(workerClass);
316 for (AlignCalcWorkerI worker : workers)
318 if (!isPending(worker))
324 System.err.println("Pending exists for " + workerClass);
331 public void workerMayRun(AlignCalcWorkerI worker)
333 synchronized (blackList)
335 if (blackList.contains(worker.getClass()))
337 blackList.remove(worker.getClass());
343 public void removeRegisteredWorkersOfClass(Class typeToRemove)
345 List<AlignCalcWorkerI> workers = getRegisteredWorkersOfClass(typeToRemove);
346 List<AlignCalcWorkerI> removable = new ArrayList<AlignCalcWorkerI>();
347 Set<AlignCalcWorkerI> toremovannot = new HashSet<AlignCalcWorkerI>();
348 synchronized (restartable)
350 for (AlignCalcWorkerI worker : restartable)
352 if (typeToRemove.equals(worker.getClass()))
354 removable.add(worker);
355 toremovannot.add(worker);
358 restartable.removeAll(removable);
360 synchronized (canUpdate)
362 for (AlignCalcWorkerI worker : canUpdate)
364 if (typeToRemove.equals(worker.getClass()))
366 removable.add(worker);
367 toremovannot.add(worker);
370 canUpdate.removeAll(removable);
372 // TODO: finish testing this extension
375 * synchronized (inProgress) { // need to kill or mark as dead any running
376 * threads... (inProgress.get(typeToRemove)); }
378 * if (workers == null) { return; } for (AlignCalcWorkerI worker : workers)
381 * if (isPending(worker)) { worker.abortAndDestroy(); startWorker(worker); }
382 * else { System.err.println("Pending exists for " + workerClass); } }