Skip to content
This repository has been archived by the owner on May 7, 2020. It is now read-only.

[core] ChannelGroupUID without '#' #6157

Merged
merged 2 commits into from
Sep 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,13 @@ public void testInvalidCharacters() {

@Test(expected = IllegalArgumentException.class)
public void testNotEnoughNumberOfSegments() {
new ChannelUID("binding:thing-type:group#");
}

@Test(expected = IllegalArgumentException.class)
public void testMissingChannelGroupSeparator() {
new ChannelGroupUID("binding:thing-type:thing:group");
}

@Test(expected = IllegalArgumentException.class)
public void testMultipleChannelGroupSeparators() {
new ChannelGroupUID("binding:thing-type:thing:group#id#what_ever");
new ChannelUID("binding:thing-type:group");
}

@Test
public void testChannelGroupUID() {
ChannelGroupUID channelGroupUID = new ChannelGroupUID(THING_UID, GROUP_ID);
assertEquals("binding:thing-type:thing:group#", channelGroupUID.toString());
assertEquals("binding:thing-type:thing:group", channelGroupUID.toString());
assertEquals(GROUP_ID, channelGroupUID.getId());
assertEquals(THING_UID, channelGroupUID.getThingUID());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@
@NonNullByDefault
public class ChannelGroupUID extends UID {

private static final String CHANNEL_GROUP_SEGMENT_PATTERN = "[\\w-]*#";
private static final String CHANNEL_GROUP_SEPERATOR = "#";

/**
* Default constructor in package scope only. Will allow to instantiate this
* class by reflection. Not intended to be used for normal instantiation.
Expand All @@ -50,7 +47,7 @@ public ChannelGroupUID(ThingUID thingUID, String id) {

private static List<String> toSegments(ThingUID thingUID, String id) {
List<String> ret = new ArrayList<>(thingUID.getAllSegments());
ret.add(id + CHANNEL_GROUP_SEPERATOR);
ret.add(id);
return ret;
}

Expand All @@ -61,27 +58,14 @@ private static List<String> toSegments(ThingUID thingUID, String id) {
*/
public String getId() {
List<String> segments = getAllSegments();
return segments.get(segments.size() - 1).replaceAll(CHANNEL_GROUP_SEPERATOR, "");
return segments.get(segments.size() - 1);
}

@Override
protected int getMinimalNumberOfSegments() {
return 4;
}

@Override
protected void validateSegment(String segment, int index, int length) {
if (index < length - 1) {
super.validateSegment(segment, index, length);
} else {
if (!segment.matches(CHANNEL_GROUP_SEGMENT_PATTERN)) {
throw new IllegalArgumentException(String.format(
"UID segment '%s' contains invalid characters. The last segment of the channel UID must match the pattern '%s'.",
segment, CHANNEL_GROUP_SEGMENT_PATTERN));
}
}
}

/**
* Returns the thing UID
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
public class ChannelUID extends UID {

private static final String CHANNEL_SEGMENT_PATTERN = "[\\w-]*|[\\w-]*#[\\w-]*";
private static final String CHANNEL_GROUP_SEPERATOR = "#";
private static final String CHANNEL_GROUP_SEPARATOR = "#";

/**
* Default constructor in package scope only. Will allow to instantiate this
Expand Down Expand Up @@ -120,7 +120,7 @@ private static List<String> toSegments(ThingUID thingUID, @Nullable String group
}

private static String getChannelId(@Nullable String groupId, String id) {
return groupId != null ? groupId + CHANNEL_GROUP_SEPERATOR + id : id;
return groupId != null ? groupId + CHANNEL_GROUP_SEPARATOR + id : id;
}

/**
Expand All @@ -142,12 +142,12 @@ public String getIdWithoutGroup() {
if (!isInGroup()) {
return getId();
} else {
return getId().split(CHANNEL_GROUP_SEPERATOR)[1];
return getId().split(CHANNEL_GROUP_SEPARATOR)[1];
}
}

public boolean isInGroup() {
return getId().contains(CHANNEL_GROUP_SEPERATOR);
return getId().contains(CHANNEL_GROUP_SEPARATOR);
}

/**
Expand All @@ -156,7 +156,7 @@ public boolean isInGroup() {
* @return group id or null if channel is not in a group
*/
public @Nullable String getGroupId() {
return isInGroup() ? getId().split(CHANNEL_GROUP_SEPERATOR)[0] : null;
return isInGroup() ? getId().split(CHANNEL_GROUP_SEPARATOR)[0] : null;
}

@Override
Expand Down