All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.krrrr38.jabot.plugin.brain.InmemoryBrain Maven / Gradle / Ivy
package com.krrrr38.jabot.plugin.brain;
import java.util.Collections;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
public final class InmemoryBrain extends Brain {
private Map> brain;
@Override
protected void build(Map options) throws JabotBrainException {
brain = new ConcurrentHashMap<>();
}
@Override
public Map getAll(String namespace) throws JabotBrainException {
return space(namespace);
}
@Override
public Optional get(String namespace, String key) throws JabotBrainException {
return Optional.ofNullable(space(namespace).get(key));
}
@Override
public boolean store(String namespace, String key, String value) throws JabotBrainException {
brain.compute(namespace, (namespace1, space) -> {
if (space == null) {
space = new ConcurrentHashMap<>();
}
space.put(key, value);
return space;
});
return true;
}
@Override
public boolean store(String namespace, String key1, String value1, String key2, String value2) throws JabotBrainException {
store(namespace, key1, value1);
store(namespace, key2, value2);
return true;
}
@Override
public boolean store(String namespace, String key1, String value1, String key2, String value2, String key3, String value3) throws JabotBrainException {
store(namespace, key1, value1);
store(namespace, key2, value2);
store(namespace, key3, value3);
return true;
}
@Override
public boolean storeAll(String namespace, Map keyvalues) throws JabotBrainException {
brain.compute(namespace, (namespace1, space) -> {
if (space == null) {
space = new ConcurrentHashMap<>();
}
space.putAll(keyvalues);
return space;
});
return true;
}
@Override
public boolean delete(String namespace, String key) throws JabotBrainException {
return space(namespace).remove(key) != null;
}
@Override
public boolean clear(String namespace) throws JabotBrainException {
brain.put(namespace, new ConcurrentHashMap<>());
return true;
}
@Override
public boolean isStored(String namespace, String key) throws JabotBrainException {
return space(namespace).containsKey(key);
}
private Map space(String namespace) {
return brain.getOrDefault(namespace, Collections.emptyMap());
}
}