Package com.tagtraum.core
Class JsonDB<T>
- java.lang.Object
-
- com.tagtraum.core.JsonDB<T>
-
public class JsonDB<T> extends Object
Very simple JSON-based store for beans with only strings, booleans and numbers as well as simple Lists, Sets and Maps of those types as property values. This DB always simply appends a line at the end, when an bean is added. Therefore adding is very cheap.- Author:
- Hendrik Schreiber
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
JsonDB.ExceptionListener
Simple exception listener to handle problems while parsing a individual bean.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
add(T bean)
Adds a bean to the DB.void
addAll(Collection<T> beans)
Bulk adds beans to the DB.void
clear()
Deletes the database file.JsonDB.ExceptionListener
getExceptionListener()
Path
getFile()
void
read(Collection<T> collection)
Adds all elements to the given collection.boolean
remove(Object bean)
Removes an object from the database.void
setExceptionListener(JsonDB.ExceptionListener exceptionListener)
-
-
-
Constructor Detail
-
JsonDB
public JsonDB(Path file)
JsonDb, creates the database file, if it does not exist yet.- Parameters:
file
- database file
-
-
Method Detail
-
getFile
public Path getFile()
-
getExceptionListener
public JsonDB.ExceptionListener getExceptionListener()
-
setExceptionListener
public void setExceptionListener(JsonDB.ExceptionListener exceptionListener)
-
read
public void read(Collection<T> collection) throws IOException
Adds all elements to the given collection.- Parameters:
collection
- collection- Throws:
IOException
-
add
public void add(T bean) throws IOException
Adds a bean to the DB.- Parameters:
bean
- bean- Throws:
IOException
-
addAll
public void addAll(Collection<T> beans) throws IOException
Bulk adds beans to the DB.- Parameters:
beans
- beans- Throws:
IOException
-
remove
public boolean remove(Object bean) throws IOException
Removes an object from the database. Note that we do not compare withObject.equals(Object)
, but simply compare the JSON strings.- Parameters:
bean
- bean- Returns:
- true, if the bean was found and removed
- Throws:
IOException
-
clear
public void clear() throws IOException
Deletes the database file.- Throws:
IOException
-
-