Merge from ECOMP's repository
[vid.git] / vid-app-common / src / main / java / org / onap / vid / aai / util / CacheProvider.java
1 package org.onap.vid.aai.util;
2
3 import java.util.List;
4 import java.util.function.Function;
5 import java.util.stream.Collectors;
6 import java.util.stream.Stream;
7
8 import static org.apache.commons.lang3.ObjectUtils.defaultIfNull;
9
10 public interface CacheProvider {
11     String KEY_DELIMITER = "!@#'";
12     /*
13     Returns the cache associated with given name; creates one if wasn't any
14     */
15     <K, V> Cache<K, V> aaiClientCacheFor(String name, Function<K, V> loader);
16
17     /*
18     reset cache if exist. Otherwise do nothing
19      */
20     void resetCache(String name);
21
22     interface Cache<K, V> {
23         V get(K key);
24     }
25
26     static String compileKey(List<String> args) {
27         return compileKey(args.toArray(new String[0]));
28     }
29
30     static String compileKey(String... args) {
31         return Stream.of(args).map(arg->defaultIfNull(arg, "")).collect( Collectors.joining( KEY_DELIMITER ) );
32     }
33
34     static String[] decompileKey(String key) {
35         return key.split(KEY_DELIMITER);
36     }
37 }