-
Notifications
You must be signed in to change notification settings - Fork 45
/
DomainClassPredicate.java
81 lines (68 loc) · 2.79 KB
/
DomainClassPredicate.java
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
/*******************************************************************************
* Copyright (c) 2019, 2024 Obeo.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.sirius.components.emf;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.function.Predicate;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.eclipse.emf.ecore.EClass;
/**
* Evaluate if a given EClass matches a domain class descriptor (as a string).
*
* @author sbegaudeau
*/
public class DomainClassPredicate implements Predicate<EClass> {
/**
* The pattern used to match the separator used by both Sirius and AQL.
*/
private static final Pattern SEPARATOR = Pattern.compile("(::?|\\.)");
private String domainClass;
public DomainClassPredicate(String domainClass) {
this.domainClass = Objects.requireNonNull(domainClass);
}
@Override
public boolean test(EClass actualType) {
String packageName = null;
String className = null;
if (!this.domainClass.isBlank()) {
Matcher matcher = SEPARATOR.matcher(this.domainClass);
if (matcher.find()) {
packageName = this.domainClass.substring(0, matcher.start());
className = this.domainClass.substring(matcher.end());
} else {
className = this.domainClass;
}
if (!("EObject".equals(className) && packageName == null) && !("EObject".equals(className) && "ecore".equals(packageName))) {
boolean result = false;
List<EClass> eAllTypes = new ArrayList<>();
eAllTypes.add(actualType);
eAllTypes.addAll(actualType.getEAllSuperTypes());
Iterator<EClass> iterator = eAllTypes.iterator();
while (iterator.hasNext() && !result) {
EClass eClass = iterator.next();
if (packageName == null && className != null) {
// Only consider the class name
result = className.equals(eClass.getName());
} else if (packageName != null && className != null) {
result = packageName.equals(eClass.getEPackage().getName()) && className.equals(eClass.getName());
}
}
return result;
}
}
return true;
}
}