org.uqbar.commons.utils.MemoryStorage Maven / Gradle / Ivy
package org.uqbar.commons.utils;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Predicate;
public class MemoryStorage implements Storage {
private Map, List>> businessObjects = new HashMap, List>>();
@Override
public void add(Class type, T object) {
this.getObjects(type).add(object);
}
@Override
public void remove(Class type, T object) {
this.getObjects(type).remove(object);
}
@Override
@SuppressWarnings("unchecked")
public List getObjects(Class type) {
List list = (List) this.businessObjects.get(type);
if (list == null) {
list = new ArrayList();
this.businessObjects.put(type, list);
}
return list;
}
public void setObjects(Class type, List objects) {
this.businessObjects.put(type, objects);
}
@Override
@SuppressWarnings("unchecked")
public Collection getObjects(Class type, Predicate predicate) {
return CollectionUtils.select(getObjects(type), predicate);
}
}