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
38 * list of registered workers
40 private volatile List<AlignCalcWorkerI> restartable;
43 * types of worker _not_ to run (for example, because they have
44 * previously thrown errors)
46 private volatile List<Class<? extends AlignCalcWorkerI>> blackList;
49 * global record of calculations in progress
51 private volatile List<AlignCalcWorkerI> inProgress;
54 * record of calculations pending or in progress in the current context
56 private volatile Map<Class<? extends AlignCalcWorkerI>, List<AlignCalcWorkerI>> updating;
59 * workers that have run to completion so are candidates for visual-only
60 * update of their results
62 private HashSet<AlignCalcWorkerI> canUpdate;
67 public AlignCalcManager()
69 restartable = Collections
70 .synchronizedList(new ArrayList<AlignCalcWorkerI>());
71 blackList = Collections
72 .synchronizedList(new ArrayList<Class<? extends AlignCalcWorkerI>>());
73 inProgress = Collections
74 .synchronizedList(new ArrayList<AlignCalcWorkerI>());
75 updating = Collections
76 .synchronizedMap(new Hashtable<Class<? extends AlignCalcWorkerI>, List<AlignCalcWorkerI>>());
77 canUpdate = new HashSet<AlignCalcWorkerI>();
81 public void notifyStart(AlignCalcWorkerI worker)
83 synchronized (updating)
85 List<AlignCalcWorkerI> upd = updating.get(worker.getClass());
91 .synchronizedList(new ArrayList<AlignCalcWorkerI>()));
103 * @see jalview.api.AlignCalcManagerI#isPending(jalview.api.AlignCalcWorkerI)
106 public boolean isPending(AlignCalcWorkerI workingClass)
108 List<AlignCalcWorkerI> upd;
109 synchronized (updating)
111 upd = updating.get(workingClass.getClass());
128 public boolean notifyWorking(AlignCalcWorkerI worker)
130 synchronized (inProgress)
132 if (inProgress.contains(worker))
134 return false; // worker is already working, so ask caller to wait around
138 inProgress.add(worker);
145 public void workerComplete(AlignCalcWorkerI worker)
147 synchronized (inProgress)
149 // System.err.println("Worker " + worker + " marked as complete.");
150 inProgress.remove(worker);
151 List<AlignCalcWorkerI> upd = updating.get(worker.getClass());
158 canUpdate.add(worker);
164 public void disableWorker(AlignCalcWorkerI worker)
166 synchronized (blackList)
168 blackList.add(worker.getClass());
173 public boolean isDisabled(AlignCalcWorkerI worker)
175 synchronized (blackList)
177 return blackList.contains(worker.getClass());
182 public void startWorker(AlignCalcWorkerI worker)
184 if (!isDisabled(worker))
186 Thread tw = new Thread(worker);
187 tw.setName(worker.getClass().toString());
193 public boolean isWorking(AlignCalcWorkerI worker)
195 synchronized (inProgress)
196 {// System.err.println("isWorking : worker "+(worker!=null ?
197 // worker.getClass():"null")+ " "+hashCode());
198 return worker != null && inProgress.contains(worker);
203 public boolean isWorking()
205 synchronized (inProgress)
207 // System.err.println("isWorking "+hashCode());
208 return inProgress.size() > 0;
213 public void registerWorker(AlignCalcWorkerI worker)
215 synchronized (restartable)
217 if (!restartable.contains(worker))
219 restartable.add(worker);
226 public void restartWorkers()
228 synchronized (restartable)
230 for (AlignCalcWorkerI worker : restartable)
238 public boolean workingInvolvedWith(AlignmentAnnotation alignmentAnnotation)
240 synchronized (inProgress)
242 for (AlignCalcWorkerI worker : inProgress)
244 if (worker.involves(alignmentAnnotation))
250 synchronized (updating)
252 for (List<AlignCalcWorkerI> workers : updating.values())
254 for (AlignCalcWorkerI worker : workers)
256 if (worker.involves(alignmentAnnotation))
267 public void updateAnnotationFor(
268 Class<? extends AlignCalcWorkerI> workerClass)
271 AlignCalcWorkerI[] workers;
272 synchronized (canUpdate)
274 workers = canUpdate.toArray(new AlignCalcWorkerI[0]);
276 for (AlignCalcWorkerI worker : workers)
278 if (workerClass.equals(worker.getClass()))
280 worker.updateAnnotation();
286 public List<AlignCalcWorkerI> getRegisteredWorkersOfClass(
287 Class<? extends AlignCalcWorkerI> workerClass)
289 List<AlignCalcWorkerI> workingClass = new ArrayList<AlignCalcWorkerI>();
290 synchronized (canUpdate)
292 for (AlignCalcWorkerI worker : canUpdate)
294 if (workerClass.equals(worker.getClass()))
296 workingClass.add(worker);
300 return (workingClass.size() == 0) ? null : workingClass;
304 public void enableWorker(AlignCalcWorkerI worker)
306 synchronized (blackList)
308 blackList.remove(worker.getClass());
313 public void removeRegisteredWorkersOfClass(
314 Class<? extends AlignCalcWorkerI> typeToRemove)
316 List<AlignCalcWorkerI> removable = new ArrayList<AlignCalcWorkerI>();
317 Set<AlignCalcWorkerI> toremovannot = new HashSet<AlignCalcWorkerI>();
318 synchronized (restartable)
320 for (AlignCalcWorkerI worker : restartable)
322 if (typeToRemove.equals(worker.getClass()))
324 removable.add(worker);
325 toremovannot.add(worker);
328 restartable.removeAll(removable);
330 synchronized (canUpdate)
332 for (AlignCalcWorkerI worker : canUpdate)
334 if (typeToRemove.equals(worker.getClass()))
336 removable.add(worker);
337 toremovannot.add(worker);
340 canUpdate.removeAll(removable);
342 // TODO: finish testing this extension
345 * synchronized (inProgress) { // need to kill or mark as dead any running
346 * threads... (inProgress.get(typeToRemove)); }
348 * if (workers == null) { return; } for (AlignCalcWorkerI worker : workers)
351 * if (isPending(worker)) { worker.abortAndDestroy(); startWorker(worker); }
352 * else { System.err.println("Pending exists for " + workerClass); } }
357 * Deletes the worker that update the given annotation, provided it is marked
361 public void removeWorkerForAnnotation(AlignmentAnnotation ann)
364 * first just find those to remove (to avoid
365 * ConcurrentModificationException)
367 List<AlignCalcWorkerI> toRemove = new ArrayList<AlignCalcWorkerI>();
368 for (AlignCalcWorkerI worker : restartable)
370 if (worker.involves(ann))
372 if (worker.isDeletable())
374 toRemove.add(worker);
380 * remove all references to deleted workers so any references
381 * they hold to annotation data can be garbage collected
383 for (AlignCalcWorkerI worker : toRemove)
385 restartable.remove(worker);
386 blackList.remove(worker.getClass());
387 inProgress.remove(worker);
388 canUpdate.remove(worker);
389 synchronized (updating)
391 List<AlignCalcWorkerI> upd = updating.get(worker.getClass());