Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 6c38f53e55c1a2af94050d8dea799618f6b2287b (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*****************************************************************************
 * Copyright (c) 2018 Christian W. Damus 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:
 *   Christian W. Damus - Initial API and implementation
 *   
 *****************************************************************************/

package org.eclipse.papyrus.uml.diagram.sequence.util;

import java.util.function.BooleanSupplier;

import org.eclipse.papyrus.uml.diagram.sequence.part.UMLDiagramEditorPlugin;
import org.eclipse.swt.widgets.Display;

/**
 * A deferred action that posts itself asynchronously on the {@link Display} thread
 * and re-tries some limited number of times if its initial conditions are not met.
 * 
 * @since 5.0
 */
public abstract class RetryingDeferredAction {
	private static final int DEFAULT_RETRY_LIMIT = 3;

	private final Display display;
	private final int retryLimit;
	private volatile int retries;

	/**
	 * Initializes me.
	 * 
	 * @param display
	 *            the display on which I post myself for delayed execution
	 * @param retryLimit
	 *            the number of times I may retry
	 * 
	 * @throws IllegalArgumentException
	 *             if the retry limit is non-positive
	 */
	public RetryingDeferredAction(Display display, int retryLimit) {
		super();

		if (retryLimit <= 0) {
			throw new IllegalArgumentException("retry limit must be positive"); //$NON-NLS-1$
		}

		this.display = display;
		this.retryLimit = retryLimit;
	}

	/**
	 * Initializes me with the default number (three) of retries.
	 * 
	 * @param display
	 *            the display on which I post myself for delayed execution
	 */
	public RetryingDeferredAction(Display display) {
		this(display, DEFAULT_RETRY_LIMIT);
	}

	/**
	 * Initializes me with the current display.
	 * 
	 * @param retryLimit
	 *            the number of times I may retry
	 * 
	 * @throws IllegalArgumentException
	 *             if the retry limit is non-positive
	 */
	public RetryingDeferredAction(int retryLimit) {
		this(Display.getCurrent(), retryLimit);
	}

	/**
	 * Initializes me with the current display and the default number (three) of retries.
	 */
	public RetryingDeferredAction() {
		this(Display.getCurrent(), DEFAULT_RETRY_LIMIT);
	}

	/**
	 * Try an {@code action} up to the given number of times, deferred on the {@code display} thread.
	 * This is useful for the simple case where it is only necessary to attempt to perform the
	 * action and there is no need for an explicit preparation step.
	 * 
	 * @param display
	 *            the display on which thread to defer the {@code action}
	 * @param retryLimit
	 *            the maximal number of times to tr-try the {@code action}
	 * @param action
	 *            the action to perform. If it returns {@code false}, then it will
	 *            be re-tried (unless the limit is exceeded, of course)
	 * 
	 * @throws IllegalArgumentException
	 *             if the retry limit is non-positive
	 */
	public static void defer(Display display, int retryLimit, BooleanSupplier action) {
		new Wrapper(display, retryLimit, action).post();
	}

	/**
	 * Try an {@code action} up to the default number (three) of times, deferred on the {@code display} thread.
	 * 
	 * @param display
	 *            the display on which thread to defer the {@code action}
	 * @param action
	 *            the action to perform
	 */
	public static void defer(Display display, BooleanSupplier action) {
		defer(display, DEFAULT_RETRY_LIMIT, action);
	}

	/**
	 * Try an {@code action} up to the given number of times, deferred on the current display thread.
	 * 
	 * @param retryLimit
	 *            the maximal number of times to tr-try the {@code action}
	 * @param action
	 *            the action to perform
	 * 
	 * @throws IllegalArgumentException
	 *             if the retry limit is non-positive
	 */
	public static void defer(int retryLimit, BooleanSupplier action) {
		defer(Display.getCurrent(), retryLimit, action);
	}

	/**
	 * Try an {@code action} up to the default number (three) of times, deferred on the current display thread.
	 * 
	 * @param action
	 *            the action to perform
	 */
	public static void defer(BooleanSupplier action) {
		defer(Display.getCurrent(), DEFAULT_RETRY_LIMIT, action);
	}

	/**
	 * Prepares me for execution, testing my initial conditions and setting up any
	 * required state.
	 * 
	 * @return {@code true} if my initial conditions are satisfied and I may {@link #perform()};
	 *         {@code false}, otherwise
	 */
	protected abstract boolean prepare();

	/**
	 * Performs me. Will not be called unless {@link #prepare()} returned {@code true}.
	 */
	protected abstract void perform();

	private void run() {
		if (prepare()) {
			perform();
		} else {
			retries = retries + 1;
			post();
		}
	}

	/**
	 * Post me for deferred execution.
	 */
	public void post() {
		if (retries < retryLimit) {
			display.asyncExec(this::run);
		} else {
			UMLDiagramEditorPlugin.log.warn("Retry limit exceeded for " + this); //$NON-NLS-1$
		}
	}

	//
	// Nested types
	//

	private static final class Wrapper extends RetryingDeferredAction {
		private final BooleanSupplier action;

		Wrapper(Display display, int retryLimit, BooleanSupplier action) {
			super(display, retryLimit);

			this.action = action;
		}

		@Override
		protected boolean prepare() {
			return action.getAsBoolean();
		}

		@Override
		protected void perform() {
			// Already done in the preparation step
		}
	}
}

Back to the top