]> git.basschouten.com Git - openhab-addons.git/blob
0e6f2092b0453c39877a75506fd29df3d6ab9ead
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.heos.internal;
14
15 import java.util.ArrayList;
16 import java.util.Collections;
17 import java.util.List;
18 import java.util.Objects;
19 import java.util.stream.Collectors;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.openhab.core.thing.Channel;
23 import org.openhab.core.thing.ChannelUID;
24 import org.openhab.core.thing.binding.ThingHandler;
25
26 /**
27  * The {@link HeosChannelManager} provides the functions to
28  * add and remove channels from the channel list provided by the thing
29  * The generation of the individual channels has to be done by the thingHandler
30  * itself.
31  *
32  * @author Johannes Einig - Initial contribution
33  */
34 @NonNullByDefault
35 public class HeosChannelManager {
36     private final ThingHandler handler;
37
38     public HeosChannelManager(ThingHandler handler) {
39         this.handler = handler;
40     }
41
42     public synchronized List<Channel> addSingleChannel(Channel channel) {
43         ChannelWrapper channelList = getChannelsFromThing();
44         channelList.removeChannel(channel.getUID());
45         channelList.add(channel);
46         return channelList.get();
47     }
48
49     public synchronized List<Channel> removeSingleChannel(String channelIdentifier) {
50         ChannelWrapper channelWrapper = getChannelsFromThing();
51         channelWrapper.removeChannel(generateChannelUID(channelIdentifier));
52         return channelWrapper.get();
53     }
54
55     /*
56      * Gets the channels from the Thing and makes the channel
57      * list editable.
58      */
59     private ChannelWrapper getChannelsFromThing() {
60         return new ChannelWrapper(handler.getThing().getChannels());
61     }
62
63     private ChannelUID generateChannelUID(String channelIdentifier) {
64         return new ChannelUID(handler.getThing().getUID(), channelIdentifier);
65     }
66
67     /**
68      * Wrap a channel list
69      *
70      * @author Martin van Wingerden - Initial contribution
71      */
72     private static class ChannelWrapper {
73         private final List<Channel> channels;
74
75         ChannelWrapper(List<Channel> channels) {
76             this.channels = new ArrayList<>(channels);
77         }
78
79         private void removeChannel(ChannelUID uid) {
80             List<Channel> itemsToBeRemoved = channels.stream().filter(Objects::nonNull)
81                     .filter(channel -> uid.equals(channel.getUID())).collect(Collectors.toList());
82
83             channels.removeAll(itemsToBeRemoved);
84         }
85
86         public void add(Channel channel) {
87             channels.add(channel);
88         }
89
90         public List<Channel> get() {
91             return Collections.unmodifiableList(channels);
92         }
93     }
94 }