]> git.basschouten.com Git - openhab-addons.git/blob
de7cec15d350d0e65c20b5b2e481a5ce95dcc748
[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.unifi.internal.api.cache;
14
15 import java.util.Collection;
16 import java.util.HashMap;
17 import java.util.Map;
18 import java.util.stream.Collectors;
19
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 /**
24  * The {@link UniFiCache} is a specialised lookup table that stores objects using multiple keys in the form
25  * <code>prefix:suffix</code>. Each implementation is responsible for providing a list of supported prefixes and must
26  * implement {@link #getSuffix(Object, String)} to provide a value specific suffix derived from the prefix.
27  *
28  * Objects are then retrieved simply by using the <code>suffix</code> key component and all combinations of
29  * <code>prefix:suffix</code> are searched in the order of their priority.
30  *
31  * @author Matthew Bowman - Initial contribution
32  */
33 public abstract class UniFiCache<T> {
34
35     private static final String SEPARATOR = ":";
36
37     public static final String PREFIX_ALIAS = "alias";
38
39     public static final String PREFIX_DESC = "desc";
40
41     public static final String PREFIX_HOSTNAME = "hostname";
42
43     public static final String PREFIX_ID = "id";
44
45     public static final String PREFIX_IP = "ip";
46
47     public static final String PREFIX_MAC = "mac";
48
49     public static final String PREFIX_NAME = "name";
50
51     private final Logger logger = LoggerFactory.getLogger(getClass());
52
53     private Map<String, T> map = new HashMap<>();
54
55     private String[] prefixes;
56
57     protected UniFiCache(String... prefixes) {
58         this.prefixes = prefixes;
59     }
60
61     public final T get(Object id) {
62         T value = null;
63         for (String prefix : prefixes) {
64             String key = prefix + SEPARATOR + id;
65             if (map.containsKey(key)) {
66                 value = map.get(key);
67                 logger.trace("Cache HIT : '{}' -> {}", key, value);
68                 break;
69             } else {
70                 logger.trace("Cache MISS : '{}'", key);
71             }
72         }
73         return value;
74     }
75
76     public final void put(T value) {
77         for (String prefix : prefixes) {
78             String suffix = getSuffix(value, prefix);
79             if (suffix != null && !suffix.isBlank()) {
80                 String key = prefix + SEPARATOR + suffix;
81                 map.put(key, value);
82             }
83         }
84     }
85
86     public final void putAll(UniFiCache<T> cache) {
87         map.putAll(cache.map);
88     }
89
90     public final Collection<T> values() {
91         return map.values().stream().distinct().collect(Collectors.toList());
92     }
93
94     protected abstract String getSuffix(T value, String prefix);
95 }