]> git.basschouten.com Git - openhab-addons.git/blob
bb3740d3e903236ea6e93b719111ea72588c6cb7
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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,
38         KM200ChannelTypeProvider.class }, immediate = true)
39 @NonNullByDefault
40 public class KM200ChannelTypeProvider implements ChannelTypeProvider, ChannelGroupTypeProvider {
41     private final List<ChannelType> channelTypes = new CopyOnWriteArrayList<>();
42     private final List<ChannelGroupType> channelGroupTypes = new CopyOnWriteArrayList<>();
43
44     @Override
45     public Collection<ChannelType> getChannelTypes(@Nullable Locale locale) {
46         return channelTypes;
47     }
48
49     @Override
50     public @Nullable ChannelType getChannelType(ChannelTypeUID channelTypeUID, @Nullable Locale locale) {
51         for (ChannelType channelType : channelTypes) {
52             if (channelType.getUID().equals(channelTypeUID)) {
53                 return channelType;
54             }
55         }
56         return null;
57     }
58
59     @Override
60     public Collection<ChannelGroupType> getChannelGroupTypes(@Nullable Locale locale) {
61         return channelGroupTypes;
62     }
63
64     @Override
65     public @Nullable ChannelGroupType getChannelGroupType(ChannelGroupTypeUID channelGroupTypeUID,
66             @Nullable Locale locale) {
67         for (ChannelGroupType channelGroupType : channelGroupTypes) {
68             if (channelGroupType.getUID().equals(channelGroupTypeUID)) {
69                 return channelGroupType;
70             }
71         }
72         return null;
73     }
74
75     public void addChannelType(ChannelType type) {
76         channelTypes.add(type);
77     }
78
79     public void removeChannelType(ChannelType type) {
80         channelTypes.remove(type);
81     }
82
83     public void removeChannelTypesForThing(ThingUID uid) {
84         List<ChannelType> removes = new ArrayList<>();
85         for (ChannelType c : channelTypes) {
86             if (c.getUID().getAsString().startsWith(uid.getAsString())) {
87                 removes.add(c);
88             }
89         }
90         channelTypes.removeAll(removes);
91     }
92 }