This repository has been archived by the owner on May 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 781
[core] Added implementation of a ChannelGroupUID #6103
Merged
htreu
merged 2 commits into
eclipse-archived:master
from
cweitkamp:feature-channelgroupuid
Sep 4, 2018
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
60 changes: 60 additions & 0 deletions
60
...e.core.thing.test/src/test/java/org/eclipse/smarthome/core/thing/ChannelGroupUIDTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/** | ||
* Copyright (c) 2014,2018 Contributors to the Eclipse Foundation | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.eclipse.smarthome.core.thing; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import org.junit.Test; | ||
|
||
/** | ||
* Tests for class {@link ChannelGroupUID}. | ||
* | ||
* @author Christoph Weitkamp - Initial contribution | ||
*/ | ||
public class ChannelGroupUIDTest { | ||
|
||
private static final String BINDING_ID = "binding"; | ||
private static final String THING_TYPE_ID = "thing-type"; | ||
private static final String THING_ID = "thing"; | ||
private static final String GROUP_ID = "group"; | ||
|
||
private static final ThingUID THING_UID = new ThingUID(BINDING_ID, THING_TYPE_ID, THING_ID); | ||
|
||
@Test(expected = IllegalArgumentException.class) | ||
public void testInvalidCharacters() { | ||
new ChannelGroupUID(THING_UID, "id_with_invalidchar%"); | ||
} | ||
|
||
@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"); | ||
} | ||
|
||
@Test | ||
public void testChannelGroupUID() { | ||
ChannelGroupUID channelGroupUID = new ChannelGroupUID(THING_UID, GROUP_ID); | ||
assertEquals("binding:thing-type:thing:group#", channelGroupUID.toString()); | ||
assertEquals(GROUP_ID, channelGroupUID.getId()); | ||
assertEquals(THING_UID, channelGroupUID.getThingUID()); | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
....smarthome.core.thing/src/main/java/org/eclipse/smarthome/core/thing/ChannelGroupUID.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/** | ||
* Copyright (c) 2014,2018 Contributors to the Eclipse Foundation | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.eclipse.smarthome.core.thing; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
|
||
/** | ||
* {@link ChannelGroupUID} represents a unique identifier for channel groups. | ||
* | ||
* @author Christoph Weitkamp - Initial contribution | ||
*/ | ||
@NonNullByDefault | ||
public class ChannelGroupUID extends UID { | ||
|
||
private static final String CHANNEL_GROUP_SEGMENT_PATTERN = "[\\w-]*#"; | ||
private static final String CHANNEL_GROUP_SEPERATOR = "#"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Typo: This should be SEPARATOR There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hehe, that is a little copy&paste error from |
||
|
||
/** | ||
* Default constructor in package scope only. Will allow to instantiate this | ||
* class by reflection. Not intended to be used for normal instantiation. | ||
*/ | ||
ChannelGroupUID() { | ||
super(); | ||
} | ||
|
||
public ChannelGroupUID(String channelGroupUid) { | ||
super(channelGroupUid); | ||
} | ||
|
||
/** | ||
* @param thingUID the unique identifier of the thing the channel belongs to | ||
* @param id the channel group's id | ||
*/ | ||
public ChannelGroupUID(ThingUID thingUID, String id) { | ||
super(toSegments(thingUID, id)); | ||
} | ||
|
||
private static List<String> toSegments(ThingUID thingUID, String id) { | ||
List<String> ret = new ArrayList<>(thingUID.getAllSegments()); | ||
ret.add(id + CHANNEL_GROUP_SEPERATOR); | ||
return ret; | ||
} | ||
|
||
/** | ||
* Returns the id. | ||
* | ||
* @return id | ||
*/ | ||
public String getId() { | ||
List<String> segments = getAllSegments(); | ||
return segments.get(segments.size() - 1).replaceAll(CHANNEL_GROUP_SEPERATOR, ""); | ||
} | ||
|
||
@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 | ||
* | ||
* @return the thing UID | ||
*/ | ||
public ThingUID getThingUID() { | ||
List<String> allSegments = getAllSegments(); | ||
return new ThingUID(allSegments.subList(0, allSegments.size() - 1).toArray(new String[allSegments.size() - 1])); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not in line with what the
ChannelGroupTypeUID
expects as achannelGroupUid
(String) parameter - the separator should NOT be part of the UID (that anyhow looks a bit weird to me).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay. I prepare a follow up to remove the
#
at the end.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See #6157