Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 1125c9c943500de7eb26f14222c9fcdb944b8744 (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
/*******************************************************************************
 * Copyright (c) 2014 protos software gmbh (http://www.protos.de).
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 * 
 * CONTRIBUTORS:
 * 		hrentz (initial contribution)
 * 
 *******************************************************************************/
package org.eclipse.etrice.core.common.validation

import java.util.List
import java.util.Set
import org.eclipse.emf.ecore.EObject
import org.eclipse.emf.ecore.EStructuralFeature
import java.util.ArrayList
import org.eclipse.emf.ecore.resource.Resource
import com.google.common.base.Function

/**
 * @author hrentz
 *
 */
class ValidationHelpers {

	public static class NamedObject {
		@Property String name
		@Property EObject obj
		@Property EStructuralFeature feature

		new(String name, EObject obj, EStructuralFeature feature) {
			this.name = name
			this.obj = obj
			this.feature = feature
		}
	}

	public static class NamedObjectList extends ArrayList<NamedObject> {
		def addAll(List<? extends EObject> items, EStructuralFeature feature) {
			items.forEach[this.add(new NamedObject(it.eGet(feature) as String, it, feature))]
		}
	}

	def static Iterable<NamedObject> removeUniques(List<NamedObject> items) {
		val Set<String> allOfName = newHashSet
		val Set<String> duplicateNames = newHashSet

		// first sweep: determine duplicate names
		for (i : items) {

			// if the name already occurred we have a duplicate name and hence an error
			if (allOfName.contains(i.name)) {
				duplicateNames.add(i.name)
			}
			else {
				allOfName.add(i.name);
			}
		}

		// second sweep: remove duplicates
		items.filter(i|duplicateNames.contains(i.name))
	}
	
	def static Iterable<NamedObject> inSameResource(Iterable<NamedObject> items, Resource resource) {
		items.filter(i|i.obj.eResource==resource)
	}
	
	/**
	 * Visitor for inheritance, safeguards from null, eProxy and circular issues.
	 * 
	 * @param start EObject, may be null
	 * @param function return null to exit
	 */
	def static <E extends EObject> saveRecursiveVisitor(E  start, Function<E , E> function){
		val Set<E> visited = newHashSet
		var E next = start
		while(next !== null && !next.eIsProxy && (visited += next))
			next = function.apply(next)
	}
}

Back to the top