Commit e1dfa870 authored by Riegel's avatar Riegel
Browse files

Feat: Add cache for CityObjects

parent ef413298
package de.hft.stuttgart.citydoctor2.database;
import de.hft.stuttgart.citydoctor2.datastructure.CityObject;
import de.hft.stuttgart.citydoctor2.datastructure.GmlId;
import java.util.List;
import java.util.Map;
import java.lang.ref.SoftReference;
import java.util.concurrent.ConcurrentHashMap;
/**
* Soft-reference cache for
*/
public class CityObjectCache {
private final Map<GmlId, SoftReference<CityObject>> cache = new ConcurrentHashMap<>();
public void put(GmlId id, CityObject cityObject) {
cache.put(id, new SoftReference<>(cityObject));
}
public void cache(List<? extends CityObject> objects) {
}
public CityObject get(GmlId id) {
SoftReference<CityObject> ref = cache.get(id);
if (ref == null) {
return null;
}
CityObject cityObject = ref.get();
if (cityObject == null) {
cityObject = DatabaseHandler.unmarshallCityObject(id);
if (cityObject == null) {
cache.remove(id);
} else {
this.put(id, cityObject);
}
}
return cityObject;
}
public void remove(GmlId id) {
cache.remove(id);
}
public void clear() {
cache.clear();
}
public int size() {
return cache.size();
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment