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
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[core] Added implementation of a ChannelGroupUID (#6103)
* Added implementation and tests for a ChannelGroupUID * Added new constructor in ChannelUID Signed-off-by: Christoph Weitkamp <[email protected]>
- Loading branch information
Showing
3 changed files
with
162 additions
and
0 deletions.
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 = "#"; | ||
|
||
/** | ||
* 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