2020-02-05 20:58:30 +01:00
|
|
|
package envoy.client.data;
|
2020-02-03 21:52:48 +01:00
|
|
|
|
|
|
|
import java.util.*;
|
|
|
|
|
2020-03-26 16:06:18 +01:00
|
|
|
import envoy.data.IDGenerator;
|
2020-02-06 21:03:08 +01:00
|
|
|
import envoy.data.Message;
|
2020-02-03 21:52:48 +01:00
|
|
|
import envoy.data.User;
|
2020-02-06 21:03:08 +01:00
|
|
|
import envoy.event.MessageStatusChangeEvent;
|
2020-02-03 21:52:48 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Stores information about the current {@link User} and their {@link Chat}s.
|
2020-03-26 16:06:18 +01:00
|
|
|
* For message ID generation a {@link IDGenerator} is stored as well.<br>
|
2020-02-03 21:52:48 +01:00
|
|
|
* <br>
|
|
|
|
* Project: <strong>envoy-client</strong><br>
|
2020-03-24 18:38:47 +01:00
|
|
|
* File: <strong>LocalDB.java</strong><br>
|
2020-02-03 21:52:48 +01:00
|
|
|
* Created: <strong>3 Feb 2020</strong><br>
|
|
|
|
*
|
|
|
|
* @author Kai S. K. Engelbart
|
2020-03-23 21:52:33 +01:00
|
|
|
* @since Envoy Client v0.3-alpha
|
2020-02-03 21:52:48 +01:00
|
|
|
*/
|
2020-03-24 18:38:47 +01:00
|
|
|
public abstract class LocalDB {
|
2020-02-03 21:52:48 +01:00
|
|
|
|
2020-02-06 21:03:08 +01:00
|
|
|
protected User user;
|
2020-03-14 16:58:19 +01:00
|
|
|
protected Map<String, User> users = new HashMap<>();
|
|
|
|
protected List<Chat> chats = new ArrayList<>();
|
2020-03-26 16:06:18 +01:00
|
|
|
protected IDGenerator idGenerator;
|
2020-02-06 21:42:17 +01:00
|
|
|
protected Cache<Message> messageCache = new Cache<>();
|
|
|
|
protected Cache<MessageStatusChangeEvent> statusCache = new Cache<>();
|
2020-02-03 21:52:48 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes a storage space for a user-specific list of chats.
|
|
|
|
*
|
2020-03-23 21:52:33 +01:00
|
|
|
* @since Envoy Client v0.3-alpha
|
2020-02-03 21:52:48 +01:00
|
|
|
*/
|
|
|
|
public void initializeUserStorage() {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stores all users. If the client user is specified, their chats will be stored
|
|
|
|
* as well. The message id generator will also be saved if present.
|
|
|
|
*
|
|
|
|
* @throws Exception if the saving process failed
|
2020-03-23 21:52:33 +01:00
|
|
|
* @since Envoy Client v0.3-alpha
|
2020-02-03 21:52:48 +01:00
|
|
|
*/
|
|
|
|
public void save() throws Exception {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads all user data.
|
|
|
|
*
|
|
|
|
* @throws Exception if the loading process failed
|
2020-03-23 21:52:33 +01:00
|
|
|
* @since Envoy Client v0.3-alpha
|
2020-02-03 21:52:48 +01:00
|
|
|
*/
|
|
|
|
public void loadUsers() throws Exception {}
|
|
|
|
|
|
|
|
/**
|
2020-02-06 21:42:17 +01:00
|
|
|
* Loads all data of the client user.
|
2020-02-03 21:52:48 +01:00
|
|
|
*
|
|
|
|
* @throws Exception if the loading process failed
|
2020-03-23 21:52:33 +01:00
|
|
|
* @since Envoy Client v0.3-alpha
|
2020-02-03 21:52:48 +01:00
|
|
|
*/
|
2020-02-06 21:42:17 +01:00
|
|
|
public void loadUserData() throws Exception {}
|
2020-02-03 21:52:48 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads the ID generator. Any exception thrown during this process is ignored.
|
|
|
|
*
|
2020-03-23 21:52:33 +01:00
|
|
|
* @since Envoy Client v0.3-alpha
|
2020-02-03 21:52:48 +01:00
|
|
|
*/
|
2020-03-26 16:06:18 +01:00
|
|
|
public void loadIDGenerator() {}
|
2020-02-03 21:52:48 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return a {@code Map<String, User>} of all users stored locally with their
|
|
|
|
* user names as keys
|
2020-03-23 21:52:33 +01:00
|
|
|
* @since Envoy Client v0.2-alpha
|
2020-02-03 21:52:48 +01:00
|
|
|
*/
|
|
|
|
public Map<String, User> getUsers() { return users; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param users the users to set
|
|
|
|
*/
|
|
|
|
public void setUsers(Map<String, User> users) { this.users = users; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return all saved {@link Chat} objects that list the client user as the
|
|
|
|
* sender
|
2020-03-23 21:52:33 +01:00
|
|
|
* @since Envoy Client v0.1-alpha
|
2020-02-03 21:52:48 +01:00
|
|
|
**/
|
|
|
|
public List<Chat> getChats() { return chats; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param chats the chats to set
|
|
|
|
*/
|
|
|
|
public void setChats(List<Chat> chats) { this.chats = chats; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return the {@link User} who initialized the local database
|
2020-03-23 21:52:33 +01:00
|
|
|
* @since Envoy Client v0.2-alpha
|
2020-02-03 21:52:48 +01:00
|
|
|
*/
|
|
|
|
public User getUser() { return user; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param user the user to set
|
2020-03-23 21:52:33 +01:00
|
|
|
* @since Envoy Client v0.2-alpha
|
2020-02-03 21:52:48 +01:00
|
|
|
*/
|
|
|
|
public void setUser(User user) { this.user = user; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return the message ID generator
|
2020-03-23 21:52:33 +01:00
|
|
|
* @since Envoy Client v0.3-alpha
|
2020-02-03 21:52:48 +01:00
|
|
|
*/
|
2020-03-26 16:06:18 +01:00
|
|
|
public IDGenerator getIDGenerator() { return idGenerator; }
|
2020-02-03 21:52:48 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param idGenerator the message ID generator to set
|
2020-03-23 21:52:33 +01:00
|
|
|
* @since Envoy Client v0.3-alpha
|
2020-02-03 21:52:48 +01:00
|
|
|
*/
|
2020-03-26 16:06:18 +01:00
|
|
|
public void setIDGenerator(IDGenerator idGenerator) { this.idGenerator = idGenerator; }
|
2020-02-03 21:52:48 +01:00
|
|
|
|
|
|
|
/**
|
2020-03-26 16:06:18 +01:00
|
|
|
* @return {@code true} if an {@link IDGenerator} is present
|
2020-03-23 21:52:33 +01:00
|
|
|
* @since Envoy Client v0.3-alpha
|
2020-02-03 21:52:48 +01:00
|
|
|
*/
|
2020-03-26 16:06:18 +01:00
|
|
|
public boolean hasIDGenerator() { return idGenerator != null; }
|
2020-02-06 21:03:08 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return the offline message cache
|
2020-03-23 21:52:33 +01:00
|
|
|
* @since Envoy Client v0.3-alpha
|
2020-02-06 21:03:08 +01:00
|
|
|
*/
|
|
|
|
public Cache<Message> getMessageCache() { return messageCache; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param messageCache the offline message cache to set
|
2020-03-23 21:52:33 +01:00
|
|
|
* @since Envoy Client v0.3-alpha
|
2020-02-06 21:03:08 +01:00
|
|
|
*/
|
|
|
|
public void setMessageCache(Cache<Message> messageCache) { this.messageCache = messageCache; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return the offline status cache
|
2020-03-23 21:52:33 +01:00
|
|
|
* @since Envoy Client v0.3-alpha
|
2020-02-06 21:03:08 +01:00
|
|
|
*/
|
|
|
|
public Cache<MessageStatusChangeEvent> getStatusCache() { return statusCache; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param statusCache the offline status cache to set
|
2020-03-23 21:52:33 +01:00
|
|
|
* @since Envoy Client v0.3-alpha
|
2020-02-06 21:03:08 +01:00
|
|
|
*/
|
|
|
|
public void setStatusCache(Cache<MessageStatusChangeEvent> statusCache) { this.statusCache = statusCache; }
|
2020-03-14 16:58:19 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Searches for a message by ID.
|
2020-03-26 16:06:18 +01:00
|
|
|
*
|
2020-03-14 16:58:19 +01:00
|
|
|
* @param id the ID of the message to search for
|
|
|
|
* @return the message with the corresponding ID, or {@code null} if no message
|
|
|
|
* has been found
|
2020-03-23 21:52:33 +01:00
|
|
|
* @since Envoy Client v0.1-beta
|
2020-03-14 16:58:19 +01:00
|
|
|
*/
|
|
|
|
public Message getMessage(long id) {
|
2020-03-29 22:15:05 +02:00
|
|
|
return chats.stream().map(Chat::getMessages).flatMap(List::stream).filter(m -> m.getID() == id).findAny().orElse(null);
|
2020-03-14 16:58:19 +01:00
|
|
|
}
|
2020-02-03 21:52:48 +01:00
|
|
|
}
|