]> git.basschouten.com Git - openhab-addons.git/blob
10c77b5712ac542980deb355e56e3417d5b31145
[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.networkupstools.internal;
14
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.HashMap;
18 import java.util.Locale;
19 import java.util.Map;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.core.thing.binding.ThingHandler;
24 import org.openhab.core.thing.binding.ThingHandlerService;
25 import org.openhab.core.thing.type.ChannelType;
26 import org.openhab.core.thing.type.ChannelTypeProvider;
27 import org.openhab.core.thing.type.ChannelTypeUID;
28
29 /**
30  * Provider class to provide channel types for user configured channels.
31  *
32  * @author Hilbrand Bouwkamp - Initial contribution
33  */
34 @NonNullByDefault
35 public class NUTChannelTypeProvider implements ChannelTypeProvider, ThingHandlerService {
36
37     private final Map<ChannelTypeUID, ChannelType> map = new HashMap<>();
38     private @Nullable ThingHandler handler;
39
40     @Override
41     public Collection<ChannelType> getChannelTypes(@Nullable final Locale locale) {
42         return Collections.unmodifiableCollection(map.values());
43     }
44
45     @Override
46     public @Nullable ChannelType getChannelType(final ChannelTypeUID channelTypeUID, @Nullable final Locale locale) {
47         return map.get(channelTypeUID);
48     }
49
50     /**
51      * Add a channel type for a user configured channel.
52      *
53      * @param channelType channelType
54      */
55     public void addChannelType(final ChannelType channelType) {
56         map.put(channelType.getUID(), channelType);
57     }
58
59     @Override
60     public void setThingHandler(@Nullable final ThingHandler handler) {
61         if (handler instanceof NUTHandler nutHandler) {
62             this.handler = handler;
63             nutHandler.setChannelTypeProvider(this);
64         }
65     }
66
67     @Override
68     public @Nullable ThingHandler getThingHandler() {
69         return handler;
70     }
71 }