2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.heos.internal;
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;
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;
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
32 * @author Johannes Einig - Initial contribution
35 public class HeosChannelManager {
36 private final ThingHandler handler;
38 public HeosChannelManager(ThingHandler handler) {
39 this.handler = handler;
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();
49 public synchronized List<Channel> removeSingleChannel(String channelIdentifier) {
50 ChannelWrapper channelWrapper = getChannelsFromThing();
51 channelWrapper.removeChannel(generateChannelUID(channelIdentifier));
52 return channelWrapper.get();
56 * Gets the channels from the Thing and makes the channel
59 private ChannelWrapper getChannelsFromThing() {
60 return new ChannelWrapper(handler.getThing().getChannels());
63 private ChannelUID generateChannelUID(String channelIdentifier) {
64 return new ChannelUID(handler.getThing().getUID(), channelIdentifier);
70 * @author Martin van Wingerden - Initial contribution
72 private static class ChannelWrapper {
73 private final List<Channel> channels;
75 ChannelWrapper(List<Channel> channels) {
76 this.channels = new ArrayList<>(channels);
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());
83 channels.removeAll(itemsToBeRemoved);
86 public void add(Channel channel) {
87 channels.add(channel);
90 public List<Channel> get() {
91 return Collections.unmodifiableList(channels);