diff --git a/src/main/java/envoy/client/LocalDB.java b/src/main/java/envoy/client/LocalDB.java index 5a06a02..d6a257a 100644 --- a/src/main/java/envoy/client/LocalDB.java +++ b/src/main/java/envoy/client/LocalDB.java @@ -26,8 +26,22 @@ public class LocalDB { private User sender; private List 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.
+ * + *@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.
+ * + *@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 getChats() { return chats; } }