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

[core] Added implementation of a ChannelGroupUID #6103

Merged
merged 2 commits into from
Sep 4, 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
@@ -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());
Copy link
Contributor

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 a channelGroupUid (String) parameter - the separator should NOT be part of the UID (that anyhow looks a bit weird to me).

Copy link
Contributor Author

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #6157

assertEquals(GROUP_ID, channelGroupUID.getId());
assertEquals(THING_UID, channelGroupUID.getThingUID());
}
}
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 = "#";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo: This should be SEPARATOR

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hehe, that is a little copy&paste error from ChannelUID 😉. I will change it.


/**
* 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]));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ public ChannelUID(ThingTypeUID thingTypeUID, ThingUID thingUID, String id) {
super(toSegments(thingUID, null, id));
}

/**
* @param channelGroupUID the unique identifier of the channel group the channel belongs to
* @param id the channel's id
*/
public ChannelUID(ChannelGroupUID channelGroupUID, String id) {
super(toSegments(channelGroupUID.getThingUID(), channelGroupUID.getId(), id));
}

/**
* @param thingUID the unique identifier of the thing the channel belongs to
* @param groupId the channel's group id
Expand Down