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