]> git.basschouten.com Git - openhab-addons.git/blob
a7e53567e2214a7f71e6018ad93f1c155afba031
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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         thingTable.put(key, thing);
35     }
36
37     public ShellyThingInterface getThing(String key) {
38         ShellyThingInterface t = thingTable.get(key);
39         if (t != null) {
40             return t;
41         }
42         for (Map.Entry<String, ShellyThingInterface> entry : thingTable.entrySet()) {
43             t = entry.getValue();
44             if (t.checkRepresentation(key)) {
45                 return t;
46             }
47         }
48         throw new IllegalArgumentException();
49     }
50
51     public void removeThing(String key) {
52         if (thingTable.containsKey(key)) {
53             thingTable.remove(key);
54         }
55     }
56
57     public Map<String, ShellyThingInterface> getTable() {
58         return thingTable;
59     }
60
61     public int size() {
62         return thingTable.size();
63     }
64 }