Enhance DBContext with a single variable getter, pushes forward #425
This commit is contained in:
parent
ce1b0402ac
commit
714857555d
@ -39,6 +39,13 @@ public interface DBContext extends Closeable {
|
||||
*/
|
||||
<T> Set<T> getSet(String name);
|
||||
|
||||
/**
|
||||
* @param name the unique name of the {@link Set}
|
||||
* @param <T> the type that the Set holds
|
||||
* @return the Set with the specified name
|
||||
*/
|
||||
<T> Var<T> getVar(String name);
|
||||
|
||||
/**
|
||||
* @return a high-level summary of the database structures (Sets, Lists, Maps, ...) present.
|
||||
*/
|
||||
|
@ -3,6 +3,7 @@ package org.telegram.abilitybots.api.db;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.mapdb.Atomic;
|
||||
import org.mapdb.DB;
|
||||
import org.mapdb.DBMaker;
|
||||
import org.mapdb.Serializer;
|
||||
@ -93,6 +94,11 @@ public class MapDBContext implements DBContext {
|
||||
return (Set<T>) db.<T>hashSet(name, JAVA).createOrOpen();
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> Var<T> getVar(String name) {
|
||||
return new MapDBVar<>((Atomic.Var<T>) db.atomicVar(name).createOrOpen());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String summary() {
|
||||
return stream(db.getAllNames().spliterator(), false)
|
||||
|
@ -0,0 +1,49 @@
|
||||
package org.telegram.abilitybots.api.db;
|
||||
|
||||
import com.google.common.base.MoreObjects;
|
||||
import org.mapdb.Atomic;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* The MapDB variant for {@link DBContext#getVar(String)}.
|
||||
*
|
||||
* @param <T> the type of the inner variable
|
||||
*/
|
||||
public final class MapDBVar<T> implements Var<T> {
|
||||
private Atomic.Var<T> var;
|
||||
|
||||
public MapDBVar(Atomic.Var<T> var) {
|
||||
this.var = var;
|
||||
}
|
||||
|
||||
@Override
|
||||
public T get() {
|
||||
return var.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void set(T var) {
|
||||
this.var.set(var);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
MapDBVar<?> mapDBVar = (MapDBVar<?>) o;
|
||||
return Objects.equals(var, mapDBVar.var);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(var);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return MoreObjects.toStringHelper(this)
|
||||
.add("var", var)
|
||||
.toString();
|
||||
}
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
package org.telegram.abilitybots.api.db;
|
||||
|
||||
/**
|
||||
* The interface governing a variable for abstract getters and setters.
|
||||
* @param <T> the type of the variable
|
||||
*
|
||||
* @author Abbas Abou Daya
|
||||
*/
|
||||
public interface Var<T> {
|
||||
/**
|
||||
* @return the variable contained
|
||||
*/
|
||||
T get();
|
||||
|
||||
/**
|
||||
* @param var the new variable value
|
||||
*/
|
||||
void set(T var);
|
||||
}
|
@ -100,6 +100,23 @@ public class MapDBContextTest {
|
||||
db.info(TEST);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void canGetAndSetVariables() {
|
||||
String varName = "somevar";
|
||||
Var<User> var = db.getVar(varName);
|
||||
var.set(CREATOR);
|
||||
db.commit();
|
||||
|
||||
var = db.getVar(varName);
|
||||
assertEquals(var.get(), CREATOR);
|
||||
|
||||
var.set(USER);
|
||||
db.commit();
|
||||
|
||||
Var<User> changedVar = db.getVar(varName);
|
||||
assertEquals(changedVar.get(), USER);
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws IOException {
|
||||
db.clear();
|
||||
|
Loading…
Reference in New Issue
Block a user