Updated Javadoc for LocalDB File

This commit is contained in:
delvh 2019-10-30 08:10:40 +01:00 committed by GitHub
parent 236564c32d
commit 043219135f
1 changed files with 32 additions and 0 deletions

View File

@ -26,8 +26,22 @@ public class LocalDB {
private User sender;
private List<Chat> chats = new ArrayList<>();
/**
*Constructs an empty local database.
*
*@param sender the user who logs in
*@since Envoy v0.1-alpha
**/
public LocalDB(User sender) { this.sender = sender; }
/**
*Initialises the local database and fills it with values
*if the user already sent/ received a message.<br>
*
*@param localDBDir the directory where we wish to save/load the database from.
*@throws EnvoyException if the directory selected is not an actual directory.
*@since Envoy v0.1-alpha
**/
public void initializeDBFile(File localDBDir) throws EnvoyException {
if (localDBDir.exists() && !localDBDir.isDirectory())
throw new EnvoyException(String.format("LocalDBDir '%s' is not a directory!", localDBDir.getAbsolutePath()));
@ -35,20 +49,34 @@ public class LocalDB {
if (localDB.exists()) loadFromLocalDB();
}
/**
*Saves the database into a file for future use.
*
*@throws IOException if something went wrong during saving
*@since Envoy v0.1-alpha
**/
public void saveToLocalDB() {
try {
localDB.getParentFile().mkdirs();
localDB.createNewFile();
} catch (IOException e) {
e.printStackTrace();
System.err.println("unable to save the messages");
}
try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(localDB))) {
out.writeObject(chats);
} catch (IOException ex) {
ex.printStackTrace();
System.err.println("unable to save the messages");
}
}
/**
*Loads all chats saved by Envoy for the user.<br>
*
*@throws EnvoyException if something fails while loading.
*@since Envoy v0.1-alpha
**/
@SuppressWarnings("unchecked")
private void loadFromLocalDB() throws EnvoyException {
try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(localDB))) {
@ -59,5 +87,9 @@ public class LocalDB {
}
}
/**
*@return all chats that the user has saved
*@since Envoy v0.1-alpha
**/
public List<Chat> getChats() { return chats; }
}