Paste: Generic inner interface
Author: | Slade |
Mode: | java |
Date: | Tue, 8 Sep 2015 16:59:37 |
Plain Text |
public class ObjectCache<T> extends HashMap<String, T> {
private final Factory factory;
public interface Factory<T> {
T getObject(String key);
}
public ObjectCache(Factory factory) {
this.factory = factory;
}
@Override
public T get(Object key) {
if (!(key instanceof String))
throw new IllegalArgumentException("Key must be a string");
String search = (String)key;
if (search.isEmpty())
return null;
T obj = super.get(key);
if (null == obj) {
obj = factory.getObject(search);
if (null != obj)
put(search, obj);
}
return obj;
}
}
New Annotation