diff options
Diffstat (limited to 'subprojects/language/src/main/java/tools/refinery/language/naming/NamingUtil.java')
-rw-r--r-- | subprojects/language/src/main/java/tools/refinery/language/naming/NamingUtil.java | 33 |
1 files changed, 28 insertions, 5 deletions
diff --git a/subprojects/language/src/main/java/tools/refinery/language/naming/NamingUtil.java b/subprojects/language/src/main/java/tools/refinery/language/naming/NamingUtil.java index 1647d4e7..feae5ebb 100644 --- a/subprojects/language/src/main/java/tools/refinery/language/naming/NamingUtil.java +++ b/subprojects/language/src/main/java/tools/refinery/language/naming/NamingUtil.java | |||
@@ -1,21 +1,24 @@ | |||
1 | /* | 1 | /* |
2 | * SPDX-FileCopyrightText: 2021-2023 The Refinery Authors <https://refinery.tools/> | 2 | * SPDX-FileCopyrightText: 2021-2024 The Refinery Authors <https://refinery.tools/> |
3 | * | 3 | * |
4 | * SPDX-License-Identifier: EPL-2.0 | 4 | * SPDX-License-Identifier: EPL-2.0 |
5 | */ | 5 | */ |
6 | package tools.refinery.language.naming; | 6 | package tools.refinery.language.naming; |
7 | 7 | ||
8 | import org.eclipse.xtext.naming.QualifiedName; | ||
9 | |||
8 | import java.util.regex.Pattern; | 10 | import java.util.regex.Pattern; |
9 | 11 | ||
10 | public final class NamingUtil { | 12 | public final class NamingUtil { |
11 | private static final String SINGLETON_VARIABLE_PREFIX = "_"; | 13 | private static final String SINGLETON_VARIABLE_PREFIX = "_"; |
12 | 14 | public static final QualifiedName ROOT_NAME = QualifiedName.create(""); | |
13 | private static final Pattern ID_REGEX = Pattern.compile("[_a-zA-Z][_0-9a-zA-Z]*"); | 15 | |
16 | private static final Pattern ID_REGEX = Pattern.compile("[_a-zA-Z]\\w*"); | ||
14 | 17 | ||
15 | private NamingUtil() { | 18 | private NamingUtil() { |
16 | throw new IllegalStateException("This is a static utility class and should not be instantiated directly"); | 19 | throw new IllegalStateException("This is a static utility class and should not be instantiated directly"); |
17 | } | 20 | } |
18 | 21 | ||
19 | public static boolean isNullOrEmpty(String name) { | 22 | public static boolean isNullOrEmpty(String name) { |
20 | return name == null || name.isEmpty(); | 23 | return name == null || name.isEmpty(); |
21 | } | 24 | } |
@@ -23,8 +26,28 @@ public final class NamingUtil { | |||
23 | public static boolean isSingletonVariableName(String name) { | 26 | public static boolean isSingletonVariableName(String name) { |
24 | return name != null && name.startsWith(SINGLETON_VARIABLE_PREFIX); | 27 | return name != null && name.startsWith(SINGLETON_VARIABLE_PREFIX); |
25 | } | 28 | } |
26 | 29 | ||
30 | // This method name only makes sense if it checks for the positive case. | ||
31 | @SuppressWarnings("BooleanMethodIsAlwaysInverted") | ||
27 | public static boolean isValidId(String name) { | 32 | public static boolean isValidId(String name) { |
28 | return name != null && ID_REGEX.matcher(name).matches(); | 33 | return name != null && ID_REGEX.matcher(name).matches(); |
29 | } | 34 | } |
35 | |||
36 | public static boolean isFullyQualified(QualifiedName name) { | ||
37 | return name.startsWith(ROOT_NAME); | ||
38 | } | ||
39 | |||
40 | public static QualifiedName stripRootPrefix(QualifiedName name) { | ||
41 | if (name == null) { | ||
42 | return null; | ||
43 | } | ||
44 | return isFullyQualified(name) ? name.skipFirst(ROOT_NAME.getSegmentCount()) : name; | ||
45 | } | ||
46 | |||
47 | public static QualifiedName addRootPrefix(QualifiedName name) { | ||
48 | if (name == null) { | ||
49 | return null; | ||
50 | } | ||
51 | return isFullyQualified(name) ? name : ROOT_NAME.append(name); | ||
52 | } | ||
30 | } | 53 | } |