2020-07-09 09:37:31 +02:00
|
|
|
package envoy.client.data;
|
|
|
|
|
2020-07-09 10:53:27 +02:00
|
|
|
import java.io.Serializable;
|
2020-09-26 21:38:31 +02:00
|
|
|
import java.util.*;
|
2020-07-09 09:37:31 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Stores a heterogeneous map of {@link Cache} objects with different type
|
|
|
|
* parameters.
|
2020-09-26 21:38:31 +02:00
|
|
|
*
|
2020-07-09 09:37:31 +02:00
|
|
|
* @author Kai S. K. Engelbart
|
|
|
|
* @since Envoy Client v0.1-beta
|
|
|
|
*/
|
2020-07-09 10:53:27 +02:00
|
|
|
public final class CacheMap implements Serializable {
|
2020-07-09 09:37:31 +02:00
|
|
|
|
|
|
|
private final Map<Class<?>, Cache<?>> map = new HashMap<>();
|
|
|
|
|
2020-07-09 10:53:27 +02:00
|
|
|
private static final long serialVersionUID = 1L;
|
|
|
|
|
2020-07-09 09:37:31 +02:00
|
|
|
/**
|
|
|
|
* Adds a cache to the map.
|
2020-09-26 21:38:31 +02:00
|
|
|
*
|
2020-07-09 09:37:31 +02:00
|
|
|
* @param <T> the type accepted by the cache
|
|
|
|
* @param key the class that maps to the cache
|
|
|
|
* @param cache the cache to store
|
|
|
|
* @since Envoy Client v0.1-beta
|
|
|
|
*/
|
|
|
|
public <T> void put(Class<T> key, Cache<T> cache) { map.put(key, cache); }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a cache mapped by a class.
|
2020-09-26 21:38:31 +02:00
|
|
|
*
|
2020-07-09 09:37:31 +02:00
|
|
|
* @param <T> the type accepted by the cache
|
|
|
|
* @param key the class that maps to the cache
|
|
|
|
* @return the cache
|
|
|
|
* @since Envoy Client v0.1-beta
|
|
|
|
*/
|
|
|
|
public <T> Cache<T> get(Class<T> key) { return (Cache<T>) map.get(key); }
|
|
|
|
|
2020-07-09 10:53:27 +02:00
|
|
|
/**
|
|
|
|
* Returns a cache mapped by a class or any of its subclasses.
|
2020-09-26 21:38:31 +02:00
|
|
|
*
|
2020-07-09 10:53:27 +02:00
|
|
|
* @param <T> the type accepted by the cache
|
|
|
|
* @param key the class that maps to the cache
|
|
|
|
* @return the cache
|
|
|
|
* @since Envoy Client v0.1-beta
|
|
|
|
*/
|
|
|
|
public <T> Cache<? super T> getApplicable(Class<T> key) {
|
|
|
|
Cache<? super T> cache = get(key);
|
|
|
|
if (cache == null)
|
2020-09-27 15:20:19 +02:00
|
|
|
for (final var e : map.entrySet())
|
2020-07-09 10:53:27 +02:00
|
|
|
if (e.getKey().isAssignableFrom(key))
|
|
|
|
cache = (Cache<? super T>) e.getValue();
|
|
|
|
return cache;
|
|
|
|
}
|
|
|
|
|
2020-07-09 09:37:31 +02:00
|
|
|
/**
|
|
|
|
* @return the map in which the caches are stored
|
|
|
|
* @since Envoy Client v0.1-beta
|
|
|
|
*/
|
|
|
|
public Map<Class<?>, Cache<?>> getMap() { return map; }
|
2020-09-26 21:38:31 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Clears the caches of this map of any values.
|
|
|
|
*
|
|
|
|
* @since Envoy Client v0.2-beta
|
|
|
|
*/
|
|
|
|
public void clear() { map.values().forEach(Cache::clear); }
|
2020-07-09 09:37:31 +02:00
|
|
|
}
|