67 lines
1.7 KiB
Java
67 lines
1.7 KiB
Java
package envoy.client.data;
|
|
|
|
import java.io.Serializable;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
|
|
/**
|
|
* Stores a heterogeneous map of {@link Cache} objects with different type
|
|
* parameters.
|
|
* <p>
|
|
* Project: <strong>envoy-client</strong><br>
|
|
* File: <strong>CacheMap.java</strong><br>
|
|
* Created: <strong>09.07.2020</strong><br>
|
|
*
|
|
* @author Kai S. K. Engelbart
|
|
* @since Envoy Client v0.1-beta
|
|
*/
|
|
public final class CacheMap implements Serializable {
|
|
|
|
private final Map<Class<?>, Cache<?>> map = new HashMap<>();
|
|
|
|
private static final long serialVersionUID = 1L;
|
|
|
|
/**
|
|
* Adds a cache to the map.
|
|
*
|
|
* @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.
|
|
*
|
|
* @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); }
|
|
|
|
/**
|
|
* Returns a cache mapped by a class or any of its subclasses.
|
|
*
|
|
* @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)
|
|
for (var e : map.entrySet())
|
|
if (e.getKey().isAssignableFrom(key))
|
|
cache = (Cache<? super T>) e.getValue();
|
|
return cache;
|
|
}
|
|
|
|
/**
|
|
* @return the map in which the caches are stored
|
|
* @since Envoy Client v0.1-beta
|
|
*/
|
|
public Map<Class<?>, Cache<?>> getMap() { return map; }
|
|
}
|