Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 724ca183b961f06ffa789fd59b6ac03688eba5bb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*******************************************************************************
 * Copyright (c) 2006, 2013 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.compare.internal;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.ProgressMonitorWrapper;
import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.jface.operation.IRunnableWithProgress;

/**
 * A worker performs a set of tasks in order and accumulates any errors
 * that may have occurred. If the same task is queued multiple times,
 * the last occurrence will be run. If a task is queued while it is
 * running, the running task will be canceled and the task added
 * to the end of the work queue.
 */
public class Worker implements IRunnableWithProgress {
	private final WorkQueue work = new WorkQueue();
	private boolean isWorking;
	private final List<Throwable> errors = new ArrayList<>();
	private WorkProgressMonitor currentMonitor;
	private IRunnableWithProgress currentTask;
	private final String taskName;
	
	/**
	 * Progress monitor that supports local cancellation of a task.
	 */
	private static class WorkProgressMonitor extends ProgressMonitorWrapper {
		private boolean localCancel;

		protected WorkProgressMonitor(IProgressMonitor monitor) {
			super(monitor);
		}

		public void cancelTask() {
			localCancel = true;
		}

		@Override
		public boolean isCanceled() {
			return localCancel || super.isCanceled();
		}
	}
	
	public Worker(String taskName) {
		this.taskName = taskName;
	}
	
	@Override
	public void run(IProgressMonitor monitor) {
		errors.clear();
		SubMonitor pm = SubMonitor.convert(monitor, getTaskName(), 100);
		try {
			isWorking = true;
			while (!work.isEmpty()) {
				try {
					performNextTask(pm);
					checkCancelled(pm);
				} catch (InterruptedException e) {
					// Only cancel all the work if the outer monitor is canceled
					checkCancelled(pm);
				} catch (InvocationTargetException e) {
					handleError(e.getTargetException());
				}
				pm.setWorkRemaining(100);
			}
			pm.done();
		} catch (OperationCanceledException e) {
			// The user chose to cancel
			work.clear();
		} finally {
			isWorking = false;
			currentMonitor = null;
			currentTask = null;
		}
	}

	private WorkProgressMonitor subMonitorFor(SubMonitor pm, int ticks) {
		return new WorkProgressMonitor(pm.newChild(ticks));
	}

	private void handleError(Throwable targetException) {
		errors.add(targetException);
	}
	
	public Throwable[] getErrors() {
		return errors.toArray(new Throwable[errors.size()]);
	}

	private void checkCancelled(SubMonitor pm) {
		if (pm.isCanceled())
			throw new OperationCanceledException();
	}

	protected String getTaskName() {
		return taskName;
	}

	private void performNextTask(SubMonitor pm) throws InvocationTargetException, InterruptedException {
		synchronized (this) {
			if (work.isEmpty())
				return;
			currentTask = work.remove();
			currentMonitor= subMonitorFor(pm, 10);
		}
		currentTask.run(currentMonitor);
	}

	public synchronized void add(IRunnableWithProgress r) {
		if (currentTask != null && currentTask.equals(r)) {
			currentMonitor.cancelTask();
		}
		work.add(r);
	}

	public boolean isWorking() {
		return isWorking;
	}

	public boolean hasWork() {
		return isWorking() || !work.isEmpty();
	}
}

Back to the top