]> git.basschouten.com Git - openhab-addons.git/blob
2719b80403cf965cf557e905a050828d40a96b5c
[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.shelly.internal.handler;
14
15 import java.util.Map;
16 import java.util.concurrent.ConcurrentHashMap;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.osgi.service.component.annotations.Component;
20 import org.osgi.service.component.annotations.ConfigurationPolicy;
21
22 /***
23  * The{@link ShellyThingTable} implements a simple table to allow dispatching incoming events to the proper thing
24  * handler
25  *
26  * @author Markus Michels - Initial contribution
27  */
28 @NonNullByDefault
29 @Component(service = ShellyThingTable.class, configurationPolicy = ConfigurationPolicy.OPTIONAL)
30 public class ShellyThingTable {
31     private Map<String, ShellyThingInterface> thingTable = new ConcurrentHashMap<>();
32
33     public void addThing(String key, ShellyThingInterface thing) {
34         if (thingTable.containsKey(key)) {
35             thingTable.remove(key);
36         }
37         thingTable.put(key, thing);
38     }
39
40     public ShellyThingInterface getThing(String key) {
41         ShellyThingInterface t = thingTable.get(key);
42         if (t != null) {
43             return t;
44         }
45         for (Map.Entry<String, ShellyThingInterface> entry : thingTable.entrySet()) {
46             t = entry.getValue();
47             if (t.checkRepresentation(key)) {
48                 return t;
49             }
50         }
51         throw new IllegalArgumentException();
52     }
53
54     public void removeThing(String key) {
55         if (thingTable.containsKey(key)) {
56             thingTable.remove(key);
57         }
58     }
59
60     public Map<String, ShellyThingInterface> getTable() {
61         return thingTable;
62     }
63
64     public int size() {
65         return thingTable.size();
66     }
67 }