]> git.basschouten.com Git - openhab-addons.git/blob
cccc18ffdb30abfafa53499cf93caf873ae3acac
[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.mielecloud.internal.webservice;
14
15 import java.util.HashMap;
16 import java.util.Map;
17 import java.util.Optional;
18 import java.util.Set;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.openhab.binding.mielecloud.internal.webservice.api.json.Device;
22 import org.openhab.binding.mielecloud.internal.webservice.api.json.DeviceCollection;
23
24 /**
25  * A cache for {@link Device} objects associated with unique identifiers.
26  *
27  * @author Björn Lange - Initial contribution
28  */
29 @NonNullByDefault
30 class DeviceCache {
31     private final Map<String, Device> entries = new HashMap<>();
32
33     public void replaceAllDevices(DeviceCollection deviceCollection) {
34         clear();
35         deviceCollection.getDeviceIdentifiers().stream().forEach(i -> entries.put(i, deviceCollection.getDevice(i)));
36     }
37
38     public void clear() {
39         entries.clear();
40     }
41
42     public Set<String> getDeviceIds() {
43         return entries.keySet();
44     }
45
46     public Optional<Device> getDevice(String deviceIdentifier) {
47         return Optional.ofNullable(entries.get(deviceIdentifier));
48     }
49 }