me.redtea.carcadex.data.repo.impl.map.MapRepo Maven / Gradle / Ivy
package me.redtea.carcadex.data.repo.impl.map;
import me.redtea.carcadex.data.repo.Repo;
import org.jetbrains.annotations.NotNull;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.stream.Collectors;
/**
* Reload container:
* data - Map.class
* @param key
* @param value
*/
public abstract class MapRepo implements Repo {
protected Map data;
public MapRepo() {
this(new HashMap<>());
}
public MapRepo(Map defaults) {
data = defaults;
}
@Override
public Collection all() {
return data.values();
}
@Override
public Optional get(@NotNull K key) {
return Optional.ofNullable(data.get(key));
}
@Override
public void close() {
data.clear();
}
@Override
public Collection find(Predicate predicate) {
return all().parallelStream().filter(predicate).collect(Collectors.toList());
}
@Override
public Optional findAny(Predicate predicate) {
return all().parallelStream().filter(predicate).findAny();
}
}