]> git.basschouten.com Git - openhab-addons.git/blob
37eebd23affd627f609077a9ea82829b5e0a8ac9
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.nobohub.internal.model;
14
15 import java.util.Collection;
16 import java.util.HashMap;
17 import java.util.Map;
18
19 import javax.validation.constraints.NotNull;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23
24 /**
25  * Stores a mapping between component ids and components that exists.
26  *
27  * @author Jørgen Austvik - Initial contribution
28  * @author Espen Fossen - Initial contribution
29  */
30 @NonNullByDefault
31 public final class ComponentRegister {
32
33     private final @NotNull Map<SerialNumber, Component> register = new HashMap<SerialNumber, Component>();
34
35     /**
36      * Stores a new Component in the register. If a component exists with the same id, that value is overwritten.
37      *
38      * @param component The Component to store.
39      */
40     public void put(Component component) {
41         register.put(component.getSerialNumber(), component);
42     }
43
44     /**
45      * Removes a component from the registry.
46      *
47      * @param componentId The component to remove
48      * @return The component that is removed. Null if the component is not found.
49      */
50     public @Nullable Component remove(SerialNumber componentId) {
51         return register.remove(componentId);
52     }
53
54     /**
55      * Returns a component from the registry.
56      *
57      * @param componentId The id of the component to return.
58      * @return Returns the component, or null if it doesn't exist in the regestry.
59      */
60     public @Nullable Component get(SerialNumber componentId) {
61         return register.get(componentId);
62     }
63
64     public Collection<Component> values() {
65         return register.values();
66     }
67 }