Skip to content

Commit f502e9d

Browse files
authored
Use a precompiled pattern in ChannelUID.validateSegment (#4327)
Signed-off-by: Jörg Sautter <[email protected]>
1 parent 8557666 commit f502e9d

File tree

1 file changed

+3
-2
lines changed
  • bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing

1 file changed

+3
-2
lines changed

bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/ChannelUID.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import java.util.ArrayList;
1616
import java.util.List;
17+
import java.util.regex.Pattern;
1718

1819
import org.eclipse.jdt.annotation.NonNullByDefault;
1920
import org.eclipse.jdt.annotation.Nullable;
@@ -30,7 +31,7 @@
3031
@NonNullByDefault
3132
public class ChannelUID extends UID {
3233

33-
public static final String CHANNEL_SEGMENT_PATTERN = "[\\w-]*|[\\w-]*#[\\w-]*";
34+
public static final Pattern CHANNEL_SEGMENT_PATTERN = Pattern.compile("[\\w-]*(?:#[\\w-]*)?");
3435
public static final String CHANNEL_GROUP_SEPARATOR = "#";
3536

3637
/**
@@ -132,7 +133,7 @@ protected void validateSegment(String segment, int index, int length) {
132133
if (index < length - 1) {
133134
super.validateSegment(segment, index, length);
134135
} else {
135-
if (!segment.matches(CHANNEL_SEGMENT_PATTERN)) {
136+
if (!CHANNEL_SEGMENT_PATTERN.matcher(segment).matches()) {
136137
throw new IllegalArgumentException(String.format(
137138
"UID segment '%s' contains invalid characters. The last segment of the channel UID must match the pattern '%s'.",
138139
segment, CHANNEL_SEGMENT_PATTERN));

0 commit comments

Comments
 (0)