]> git.basschouten.com Git - openhab-addons.git/blob
fdca5779bec7033db8eba218b519805e36553137
[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.km200.internal;
14
15 import java.util.ArrayList;
16 import java.util.Collection;
17 import java.util.List;
18 import java.util.Locale;
19 import java.util.concurrent.CopyOnWriteArrayList;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.core.thing.ThingUID;
24 import org.openhab.core.thing.type.ChannelGroupType;
25 import org.openhab.core.thing.type.ChannelGroupTypeProvider;
26 import org.openhab.core.thing.type.ChannelGroupTypeUID;
27 import org.openhab.core.thing.type.ChannelType;
28 import org.openhab.core.thing.type.ChannelTypeProvider;
29 import org.openhab.core.thing.type.ChannelTypeUID;
30 import org.osgi.service.component.annotations.Component;
31
32 /**
33  * Extends the ChannelTypeProvider for user defined channel and channel group types.
34  *
35  * @author Markus Eckhardt - Initial contribution
36  */
37 @Component(service = { ChannelTypeProvider.class, ChannelGroupTypeProvider.class, KM200ChannelTypeProvider.class })
38 @NonNullByDefault
39 public class KM200ChannelTypeProvider implements ChannelTypeProvider, ChannelGroupTypeProvider {
40     private final List<ChannelType> channelTypes = new CopyOnWriteArrayList<>();
41     private final List<ChannelGroupType> channelGroupTypes = new CopyOnWriteArrayList<>();
42
43     @Override
44     public Collection<ChannelType> getChannelTypes(@Nullable Locale locale) {
45         return channelTypes;
46     }
47
48     @Override
49     public @Nullable ChannelType getChannelType(ChannelTypeUID channelTypeUID, @Nullable Locale locale) {
50         for (ChannelType channelType : channelTypes) {
51             if (channelType.getUID().equals(channelTypeUID)) {
52                 return channelType;
53             }
54         }
55         return null;
56     }
57
58     @Override
59     public Collection<ChannelGroupType> getChannelGroupTypes(@Nullable Locale locale) {
60         return channelGroupTypes;
61     }
62
63     @Override
64     public @Nullable ChannelGroupType getChannelGroupType(ChannelGroupTypeUID channelGroupTypeUID,
65             @Nullable Locale locale) {
66         for (ChannelGroupType channelGroupType : channelGroupTypes) {
67             if (channelGroupType.getUID().equals(channelGroupTypeUID)) {
68                 return channelGroupType;
69             }
70         }
71         return null;
72     }
73
74     public void addChannelType(ChannelType type) {
75         channelTypes.add(type);
76     }
77
78     public void removeChannelType(ChannelType type) {
79         channelTypes.remove(type);
80     }
81
82     public void removeChannelTypesForThing(ThingUID uid) {
83         List<ChannelType> removes = new ArrayList<>();
84         for (ChannelType c : channelTypes) {
85             if (c.getUID().getAsString().startsWith(uid.getAsString())) {
86                 removes.add(c);
87             }
88         }
89         channelTypes.removeAll(removes);
90     }
91 }