1 /*******************************************************************************
2  * Copyright (c) 2004, 2015 IBM Corporation and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.jface.viewers.deferred;
15 
16 import org.eclipse.core.runtime.IProgressMonitor;
17 
18 /**
19  * A more efficient alternative to an IProgressMonitor. In particular, the
20  * implementation is designed to make isCanceled() run as efficiently as
21  * possible. Currently package-visible because the implementation is incomplete.
22  *
23  * @since 3.1
24  *
25  * @deprecated use SubMonitor instead
26  *
27  *             TODO mark for deletion
28  *
29  */
30 @Deprecated
31 final class FastProgressReporter {
32 	private IProgressMonitor monitor;
33 	private volatile boolean canceled = false;
34 	private int cancelCheck = 0;
35 
36 	private static int CANCEL_CHECK_PERIOD = 40;
37 
38 	/**
39 	 * Constructs a null FastProgressReporter
40 	 */
FastProgressReporter()41 	public FastProgressReporter() {
42 	}
43 
44 	/**
45 	 * Constructs a FastProgressReporter that wraps the given progress monitor
46 	 *
47 	 * @param monitor the monitor to wrap
48 	 * @param totalProgress the total progress to be reported
49 	 */
FastProgressReporter(IProgressMonitor monitor, int totalProgress)50 	public FastProgressReporter(IProgressMonitor monitor, int totalProgress) {
51 		this.monitor = monitor;
52 		canceled = monitor.isCanceled();
53 	}
54 	/**
55 	 * Return whether the progress monitor has been canceled.
56 	 *
57 	 * @return <code>true</code> if the monitor has been cancelled, <code>false</code> otherwise.
58 	 */
isCanceled()59 	public boolean isCanceled() {
60 		if (monitor == null) {
61 			return canceled;
62 		}
63 
64 		cancelCheck++;
65 		if (cancelCheck > CANCEL_CHECK_PERIOD) {
66 			canceled = monitor.isCanceled();
67 			cancelCheck = 0;
68 		}
69 		return canceled;
70 	}
71 
72 	/**
73 	 * Cancel the progress monitor.
74 	 */
cancel()75 	public void cancel() {
76 		canceled = true;
77 
78 		if (monitor == null) {
79 			return;
80 		}
81 		monitor.setCanceled(true);
82 	}
83 }
84