Système de synchronisation de l'information entre FileFrontEnd<>StorBackEnd -> implémentation de AppData + ajout de la bibliothèque GSON à build.gradle
This commit is contained in:
parent
a1b3463d3b
commit
2f1f72b1fc
@ -22,6 +22,8 @@ dependencies {
|
||||
|
||||
// This dependency is used by the application.
|
||||
implementation 'com.google.guava:guava:30.1-jre'
|
||||
// Use gson to serialize/deserialize json files
|
||||
implementation 'com.google.code.gson:gson:2.9.0'
|
||||
}
|
||||
|
||||
application {
|
||||
|
132
app/src/main/java/lightcontainer/storage/AppData.java
Normal file
132
app/src/main/java/lightcontainer/storage/AppData.java
Normal file
@ -0,0 +1,132 @@
|
||||
package lightcontainer.storage;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* AppData represents the database of the FileFrontEnd program.
|
||||
* It contains an AppConfig instance and a collection of Users.
|
||||
*
|
||||
* @author Maximilien LEDOUX <m.ledoux@student.helmo.be>
|
||||
* @version 1.0
|
||||
* @see AppConfig
|
||||
* @see User
|
||||
* @since 1.0
|
||||
*/
|
||||
public class AppData {
|
||||
|
||||
private static AppData instance = null;
|
||||
private AppConfig appConfig;
|
||||
private final Map<String, User> users;
|
||||
|
||||
|
||||
/**
|
||||
* Constructs a new instance of AppData.
|
||||
* Sets appConfig to null and creates a new Hashmap of users.
|
||||
*/
|
||||
private AppData() {
|
||||
this.appConfig = null;
|
||||
this.users = new HashMap<>();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return An instance of this class. Always returns the same instance.
|
||||
*/
|
||||
public static AppData getInstance() {
|
||||
if (instance == null) {
|
||||
instance = new AppData();
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The AppConfig
|
||||
*/
|
||||
public AppConfig getAppConfig() {
|
||||
return appConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the AppConfig. This method sets the AppConfig for once and for all.
|
||||
* It is locked after first call.
|
||||
*
|
||||
* @param appConfig The network configuration of the program.
|
||||
*/
|
||||
public void setAppConfig(AppConfig appConfig) {
|
||||
if (this.appConfig == null) {
|
||||
this.appConfig = appConfig;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param userName The name of the user.
|
||||
* @return The user corresponding to userName, null otherwise.
|
||||
*/
|
||||
public User getUser(String userName) {
|
||||
return this.users.get(userName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Use this method when a user signs up.
|
||||
*
|
||||
* @param user The user to add.
|
||||
* @return True if the user was added. False if a user with the same name already exists.
|
||||
*/
|
||||
public boolean addUser(User user) {
|
||||
if (this.users.containsKey(user.getName())) {
|
||||
return false;
|
||||
} else {
|
||||
this.users.put(user.getName(), user);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param fileName The name of the file
|
||||
* @param user The user
|
||||
* @return The file corresponding to the given name and belonging to the user. Null if the user cannot be found or the file cannot be found
|
||||
* @deprecated Maybe not useful. DO NOT USE FOR THE TIME BEING
|
||||
*/
|
||||
public File getFileOf(String fileName, User user) {
|
||||
return this.users.get(user.getName()).getFile(fileName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Call this method after receiving SAVEFILE_OK from the StorBackEnd.
|
||||
* Do NOT call when receiving SAVEFILE_ERROR, or it will break the system's synchronization.
|
||||
* <p>
|
||||
* Adds the file of for a specific user.
|
||||
* True indicates the success of the operation.
|
||||
* False indicates the failure of the operation.
|
||||
*
|
||||
* @param file The file to add
|
||||
* @param user The user who wants to add the file
|
||||
* @return True if the user is found and a file with the same name doesn't already exist for this user. False otherwise.
|
||||
*/
|
||||
public boolean addFileFor(File file, User user) {
|
||||
if (!this.users.containsKey(user.getName())) {
|
||||
return false;
|
||||
} else {
|
||||
return this.users.get(user.getName()).addFile(file);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Call this method after receiving REMOVEFILE_OK from the StorBackEnd.
|
||||
* Do NOT call when receiving REMOVEFILE_ERROR, or it will break the system's synchronization.
|
||||
* Deletes the file of for a specific user.
|
||||
* True indicates the success of the operation.
|
||||
* False indicates the failure of the operation.
|
||||
*
|
||||
* @param fileName The name of the file to delete
|
||||
* @param user The user who wants to delete the file
|
||||
* @return True if the user is found and the file was deleted. False otherwise.
|
||||
*/
|
||||
public boolean deleteFileOf(String fileName, User user) {
|
||||
if (!this.users.containsKey(user.getName())) {
|
||||
return false;
|
||||
} else {
|
||||
return this.users.get(user.getName()).deleteFile(fileName);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user