Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 5a9da663c308919af091777225768560a15c5fe6 (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
/*****************************************************************************
 * Copyright (c) 2015 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.infra.sync.service;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.papyrus.infra.sync.Activator;
import org.eclipse.papyrus.infra.sync.internal.SyncService;

import com.google.common.base.Function;

/**
 * A sync action that cascades the evaluation of triggers to related objects.
 */
public class CascadeTriggers implements ISyncAction {

	private final ISyncService service = SyncService.getCurrent();

	private Function<Object, ? extends Iterable<?>> cascadeFunction;

	public CascadeTriggers() {
		this(null);
	}

	/**
	 * Initializes me with a cascade function.
	 */
	public CascadeTriggers(Function<Object, ? extends Iterable<?>> cascadeFunction) {
		super();

		this.cascadeFunction = cascadeFunction;
	}

	/**
	 * Assigns a function that I use to compute the objects to which to cascade the
	 * evaluation of synchronization triggers.
	 */
	public void setCascadeFunction(Function<Object, ? extends Iterable<?>> cascadeFunction) {
		this.cascadeFunction = cascadeFunction;
	}

	/**
	 * Evaluates sync triggers on the objects related to the given triggered {@code object} by my
	 * {@linkplain #setCascadeFunction(Function) cascade function}.
	 */
	@Override
	public IStatus perform(ISyncService syncService, Object object) {
		IStatus result = Status.OK_STATUS;

		for (Object next : cascade(object)) {
			IStatus nextResult = service.evaluateTriggers(next);
			if ((nextResult != null) && !nextResult.isOK()) {
				if (result.isOK()) {
					result = nextResult;
				} else if (result.isMultiStatus()) {
					((MultiStatus) result).merge(nextResult);
				} else {
					result = new MultiStatus(Activator.PLUGIN_ID, 0, new IStatus[] { result, nextResult }, "Multiple sync trigger cascade problems occurred.", null);
				}
			}
		}

		return result;
	}

	/**
	 * Obtains the objects on which to evaluate cascaded sync triggers, based on the given {@code triggered} object.
	 * 
	 * @throws IllegalStateException
	 *             if I have no {@linkplain #setCascadeFunction(Function) cascade function} with which
	 *             to compute the cascaded triggers
	 */
	protected Iterable<?> cascade(Object triggered) {
		if (cascadeFunction == null) {
			throw new IllegalStateException("no cascade function"); //$NON-NLS-1$
		}

		return cascadeFunction.apply(triggered);
	}
}

Back to the top