Refactoring of builders (#908)

Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
This commit is contained in:
Christoph Weitkamp 2019-09-16 21:10:39 +02:00 committed by Kai Kreuzer
parent cf4b752ae1
commit cd57f1d79c
9 changed files with 347 additions and 154 deletions

View File

@ -12,7 +12,6 @@
*/
package org.eclipse.smarthome.core.thing.binding.builder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@ -36,24 +35,23 @@ import org.eclipse.smarthome.core.thing.internal.BridgeImpl;
@NonNullByDefault
public class BridgeBuilder extends ThingBuilder {
private BridgeBuilder(BridgeImpl thing) {
super(thing);
private BridgeBuilder(ThingTypeUID thingTypeUID, ThingUID thingUID) {
super(thingTypeUID, thingUID);
}
public static BridgeBuilder create(ThingTypeUID thingTypeUID, String bridgeId) {
BridgeImpl bridge = new BridgeImpl(thingTypeUID, bridgeId);
bridge.setChannels(new ArrayList<Channel>());
return new BridgeBuilder(bridge);
return new BridgeBuilder(thingTypeUID,
new ThingUID(thingTypeUID.getBindingId(), thingTypeUID.getId(), bridgeId));
}
public static BridgeBuilder create(ThingTypeUID thingTypeUID, ThingUID thingUID) {
BridgeImpl bridge = new BridgeImpl(thingTypeUID, thingUID);
return new BridgeBuilder(bridge);
return new BridgeBuilder(thingTypeUID, thingUID);
}
@Override
public Bridge build() {
return (Bridge) super.build();
final BridgeImpl bridge = new BridgeImpl(thingTypeUID, thingUID);
return (Bridge) super.populate(bridge);
}
@Override
@ -81,6 +79,16 @@ public class BridgeBuilder extends ThingBuilder {
return (BridgeBuilder) super.withoutChannel(channelUID);
}
@Override
public BridgeBuilder withoutChannels(Channel... channels) {
return (BridgeBuilder) super.withoutChannels(channels);
}
@Override
public BridgeBuilder withoutChannels(List<Channel> channels) {
return (BridgeBuilder) super.withoutChannels(channels);
}
@Override
public BridgeBuilder withConfiguration(Configuration thingConfiguration) {
return (BridgeBuilder) super.withConfiguration(thingConfiguration);

View File

@ -39,25 +39,35 @@ import org.eclipse.smarthome.core.thing.util.ThingHelper;
@NonNullByDefault
public class ThingBuilder {
protected final ThingUID thingUID;
protected final ThingTypeUID thingTypeUID;
private @Nullable String label;
private final List<Channel> channels = new ArrayList<>();
private final ThingImpl thing;
private @Nullable Configuration configuration;
private @Nullable ThingUID bridgeUID;
private @Nullable Map<String, String> properties;
private @Nullable String location;
protected ThingBuilder(ThingImpl thing) {
this.thing = thing;
protected ThingBuilder(ThingTypeUID thingTypeUID, ThingUID thingUID) {
this.thingUID = thingUID;
this.thingTypeUID = thingTypeUID;
}
public static ThingBuilder create(ThingTypeUID thingTypeUID, String thingId) {
ThingImpl thing = new ThingImpl(thingTypeUID, thingId);
return new ThingBuilder(thing);
return new ThingBuilder(thingTypeUID, new ThingUID(thingTypeUID.getBindingId(), thingTypeUID.getId(), thingId));
}
public static ThingBuilder create(ThingTypeUID thingTypeUID, ThingUID thingUID) {
ThingImpl thing = new ThingImpl(thingTypeUID, thingUID);
return new ThingBuilder(thing);
return new ThingBuilder(thingTypeUID, thingUID);
}
public Thing build() {
final ThingImpl thing = new ThingImpl(thingTypeUID, thingUID);
return populate(thing);
}
public ThingBuilder withLabel(@Nullable String label) {
this.thing.setLabel(label);
this.label = label;
return this;
}
@ -102,38 +112,45 @@ public class ThingBuilder {
return this;
}
public ThingBuilder withConfiguration(Configuration thingConfiguration) {
this.thing.setConfiguration(thingConfiguration);
public ThingBuilder withConfiguration(Configuration configuration) {
this.configuration = configuration;
return this;
}
public ThingBuilder withBridge(@Nullable ThingUID bridgeUID) {
this.thing.setBridgeUID(bridgeUID);
this.bridgeUID = bridgeUID;
return this;
}
public ThingBuilder withProperties(Map<String, String> properties) {
for (String key : properties.keySet()) {
this.thing.setProperty(key, properties.get(key));
}
this.properties = properties;
return this;
}
public ThingBuilder withLocation(@Nullable String location) {
this.thing.setLocation(location);
this.location = location;
return this;
}
public Thing build() {
protected Thing populate(ThingImpl thing) {
thing.setLabel(label);
thing.setChannels(channels);
thing.setConfiguration(configuration);
thing.setBridgeUID(bridgeUID);
if (properties != null) {
for (Map.Entry<String, String> entry : properties.entrySet()) {
thing.setProperty(entry.getKey(), entry.getValue());
}
}
thing.setLocation(location);
return thing;
}
private void validateChannelUIDs(List<Channel> channels) {
for (Channel channel : channels) {
if (!thing.getUID().equals(channel.getUID().getThingUID())) {
if (!thingUID.equals(channel.getUID().getThingUID())) {
throw new IllegalArgumentException(
"Channel UID '" + channel.getUID() + "' does not match thing UID '" + thing.getUID() + "'");
"Channel UID '" + channel.getUID() + "' does not match thing UID '" + thingUID + "'");
}
}
}

View File

@ -0,0 +1,104 @@
/**
* Copyright (c) 2010-2019 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* 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.binding.builder;
import static org.eclipse.smarthome.core.thing.DefaultSystemChannelTypeProvider.SYSTEM_OUTDOOR_TEMPERATURE;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.assertThat;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.smarthome.core.thing.Channel;
import org.eclipse.smarthome.core.thing.ChannelUID;
import org.eclipse.smarthome.core.thing.ThingTypeUID;
import org.eclipse.smarthome.core.thing.ThingUID;
import org.eclipse.smarthome.core.thing.type.ChannelKind;
import org.eclipse.smarthome.core.thing.type.ThingType;
import org.eclipse.smarthome.core.thing.type.ThingTypeBuilder;
import org.junit.Before;
import org.junit.Test;
/**
* Tests the {@link ChannelBuilder}.
*
* @author Christoph Weitkamp - Initial contribution
*/
public class ChannelBuilderTest {
private static final String KEY1 = "key1";
private static final String KEY2 = "key2";
private static final String VALUE1 = "value1";
private static final String VALUE2 = "value2";
private final Map<String, String> properties = new HashMap<String, String>() {
private static final long serialVersionUID = 1L;
{
put(KEY1, VALUE1);
put(KEY2, VALUE2);
}
};
private ChannelBuilder builder;
private Channel channel;
@Before
public void setup() {
ThingType thingType = ThingTypeBuilder.instance(new ThingTypeUID("bindingId", "thingTypeId"), "thingLabel")
.build();
ChannelUID channelUID = new ChannelUID(new ThingUID(thingType.getUID(), "thingId"), "temperature");
builder = ChannelBuilder.create(channelUID, SYSTEM_OUTDOOR_TEMPERATURE.getItemType()).withLabel("Test")
.withDescription("My test channel").withType(SYSTEM_OUTDOOR_TEMPERATURE.getUID())
.withProperties(properties);
channel = builder.build();
}
@Test
public void testChannelBuilder() {
assertThat(channel.getAcceptedItemType(), is(SYSTEM_OUTDOOR_TEMPERATURE.getItemType()));
assertThat(channel.getChannelTypeUID(), is(SYSTEM_OUTDOOR_TEMPERATURE.getUID()));
assertThat(channel.getDefaultTags().size(), is(0));
assertThat(channel.getDescription(), is("My test channel"));
assertThat(channel.getKind(), is(ChannelKind.STATE));
assertThat(channel.getLabel(), is("Test"));
assertThat(channel.getProperties().size(), is(2));
assertThat(channel.getProperties().get(KEY1), is(VALUE1));
assertThat(channel.getProperties().get(KEY2), is(VALUE2));
}
@Test
public void testChannelBuilderFromChannel() {
Channel otherChannel = ChannelBuilder.create(channel).build();
assertThat(otherChannel.getAcceptedItemType(), is(channel.getAcceptedItemType()));
assertThat(otherChannel.getChannelTypeUID(), is(channel.getChannelTypeUID()));
assertThat(otherChannel.getConfiguration(), is(channel.getConfiguration()));
assertThat(otherChannel.getDefaultTags().size(), is(channel.getDefaultTags().size()));
assertThat(otherChannel.getDescription(), is(channel.getDescription()));
assertThat(otherChannel.getKind(), is(channel.getKind()));
assertThat(otherChannel.getLabel(), is(channel.getLabel()));
assertThat(otherChannel.getProperties().size(), is(channel.getProperties().size()));
assertThat(otherChannel.getProperties().get(KEY1), is(channel.getProperties().get(KEY1)));
assertThat(otherChannel.getProperties().get(KEY2), is(channel.getProperties().get(KEY2)));
assertThat(otherChannel.getUID(), is(channel.getUID()));
}
@Test
public void subsequentBuildsCreateIndependentChannels() {
Channel otherChannel = builder.withLabel("Second Test").withDescription("My second test channel")
.withProperties(Collections.emptyMap()).build();
assertThat(otherChannel.getDescription(), is(not(channel.getDescription())));
assertThat(otherChannel.getLabel(), is(not(channel.getLabel())));
assertThat(otherChannel.getProperties().size(), is(not(channel.getProperties().size())));
}
}

View File

@ -16,34 +16,65 @@ import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.assertThat;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.smarthome.core.thing.ChannelUID;
import org.eclipse.smarthome.core.thing.Thing;
import org.eclipse.smarthome.core.thing.ThingTypeUID;
import org.eclipse.smarthome.core.thing.ThingUID;
import org.eclipse.smarthome.core.thing.binding.builder.ChannelBuilder;
import org.eclipse.smarthome.core.thing.binding.builder.ThingBuilder;
import org.eclipse.smarthome.core.thing.internal.BridgeImpl;
import org.eclipse.smarthome.core.thing.internal.ThingImpl;
import org.junit.Before;
import org.junit.Test;
/**
*
* @author Simon Kaufmann - Initial contribution and API
*
* @author Simon Kaufmann - Initial contribution
*/
public class ThingBuilderTest {
private static final ThingTypeUID THING_TYPE_UID = new ThingTypeUID("test", "test");
private static final ThingUID THING_UID = new ThingUID(THING_TYPE_UID, "test");
private static final String KEY1 = "key1";
private static final String KEY2 = "key2";
private static final String VALUE1 = "value1";
private static final String VALUE2 = "value2";
private final Map<String, String> properties = new HashMap<String, String>() {
private static final long serialVersionUID = 1L;
{
put(KEY1, VALUE1);
put(KEY2, VALUE2);
}
};
private ThingBuilder thingBuilder;
@Before
public void setup() {
thingBuilder = ThingBuilder.create(THING_TYPE_UID, THING_UID);
}
@Test
public void testInstance() {
assertThat(thingBuilder, is(instanceOf(ThingBuilder.class)));
assertThat(thingBuilder.withLabel("TEST"), is(instanceOf(ThingBuilder.class)));
assertThat(thingBuilder.build(), is(instanceOf(ThingImpl.class)));
final BridgeBuilder bridgeBuilder = BridgeBuilder.create(THING_TYPE_UID, THING_UID);
assertThat(bridgeBuilder, is(instanceOf(BridgeBuilder.class)));
assertThat(bridgeBuilder.withLabel("TEST"), is(instanceOf(BridgeBuilder.class)));
assertThat(bridgeBuilder.build(), is(instanceOf(BridgeImpl.class)));
}
@Test(expected = IllegalArgumentException.class)
public void testWithChannel_duplicates() {
ThingBuilder thingBuilder = ThingBuilder.create(THING_TYPE_UID, THING_UID);
thingBuilder.withChannel(ChannelBuilder.create(new ChannelUID(THING_UID, "channel1"), "").build());
thingBuilder.withChannel(ChannelBuilder.create(new ChannelUID(THING_UID, "channel1"), "").build());
}
@Test(expected = IllegalArgumentException.class)
public void testWithChannels_duplicatesCollections() {
ThingBuilder thingBuilder = ThingBuilder.create(THING_TYPE_UID, THING_UID);
thingBuilder.withChannels(Arrays.asList( //
ChannelBuilder.create(new ChannelUID(THING_UID, "channel1"), "").build(), //
ChannelBuilder.create(new ChannelUID(THING_UID, "channel1"), "").build()));
@ -51,7 +82,6 @@ public class ThingBuilderTest {
@Test(expected = IllegalArgumentException.class)
public void testWithChannels_duplicatesVararg() {
ThingBuilder thingBuilder = ThingBuilder.create(THING_TYPE_UID, THING_UID);
thingBuilder.withChannels( //
ChannelBuilder.create(new ChannelUID(THING_UID, "channel1"), "").build(), //
ChannelBuilder.create(new ChannelUID(THING_UID, "channel1"), "").build());
@ -59,18 +89,17 @@ public class ThingBuilderTest {
@Test
public void testWithoutChannel() {
ThingBuilder thingBuilder = ThingBuilder.create(THING_TYPE_UID, THING_UID);
thingBuilder.withChannels( //
ChannelBuilder.create(new ChannelUID(THING_UID, "channel1"), "").build(), //
ChannelBuilder.create(new ChannelUID(THING_UID, "channel2"), "").build());
thingBuilder.withoutChannel(new ChannelUID(THING_UID, "channel1"));
assertThat(thingBuilder.build().getChannels().size(), is(equalTo(1)));
assertThat(thingBuilder.build().getChannels().get(0).getUID().getId(), is(equalTo("channel2")));
Thing thing = thingBuilder.build();
assertThat(thing.getChannels().size(), is(equalTo(1)));
assertThat(thing.getChannels().get(0).getUID().getId(), is(equalTo("channel2")));
}
@Test
public void testWithoutChannel_missing() {
ThingBuilder thingBuilder = ThingBuilder.create(THING_TYPE_UID, THING_UID);
thingBuilder.withChannels( //
ChannelBuilder.create(new ChannelUID(THING_UID, "channel1"), "").build(), //
ChannelBuilder.create(new ChannelUID(THING_UID, "channel2"), "").build());
@ -80,8 +109,18 @@ public class ThingBuilderTest {
@Test(expected = IllegalArgumentException.class)
public void testWithChannel_wrongThing() {
ThingBuilder thingBuilder = ThingBuilder.create(THING_TYPE_UID, THING_UID);
thingBuilder.withChannel(
ChannelBuilder.create(new ChannelUID(new ThingUID(THING_TYPE_UID, "wrong"), "channel1"), "").build());
}
@Test
public void subsequentBuildsCreateIndependentThings() {
Thing thing = thingBuilder.withLabel("Test").withLocation("Some Place").withProperties(properties).build();
Thing otherThing = thingBuilder.withLabel("Second Test").withLocation("Other Place")
.withProperties(Collections.emptyMap()).build();
assertThat(otherThing.getLabel(), is(not(thing.getLabel())));
assertThat(otherThing.getLocation(), is(not(thing.getLocation())));
assertThat(otherThing.getProperties().size(), is(not(thing.getProperties().size())));
}
}

View File

@ -0,0 +1,76 @@
/**
* Copyright (c) 2010-2019 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* 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.binding.builder;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.assertThat;
import org.eclipse.smarthome.core.thing.ThingStatus;
import org.eclipse.smarthome.core.thing.ThingStatusDetail;
import org.eclipse.smarthome.core.thing.ThingStatusInfo;
import org.junit.Before;
import org.junit.Test;
/**
* Tests the {@link ThingStatusInfoBuilder}.
*
* @author Christoph Weitkamp - Initial contribution
*/
public class ThingStatusInfoBuilderTest {
private ThingStatusInfoBuilder builder;
@Before
public void setup() {
builder = ThingStatusInfoBuilder.create(ThingStatus.ONLINE);
}
@Test
public void testThingStatusInfoBuilderStatus() {
ThingStatusInfo thigStatusInfo = builder.build();
assertThat(thigStatusInfo.getStatus(), is(ThingStatus.ONLINE));
assertThat(thigStatusInfo.getStatusDetail(), is(ThingStatusDetail.NONE));
assertThat(thigStatusInfo.getDescription(), is(nullValue()));
}
@Test
public void testThingStatusInfoBuilderStatusDetails() {
ThingStatusInfo thigStatusInfo = builder.withStatusDetail(ThingStatusDetail.DISABLED).build();
assertThat(thigStatusInfo.getStatus(), is(ThingStatus.ONLINE));
assertThat(thigStatusInfo.getStatusDetail(), is(ThingStatusDetail.DISABLED));
assertThat(thigStatusInfo.getDescription(), is(nullValue()));
}
@Test
public void testThingStatusInfoBuilderStatusDescription() {
ThingStatusInfo thigStatusInfo = builder.withDescription("My test description").build();
assertThat(thigStatusInfo.getStatus(), is(ThingStatus.ONLINE));
assertThat(thigStatusInfo.getStatusDetail(), is(ThingStatusDetail.NONE));
assertThat(thigStatusInfo.getDescription(), is("My test description"));
}
@Test
public void subsequentBuildsCreateIndependentThingStatusInfos() {
ThingStatusInfo thigStatusInfo1 = builder.build();
ThingStatusInfo thigStatusInfo2 = builder.withStatusDetail(ThingStatusDetail.DISABLED)
.withDescription("My test description").build();
assertThat(thigStatusInfo2.getStatus(), is(thigStatusInfo1.getStatus()));
assertThat(thigStatusInfo2.getStatusDetail(), is(not(thigStatusInfo1.getStatusDetail())));
assertThat(thigStatusInfo2.getDescription(), is(not(thigStatusInfo1.getDescription())));
}
}

View File

@ -13,12 +13,14 @@
package org.eclipse.smarthome.core.internal.types;
import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.smarthome.core.types.StateDescription;
import org.eclipse.smarthome.core.types.StateDescriptionFragment;
import org.eclipse.smarthome.core.types.StateDescriptionFragmentBuilder;
import org.eclipse.smarthome.core.types.StateOption;
/**
@ -26,6 +28,7 @@ import org.eclipse.smarthome.core.types.StateOption;
*
* @author Henning Treu - Initial contribution
*/
@NonNullByDefault
public class StateDescriptionFragmentImpl implements StateDescriptionFragment {
private @Nullable BigDecimal minimum;
@ -43,10 +46,33 @@ public class StateDescriptionFragmentImpl implements StateDescriptionFragment {
}
/**
* Create a {@link StateDescriptionFragmentImpl} and initialise from the given {@link StateDescription}.
* Create a {@link StateDescriptionFragmentImpl} and initialize from the given values.
*
* @param minimum minimum value of the state
* @param maximum maximum value of the state
* @param step step size
* @param pattern pattern to render the state
* @param readOnly if the state can be changed by the system
* @param options predefined list of options
* @deprecated use {@link StateDescriptionFragmentBuilder} instead.
*/
@Deprecated
public StateDescriptionFragmentImpl(@Nullable BigDecimal minimum, @Nullable BigDecimal maximum,
@Nullable BigDecimal step, @Nullable String pattern, @Nullable Boolean readOnly,
@Nullable List<StateOption> options) {
this.minimum = minimum;
this.maximum = maximum;
this.step = step;
this.pattern = pattern;
this.readOnly = readOnly;
this.options = options == null ? Collections.emptyList() : Collections.unmodifiableList(options);
}
/**
* Create a {@link StateDescriptionFragmentImpl} and initialize from the given {@link StateDescription}.
* Note: State options will only be set if not empty.
*
* @param legacy the {@link StateDescription} to initialise from.
* @param legacy the {@link StateDescription} to initialize from.
*/
public StateDescriptionFragmentImpl(StateDescription legacy) {
this.minimum = legacy.getMinimum();
@ -54,7 +80,7 @@ public class StateDescriptionFragmentImpl implements StateDescriptionFragment {
this.step = legacy.getStep();
this.pattern = legacy.getPattern();
this.readOnly = Boolean.valueOf(legacy.isReadOnly());
if (legacy.getOptions() != null && !legacy.getOptions().isEmpty()) {
if (!legacy.getOptions().isEmpty()) {
this.options = legacy.getOptions();
}
}
@ -64,7 +90,7 @@ public class StateDescriptionFragmentImpl implements StateDescriptionFragment {
*
* @param source the source to copy from.
*/
public StateDescriptionFragmentImpl(@NonNull StateDescriptionFragmentImpl source) {
public StateDescriptionFragmentImpl(StateDescriptionFragmentImpl source) {
this.minimum = source.getMinimum();
this.maximum = source.getMaximum();
this.step = source.getStep();
@ -74,7 +100,7 @@ public class StateDescriptionFragmentImpl implements StateDescriptionFragment {
}
@Override
public BigDecimal getMinimum() {
public @Nullable BigDecimal getMinimum() {
return minimum;
}
@ -83,7 +109,7 @@ public class StateDescriptionFragmentImpl implements StateDescriptionFragment {
}
@Override
public BigDecimal getMaximum() {
public @Nullable BigDecimal getMaximum() {
return maximum;
}
@ -92,7 +118,7 @@ public class StateDescriptionFragmentImpl implements StateDescriptionFragment {
}
@Override
public BigDecimal getStep() {
public @Nullable BigDecimal getStep() {
return step;
}
@ -101,7 +127,7 @@ public class StateDescriptionFragmentImpl implements StateDescriptionFragment {
}
@Override
public String getPattern() {
public @Nullable String getPattern() {
return pattern;
}
@ -110,7 +136,7 @@ public class StateDescriptionFragmentImpl implements StateDescriptionFragment {
}
@Override
public Boolean isReadOnly() {
public @Nullable Boolean isReadOnly() {
return readOnly;
}
@ -119,7 +145,7 @@ public class StateDescriptionFragmentImpl implements StateDescriptionFragment {
}
@Override
public List<StateOption> getOptions() {
public @Nullable List<StateOption> getOptions() {
return options;
}
@ -127,6 +153,7 @@ public class StateDescriptionFragmentImpl implements StateDescriptionFragment {
this.options = options;
}
@SuppressWarnings("deprecation")
@Override
public @Nullable StateDescription toStateDescription() {
if (minimum == null && maximum == null && step == null && readOnly == null && pattern == null

View File

@ -16,6 +16,7 @@ import java.math.BigDecimal;
import java.util.List;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.smarthome.core.internal.types.StateDescriptionFragmentImpl;
/**
@ -26,14 +27,26 @@ import org.eclipse.smarthome.core.internal.types.StateDescriptionFragmentImpl;
@NonNullByDefault
public class StateDescriptionFragmentBuilder {
private final StateDescriptionFragmentImpl fragment;
private @Nullable BigDecimal minimum;
private @Nullable BigDecimal maximum;
private @Nullable BigDecimal step;
private @Nullable String pattern;
private @Nullable Boolean readOnly;
private @Nullable List<StateOption> options;
private StateDescriptionFragmentBuilder() {
fragment = new StateDescriptionFragmentImpl();
//
}
private StateDescriptionFragmentBuilder(StateDescription legacy) {
fragment = new StateDescriptionFragmentImpl(legacy);
this.minimum = legacy.getMinimum();
this.maximum = legacy.getMaximum();
this.step = legacy.getStep();
this.pattern = legacy.getPattern();
this.readOnly = Boolean.valueOf(legacy.isReadOnly());
if (!legacy.getOptions().isEmpty()) {
this.options = legacy.getOptions();
}
}
/**
@ -46,10 +59,10 @@ public class StateDescriptionFragmentBuilder {
}
/**
* Create a builder instance and initialise all fields from the given {@link StateDescription}.
* Create a builder instance and initialize all fields from the given {@link StateDescription}.
* Note: State options will only be taken into account if the list is not empty.
*
* @param legacy the {@link StateDescription} this builder be initialised from.
* @param legacy the {@link StateDescription} this builder be initialized from.
* @return the builder.
*/
public static StateDescriptionFragmentBuilder create(StateDescription legacy) {
@ -61,8 +74,9 @@ public class StateDescriptionFragmentBuilder {
*
* @return a {@link StateDescriptionFragment} from the values of this builder.
*/
@SuppressWarnings("deprecation")
public StateDescriptionFragment build() {
return new StateDescriptionFragmentImpl(fragment);
return new StateDescriptionFragmentImpl(minimum, maximum, step, pattern, readOnly, options);
}
/**
@ -72,7 +86,7 @@ public class StateDescriptionFragmentBuilder {
* @return this builder.
*/
public StateDescriptionFragmentBuilder withMaximum(BigDecimal maximum) {
fragment.setMaximum(maximum);
this.maximum = maximum;
return this;
}
@ -83,7 +97,7 @@ public class StateDescriptionFragmentBuilder {
* @return this builder.
*/
public StateDescriptionFragmentBuilder withMinimum(BigDecimal minimum) {
fragment.setMinimum(minimum);
this.minimum = minimum;
return this;
}
@ -94,7 +108,7 @@ public class StateDescriptionFragmentBuilder {
* @return this builder.
*/
public StateDescriptionFragmentBuilder withStep(BigDecimal step) {
fragment.setStep(step);
this.step = step;
return this;
}
@ -105,7 +119,7 @@ public class StateDescriptionFragmentBuilder {
* @return this builder.
*/
public StateDescriptionFragmentBuilder withPattern(String pattern) {
fragment.setPattern(pattern);
this.pattern = pattern;
return this;
}
@ -116,7 +130,7 @@ public class StateDescriptionFragmentBuilder {
* @return this builder.
*/
public StateDescriptionFragmentBuilder withReadOnly(Boolean readOnly) {
fragment.setReadOnly(readOnly);
this.readOnly = readOnly;
return this;
}
@ -127,7 +141,7 @@ public class StateDescriptionFragmentBuilder {
* @return this builder.
*/
public StateDescriptionFragmentBuilder withOptions(List<StateOption> options) {
fragment.setOptions(options);
this.options = options;
return this;
}
}

View File

@ -28,7 +28,6 @@ import org.junit.Test;
* Test the {@link StateDescriptionFragmentBuilder}.
*
* @author Henning Treu - Initial contribution
*
*/
public class StateDescriptionFragmentBuilderTest {

View File

@ -1,91 +0,0 @@
/**
* Copyright (c) 2010-2019 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* 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.binding.builder;
import static org.eclipse.smarthome.core.thing.DefaultSystemChannelTypeProvider.SYSTEM_OUTDOOR_TEMPERATURE;
import static org.junit.Assert.assertEquals;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.smarthome.core.thing.Channel;
import org.eclipse.smarthome.core.thing.ChannelUID;
import org.eclipse.smarthome.core.thing.ThingTypeUID;
import org.eclipse.smarthome.core.thing.ThingUID;
import org.eclipse.smarthome.core.thing.type.ChannelKind;
import org.eclipse.smarthome.core.thing.type.ThingType;
import org.eclipse.smarthome.core.thing.type.ThingTypeBuilder;
import org.eclipse.smarthome.test.java.JavaOSGiTest;
import org.junit.Before;
import org.junit.Test;
/**
* Tests the {@link ChannelBuilder}.
*
* @author Christoph Weitkamp - Initial contribution
*/
public class ChannelBuilderTest extends JavaOSGiTest {
private static final String KEY1 = "key1";
private static final String KEY2 = "key2";
private static final String VALUE1 = "value1";
private static final String VALUE2 = "value2";
private final Map<String, String> properties = new HashMap<String, String>() {
private static final long serialVersionUID = 1L;
{
put(KEY1, VALUE1);
put(KEY2, VALUE2);
}
};
private Channel channel;
@Before
public void setup() {
ThingType thingType = ThingTypeBuilder.instance(new ThingTypeUID("bindingId", "thingTypeId"), "thingLabel")
.build();
ChannelUID channelUID = new ChannelUID(new ThingUID(thingType.getUID(), "thingId"), "temperature");
channel = ChannelBuilder.create(channelUID, SYSTEM_OUTDOOR_TEMPERATURE.getItemType()).withLabel("Test")
.withDescription("My test channel").withType(SYSTEM_OUTDOOR_TEMPERATURE.getUID())
.withProperties(properties).build();
}
@Test
public void testChannelBuilder() {
assertEquals(SYSTEM_OUTDOOR_TEMPERATURE.getItemType(), channel.getAcceptedItemType());
assertEquals(SYSTEM_OUTDOOR_TEMPERATURE.getUID(), channel.getChannelTypeUID());
assertEquals(0, channel.getDefaultTags().size());
assertEquals("My test channel", channel.getDescription());
assertEquals(ChannelKind.STATE, channel.getKind());
assertEquals("Test", channel.getLabel());
assertEquals(2, channel.getProperties().size());
assertEquals(VALUE1, channel.getProperties().get(KEY1));
assertEquals(VALUE2, channel.getProperties().get(KEY2));
}
@Test
public void testChannelBuilderFromChannel() {
Channel otherChannel = ChannelBuilder.create(channel).build();
assertEquals(channel.getAcceptedItemType(), otherChannel.getAcceptedItemType());
assertEquals(channel.getChannelTypeUID(), otherChannel.getChannelTypeUID());
assertEquals(channel.getConfiguration(), otherChannel.getConfiguration());
assertEquals(channel.getDefaultTags().size(), otherChannel.getDefaultTags().size());
assertEquals(channel.getDescription(), otherChannel.getDescription());
assertEquals(channel.getKind(), otherChannel.getKind());
assertEquals(channel.getLabel(), otherChannel.getLabel());
assertEquals(channel.getProperties().size(), otherChannel.getProperties().size());
assertEquals(channel.getProperties().get(KEY1), otherChannel.getProperties().get(KEY1));
assertEquals(channel.getProperties().get(KEY2), otherChannel.getProperties().get(KEY2));
assertEquals(channel.getUID(), otherChannel.getUID());
}
}