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.synchronizedList(
72 new ArrayList<Class<? extends AlignCalcWorkerI>>());
73 inProgress = Collections
74 .synchronizedList(new ArrayList<AlignCalcWorkerI>());
75 updating = Collections.synchronizedMap(
76 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());
88 updating.put(worker.getClass(), upd = Collections
89 .synchronizedList(new ArrayList<AlignCalcWorkerI>()));
101 * @see jalview.api.AlignCalcManagerI#isPending(jalview.api.AlignCalcWorkerI)
104 public boolean isPending(AlignCalcWorkerI workingClass)
106 List<AlignCalcWorkerI> upd;
107 synchronized (updating)
109 upd = updating.get(workingClass.getClass());
126 public boolean notifyWorking(AlignCalcWorkerI worker)
128 synchronized (inProgress)
130 if (inProgress.contains(worker))
132 return false; // worker is already working, so ask caller to wait around
136 inProgress.add(worker);
143 public void workerComplete(AlignCalcWorkerI worker)
145 synchronized (inProgress)
147 // System.err.println("Worker " + worker + " marked as complete.");
148 inProgress.remove(worker);
149 List<AlignCalcWorkerI> upd = updating.get(worker.getClass());
156 canUpdate.add(worker);
162 public void disableWorker(AlignCalcWorkerI worker)
164 synchronized (blackList)
166 blackList.add(worker.getClass());
171 public boolean isDisabled(AlignCalcWorkerI worker)
173 synchronized (blackList)
175 return blackList.contains(worker.getClass());
180 public void startWorker(AlignCalcWorkerI worker)
182 if (!isDisabled(worker))
184 Thread tw = new Thread(worker);
185 tw.setName(worker.getClass().toString());
191 public boolean isWorking(AlignCalcWorkerI worker)
193 synchronized (inProgress)
194 {// System.err.println("isWorking : worker "+(worker!=null ?
195 // worker.getClass():"null")+ " "+hashCode());
196 return worker != null && inProgress.contains(worker);
201 public boolean isWorking()
203 synchronized (inProgress)
205 // System.err.println("isWorking "+hashCode());
206 return inProgress.size() > 0;
211 public void registerWorker(AlignCalcWorkerI worker)
213 synchronized (restartable)
215 if (!restartable.contains(worker))
217 restartable.add(worker);
224 public void restartWorkers()
226 synchronized (restartable)
228 for (AlignCalcWorkerI worker : restartable)
236 public boolean workingInvolvedWith(
237 AlignmentAnnotation alignmentAnnotation)
239 synchronized (inProgress)
241 for (AlignCalcWorkerI worker : inProgress)
243 if (worker.involves(alignmentAnnotation))
249 synchronized (updating)
251 for (List<AlignCalcWorkerI> workers : updating.values())
253 for (AlignCalcWorkerI worker : workers)
255 if (worker.involves(alignmentAnnotation))
266 public void updateAnnotationFor(
267 Class<? extends AlignCalcWorkerI> workerClass)
270 AlignCalcWorkerI[] workers;
271 synchronized (canUpdate)
273 workers = canUpdate.toArray(new AlignCalcWorkerI[0]);
275 for (AlignCalcWorkerI worker : workers)
277 if (workerClass.equals(worker.getClass()))
279 worker.updateAnnotation();
285 public List<AlignCalcWorkerI> getRegisteredWorkersOfClass(
286 Class<? extends AlignCalcWorkerI> workerClass)
288 List<AlignCalcWorkerI> workingClass = new ArrayList<AlignCalcWorkerI>();
289 synchronized (canUpdate)
291 for (AlignCalcWorkerI worker : canUpdate)
293 if (workerClass.equals(worker.getClass()))
295 workingClass.add(worker);
299 return (workingClass.size() == 0) ? null : workingClass;
303 public void enableWorker(AlignCalcWorkerI worker)
305 synchronized (blackList)
307 blackList.remove(worker.getClass());
312 public void removeRegisteredWorkersOfClass(
313 Class<? extends AlignCalcWorkerI> typeToRemove)
315 List<AlignCalcWorkerI> removable = new ArrayList<AlignCalcWorkerI>();
316 Set<AlignCalcWorkerI> toremovannot = new HashSet<AlignCalcWorkerI>();
317 synchronized (restartable)
319 for (AlignCalcWorkerI worker : restartable)
321 if (typeToRemove.equals(worker.getClass()))
323 removable.add(worker);
324 toremovannot.add(worker);
327 restartable.removeAll(removable);
329 synchronized (canUpdate)
331 for (AlignCalcWorkerI worker : canUpdate)
333 if (typeToRemove.equals(worker.getClass()))
335 removable.add(worker);
336 toremovannot.add(worker);
339 canUpdate.removeAll(removable);
341 // TODO: finish testing this extension
344 * synchronized (inProgress) { // need to kill or mark as dead any running
345 * threads... (inProgress.get(typeToRemove)); }
347 * if (workers == null) { return; } for (AlignCalcWorkerI worker : workers)
350 * if (isPending(worker)) { worker.abortAndDestroy(); startWorker(worker); }
351 * else { System.err.println("Pending exists for " + workerClass); } }
356 * Deletes the worker that update the given annotation, provided it is marked
360 public void removeWorkerForAnnotation(AlignmentAnnotation ann)
363 * first just find those to remove (to avoid
364 * ConcurrentModificationException)
366 List<AlignCalcWorkerI> toRemove = new ArrayList<AlignCalcWorkerI>();
367 for (AlignCalcWorkerI worker : restartable)
369 if (worker.involves(ann))
371 if (worker.isDeletable())
373 toRemove.add(worker);
379 * remove all references to deleted workers so any references
380 * they hold to annotation data can be garbage collected
382 for (AlignCalcWorkerI worker : toRemove)
384 restartable.remove(worker);
385 blackList.remove(worker.getClass());
386 inProgress.remove(worker);
387 canUpdate.remove(worker);
388 synchronized (updating)
390 List<AlignCalcWorkerI> upd = updating.get(worker.getClass());