Revert ThingImpl and BridgeImpl changes as the storage uses those implementations (#719)

Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
pull/723/head
Christoph Weitkamp 2019-04-14 23:09:09 +02:00 committed by Markus Rathgeb
parent 48ce8357f5
commit 0aa7661764
2 changed files with 32 additions and 16 deletions

View File

@ -15,8 +15,7 @@ package org.eclipse.smarthome.core.thing.internal;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ConcurrentHashMap;
import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jdt.annotation.Nullable;
@ -32,14 +31,18 @@ import org.slf4j.LoggerFactory;
/** /**
* *
* @author Denis Nobel - Initial contribution * @author Denis Nobel - Initial contribution
* @author Christoph Weitkamp - Changed internal handling of things and added method `getThing(ThingUID)` * @author Christoph Weitkamp - Added method `getThing(ThingUID)`
*/ */
@NonNullByDefault @NonNullByDefault
public class BridgeImpl extends ThingImpl implements Bridge { public class BridgeImpl extends ThingImpl implements Bridge {
private final transient Logger logger = LoggerFactory.getLogger(BridgeImpl.class); private final transient Logger logger = LoggerFactory.getLogger(BridgeImpl.class);
private final transient Map<ThingUID, Thing> things = new ConcurrentHashMap<>(); /*
* !!! DO NOT CHANGE - We are not allowed to change the members of the BridgeImpl implementation as the storage for
* things uses this implementation itself to store and restore the data.
*/
private transient List<Thing> things = new CopyOnWriteArrayList<>();
/** /**
* Package protected default constructor to allow reflective instantiation. * Package protected default constructor to allow reflective instantiation.
@ -73,21 +76,26 @@ public class BridgeImpl extends ThingImpl implements Bridge {
} }
public void addThing(Thing thing) { public void addThing(Thing thing) {
things.put(thing.getUID(), thing); things.add(thing);
} }
public void removeThing(Thing thing) { public void removeThing(Thing thing) {
things.remove(thing.getUID(), thing); things.remove(thing);
} }
@Override @Override
public @Nullable Thing getThing(ThingUID thingUID) { public @Nullable Thing getThing(ThingUID thingUID) {
return things.get(thingUID); for (Thing thing : things) {
if (thing.getUID().equals(thingUID)) {
return thing;
}
}
return null;
} }
@Override @Override
public List<Thing> getThings() { public List<Thing> getThings() {
return Collections.unmodifiableList(new ArrayList<>(things.values())); return Collections.unmodifiableList(new ArrayList<>(things));
} }
@Override @Override

View File

@ -45,16 +45,20 @@ import org.eclipse.smarthome.core.thing.binding.builder.ThingStatusInfoBuilder;
* ThingType Description * ThingType Description
* @author Thomas Höfer - Added thing and thing type properties * @author Thomas Höfer - Added thing and thing type properties
* @author Simon Kaufmann - Added label * @author Simon Kaufmann - Added label
* @author Christoph Weitkamp - Changed internal handling of things and added method `getChannel(ChannelUID)` * @author Christoph Weitkamp - Added method `getChannel(ChannelUID)`
*/ */
@NonNullByDefault @NonNullByDefault
public class ThingImpl implements Thing { public class ThingImpl implements Thing {
/*
* !!! DO NOT CHANGE - We are not allowed to change the members of the ThingImpl implementation as the storage for
* things uses this implementation itself to store and restore the data.
*/
private @Nullable String label; private @Nullable String label;
private @Nullable ThingUID bridgeUID; private @Nullable ThingUID bridgeUID;
private final Map<ChannelUID, Channel> channels = new HashMap<>(); private List<Channel> channels = new ArrayList<>();
private Configuration configuration = new Configuration(); private Configuration configuration = new Configuration();
@ -133,13 +137,13 @@ public class ThingImpl implements Thing {
@Override @Override
public List<Channel> getChannels() { public List<Channel> getChannels() {
return Collections.unmodifiableList(new ArrayList<>(this.channels.values())); return Collections.unmodifiableList(new ArrayList<>(this.channels));
} }
@Override @Override
public List<Channel> getChannelsOfGroup(String channelGroupId) { public List<Channel> getChannelsOfGroup(String channelGroupId) {
List<Channel> channels = new ArrayList<>(); List<Channel> channels = new ArrayList<>();
for (Channel channel : this.channels.values()) { for (Channel channel : this.channels) {
if (channelGroupId.equals(channel.getUID().getGroupId())) { if (channelGroupId.equals(channel.getUID().getGroupId())) {
channels.add(channel); channels.add(channel);
} }
@ -149,13 +153,17 @@ public class ThingImpl implements Thing {
@Override @Override
public @Nullable Channel getChannel(String channelId) { public @Nullable Channel getChannel(String channelId) {
ChannelUID channelUID = new ChannelUID(uid, channelId); for (Channel channel : this.channels) {
return getChannel(channelUID); if (channel.getUID().getId().equals(channelId)) {
return channel;
}
}
return null;
} }
@Override @Override
public @Nullable Channel getChannel(ChannelUID channelUID) { public @Nullable Channel getChannel(ChannelUID channelUID) {
return this.channels.get(channelUID); return getChannel(channelUID.getId());
} }
@Override @Override
@ -189,7 +197,7 @@ public class ThingImpl implements Thing {
} }
public void addChannel(Channel channel) { public void addChannel(Channel channel) {
this.channels.put(channel.getUID(), channel); this.channels.add(channel);
} }
public void setChannels(List<Channel> channels) { public void setChannels(List<Channel> channels) {