Enhance DBContext with a single variable getter, pushes forward #425

This commit is contained in:
Abbas Abou Daya 2018-05-27 04:16:19 -04:00
parent ce1b0402ac
commit 714857555d
5 changed files with 98 additions and 0 deletions

View File

@ -39,6 +39,13 @@ public interface DBContext extends Closeable {
*/ */
<T> Set<T> getSet(String name); <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. * @return a high-level summary of the database structures (Sets, Lists, Maps, ...) present.
*/ */

View File

@ -3,6 +3,7 @@ package org.telegram.abilitybots.api.db;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import org.mapdb.Atomic;
import org.mapdb.DB; import org.mapdb.DB;
import org.mapdb.DBMaker; import org.mapdb.DBMaker;
import org.mapdb.Serializer; import org.mapdb.Serializer;
@ -93,6 +94,11 @@ public class MapDBContext implements DBContext {
return (Set<T>) db.<T>hashSet(name, JAVA).createOrOpen(); 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 @Override
public String summary() { public String summary() {
return stream(db.getAllNames().spliterator(), false) return stream(db.getAllNames().spliterator(), false)

View File

@ -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();
}
}

View File

@ -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);
}

View File

@ -100,6 +100,23 @@ public class MapDBContextTest {
db.info(TEST); 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 @After
public void tearDown() throws IOException { public void tearDown() throws IOException {
db.clear(); db.clear();