diff --git a/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/db/DBContext.java b/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/db/DBContext.java index eda5e9c1..cd25683b 100644 --- a/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/db/DBContext.java +++ b/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/db/DBContext.java @@ -39,6 +39,13 @@ public interface DBContext extends Closeable { */ Set getSet(String name); + /** + * @param name the unique name of the {@link Set} + * @param the type that the Set holds + * @return the Set with the specified name + */ + Var getVar(String name); + /** * @return a high-level summary of the database structures (Sets, Lists, Maps, ...) present. */ diff --git a/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/db/MapDBContext.java b/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/db/MapDBContext.java index e9d2efd6..c7e5b018 100644 --- a/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/db/MapDBContext.java +++ b/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/db/MapDBContext.java @@ -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) db.hashSet(name, JAVA).createOrOpen(); } + @Override + public Var getVar(String name) { + return new MapDBVar<>((Atomic.Var) db.atomicVar(name).createOrOpen()); + } + @Override public String summary() { return stream(db.getAllNames().spliterator(), false) diff --git a/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/db/MapDBVar.java b/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/db/MapDBVar.java new file mode 100644 index 00000000..a325fd36 --- /dev/null +++ b/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/db/MapDBVar.java @@ -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 the type of the inner variable + */ +public final class MapDBVar implements Var { + private Atomic.Var var; + + public MapDBVar(Atomic.Var 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(); + } +} diff --git a/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/db/Var.java b/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/db/Var.java new file mode 100644 index 00000000..b6ec4593 --- /dev/null +++ b/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/db/Var.java @@ -0,0 +1,19 @@ +package org.telegram.abilitybots.api.db; + +/** + * The interface governing a variable for abstract getters and setters. + * @param the type of the variable + * + * @author Abbas Abou Daya + */ +public interface Var { + /** + * @return the variable contained + */ + T get(); + + /** + * @param var the new variable value + */ + void set(T var); +} diff --git a/telegrambots-abilities/src/test/java/org/telegram/abilitybots/api/db/MapDBContextTest.java b/telegrambots-abilities/src/test/java/org/telegram/abilitybots/api/db/MapDBContextTest.java index 502f70a1..0f85ce36 100644 --- a/telegrambots-abilities/src/test/java/org/telegram/abilitybots/api/db/MapDBContextTest.java +++ b/telegrambots-abilities/src/test/java/org/telegram/abilitybots/api/db/MapDBContextTest.java @@ -100,6 +100,23 @@ public class MapDBContextTest { db.info(TEST); } + @Test + public void canGetAndSetVariables() { + String varName = "somevar"; + Var var = db.getVar(varName); + var.set(CREATOR); + db.commit(); + + var = db.getVar(varName); + assertEquals(var.get(), CREATOR); + + var.set(USER); + db.commit(); + + Var changedVar = db.getVar(varName); + assertEquals(changedVar.get(), USER); + } + @After public void tearDown() throws IOException { db.clear();