Renamed classes with a two-letter initialism according to convention

This commit is contained in:
delvh 2020-03-24 18:38:47 +01:00
parent c1087a8c6a
commit 5c2abe7c1c
10 changed files with 79 additions and 77 deletions

File diff suppressed because one or more lines are too long

View File

@ -81,15 +81,15 @@ public class Startup {
EnvoyLog.setConsoleLevelBarrier(config.getConsoleLevelBarrier());
// Initialize the local database
LocalDb localDb;
LocalDB localDB;
if (config.isIgnoreLocalDB()) {
localDb = new TransientLocalDb();
localDB = new TransientLocalDB();
JOptionPane.showMessageDialog(null,
"Ignoring local database.\nMessages will not be saved!",
"Local database warning",
JOptionPane.WARNING_MESSAGE);
} else try {
localDb = new PersistentLocalDb(new File(config.getHomeDirectory(), config.getLocalDB().getPath()));
localDB = new PersistentLocalDB(new File(config.getHomeDirectory(), config.getLocalDB().getPath()));
} catch (IOException e3) {
logger.log(Level.SEVERE, "Could not initialize local database", e3);
JOptionPane.showMessageDialog(null, "Could not initialize local database!\n" + e3, "Local database error", JOptionPane.ERROR_MESSAGE);
@ -102,16 +102,16 @@ public class Startup {
Cache<Message> cache = new Cache<>();
// Try to connect to the server
new LoginDialog(client, localDb, cache);
new LoginDialog(client, localDB, cache);
SwingUtilities.invokeLater(() -> chatWindow.setVisible(true));
// Set client user in local database
localDb.setUser(client.getSender());
localDB.setUser(client.getSender());
// Initialize chats in local database
try {
localDb.initializeUserStorage();
localDb.loadUserData();
localDB.initializeUserStorage();
localDB.loadUserData();
} catch (FileNotFoundException e) {
// The local database file has not yet been created, probably first login
} catch (Exception e) {
@ -123,21 +123,21 @@ public class Startup {
}
// Initialize write proxy
final WriteProxy writeProxy = client.createWriteProxy(localDb);
final WriteProxy writeProxy = client.createWriteProxy(localDB);
if (client.isOnline()) {
// Save all users to the local database and flush cache
localDb.setUsers(client.getUsers());
localDB.setUsers(client.getUsers());
writeProxy.flushCache();
} else
// Set all contacts to offline mode
localDb.getUsers().values().stream().filter(u -> u != localDb.getUser()).forEach(u -> u.setStatus(UserStatus.OFFLINE));
localDB.getUsers().values().stream().filter(u -> u != localDB.getUser()).forEach(u -> u.setStatus(UserStatus.OFFLINE));
// Display ChatWindow and StatusTrayIcon
EventQueue.invokeLater(() -> {
try {
chatWindow.initContent(client, localDb, writeProxy);
chatWindow.initContent(client, localDB, writeProxy);
// Relay unread messages from cache
if (cache != null && client.isOnline()) cache.relay();
@ -160,14 +160,14 @@ public class Startup {
}
});
// Save Settings and PersistentLocalDb on shutdown
// Save Settings and PersistentLocalDB on shutdown
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
try {
logger.info("Closing connection...");
client.close();
logger.info("Saving local database and settings...");
localDb.save();
localDB.save();
Settings.getInstance().save();
} catch (Exception e) {
logger.log(Level.SEVERE, "Unable to save local files", e);

View File

@ -12,13 +12,13 @@ import envoy.event.MessageStatusChangeEvent;
* For message ID generation a {@link IdGenerator} is stored as well.<br>
* <br>
* Project: <strong>envoy-client</strong><br>
* File: <strong>LocalDb.java</strong><br>
* File: <strong>LocalDB.java</strong><br>
* Created: <strong>3 Feb 2020</strong><br>
*
* @author Kai S. K. Engelbart
* @since Envoy Client v0.3-alpha
*/
public abstract class LocalDb {
public abstract class LocalDB {
protected User user;
protected Map<String, User> users = new HashMap<>();

View File

@ -10,18 +10,18 @@ import envoy.data.IdGenerator;
import envoy.util.SerializationUtils;
/**
* Implements a {@link LocalDb} in a way that stores all information inside a
* Implements a {@link LocalDB} in a way that stores all information inside a
* folder on the local file system.<br>
* <br>
* Project: <strong>envoy-client</strong><br>
* File: <strong>PersistentLocalDb.java</strong><br>
* File: <strong>PersistentLocalDB.java</strong><br>
* Created: <strong>27.10.2019</strong><br>
*
* @author Kai S. K. Engelbart
* @author Maximilian K&auml;fer
* @since Envoy Client v0.1-alpha
*/
public class PersistentLocalDb extends LocalDb {
public class PersistentLocalDB extends LocalDB {
private File localDBDir, localDBFile, usersFile, idGeneratorFile, messageCacheFile, statusCacheFile;
@ -34,17 +34,17 @@ public class PersistentLocalDb extends LocalDb {
*
* @since Envoy Client v0.3-alpha
*/
public PersistentLocalDb() {}
public PersistentLocalDB() {}
/**
* Constructs an empty local database. To serialize any chats to the file
* system, call {@link PersistentLocalDb#initializeUserStorage()}.
* system, call {@link PersistentLocalDB#initializeUserStorage()}.
*
* @param localDbDir the directory in which to store users and chats
* @throws IOException if the PersistentLocalDb could not be initialized
* @throws IOException if the PersistentLocalDB could not be initialized
* @since Envoy Client v0.1-alpha
*/
public PersistentLocalDb(File localDbDir) throws IOException {
public PersistentLocalDB(File localDbDir) throws IOException {
localDBDir = localDbDir;
// Initialize local database directory

View File

@ -1,15 +1,15 @@
package envoy.client.data;
/**
* Implements a {@link LocalDb} in a way that does not persist any information
* Implements a {@link LocalDB} in a way that does not persist any information
* after application shutdown.<br>
* <br>
* Project: <strong>envoy-client</strong><br>
* File: <strong>TransientLocalDb.java</strong><br>
* File: <strong>TransientLocalDB.java</strong><br>
* Created: <strong>3 Feb 2020</strong><br>
*
* @author Kai S. K. Engelbart
* @since Envoy Client v0.3-alpha
*/
public class TransientLocalDb extends LocalDb {
public class TransientLocalDB extends LocalDB {
}

View File

@ -11,7 +11,7 @@ import javax.naming.TimeLimitExceededException;
import envoy.client.data.Cache;
import envoy.client.data.ClientConfig;
import envoy.client.data.LocalDb;
import envoy.client.data.LocalDB;
import envoy.client.event.SendEvent;
import envoy.data.*;
import envoy.event.*;
@ -117,7 +117,7 @@ public class Client implements Closeable {
* Initializes the {@link Receiver} used to process data sent from the server to
* this client.
*
* @param localDb the local database used to persist the current
* @param localDB the local database used to persist the current
* {@link IdGenerator}
* @param receivedMessageCache a message cache containing all unread messages
* from the server that can be relayed after
@ -126,7 +126,7 @@ public class Client implements Closeable {
* requested from the server
* @since Envoy Client v0.2-alpha
*/
public void initReceiver(LocalDb localDb, Cache<Message> receivedMessageCache) throws IOException {
public void initReceiver(LocalDB localDB, Cache<Message> receivedMessageCache) throws IOException {
checkOnline();
// Process incoming messages
@ -140,10 +140,10 @@ public class Client implements Closeable {
receiver.registerProcessor(MessageStatusChangeEvent.class, new MessageStatusChangeEventProcessor());
// Process user status changes
receiver.registerProcessor(UserStatusChangeEvent.class, new UserStatusChangeProcessor(localDb));
receiver.registerProcessor(UserStatusChangeEvent.class, new UserStatusChangeProcessor(localDB));
// Process message ID generation
receiver.registerProcessor(IdGenerator.class, localDb::setIdGenerator);
receiver.registerProcessor(IdGenerator.class, localDB::setIdGenerator);
// Process contact searches
receiver.registerProcessor(ContactSearchResult.class, EventBus.getInstance()::dispatch);
@ -161,19 +161,19 @@ public class Client implements Closeable {
});
// Request a generator if none is present or the existing one is consumed
if (!localDb.hasIdGenerator() || !localDb.getIdGenerator().hasNext()) requestIdGenerator();
if (!localDB.hasIdGenerator() || !localDB.getIdGenerator().hasNext()) requestIdGenerator();
}
/**
* Creates a new write proxy that uses this client to communicate with the
* server.
*
* @param localDb the local database that the write proxy will use to access
* @param localDB the local database that the write proxy will use to access
* caches
* @return a new write proxy
* @since Envoy Client v0.3-alpha
*/
public WriteProxy createWriteProxy(LocalDb localDb) { return new WriteProxy(this, localDb); }
public WriteProxy createWriteProxy(LocalDB localDB) { return new WriteProxy(this, localDB); }
/**
* Sends a message to the server. The message's status will be incremented once

View File

@ -2,7 +2,7 @@ package envoy.client.net;
import java.util.function.Consumer;
import envoy.client.data.LocalDb;
import envoy.client.data.LocalDB;
import envoy.event.EventBus;
import envoy.event.UserStatusChangeEvent;
@ -16,17 +16,17 @@ import envoy.event.UserStatusChangeEvent;
*/
public class UserStatusChangeProcessor implements Consumer<UserStatusChangeEvent> {
private final LocalDb localDb;
private final LocalDB localDB;
/**
* @param localDb the local database in which status updates will by applied
* @param localDB the local database in which status updates will by applied
* @since Envoy Client v0.3-alpha
*/
public UserStatusChangeProcessor(LocalDb localDb) { this.localDb = localDb; }
public UserStatusChangeProcessor(LocalDB localDB) { this.localDB = localDB; }
@Override
public void accept(UserStatusChangeEvent evt) {
localDb.getUsers().values().stream().filter(u -> u.getId() == evt.getId()).findFirst().get().setStatus(evt.get());
localDB.getUsers().values().stream().filter(u -> u.getId() == evt.getId()).findFirst().get().setStatus(evt.get());
EventBus.getInstance().dispatch(evt);
}
}

View File

@ -4,7 +4,7 @@ import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import envoy.client.data.LocalDb;
import envoy.client.data.LocalDB;
import envoy.data.Message;
import envoy.event.MessageStatusChangeEvent;
import envoy.util.EnvoyLog;
@ -12,7 +12,7 @@ import envoy.util.EnvoyLog;
/**
* Implements methods to send {@link Message}s and
* {@link MessageStatusChangeEvent}s to the server or cache them inside a
* {@link LocalDb} depending on the online status.<br>
* {@link LocalDB} depending on the online status.<br>
* <br>
* Project: <strong>envoy-client</strong><br>
* File: <strong>WriteProxy.java</strong><br>
@ -24,7 +24,7 @@ import envoy.util.EnvoyLog;
public class WriteProxy {
private final Client client;
private final LocalDb localDb;
private final LocalDB localDB;
private static final Logger logger = EnvoyLog.getLogger(WriteProxy.class);
@ -34,27 +34,27 @@ public class WriteProxy {
*
* @param client the client used to send messages and message status change
* events
* @param localDb the local database used to cache messages and message status
* @param localDB the local database used to cache messages and message status
* change events
* @since Envoy Client v0.3-alpha
*/
public WriteProxy(Client client, LocalDb localDb) {
public WriteProxy(Client client, LocalDB localDB) {
this.client = client;
this.localDb = localDb;
this.localDB = localDB;
// Initialize cache processors for messages and message status change events
localDb.getMessageCache().setProcessor(msg -> {
localDB.getMessageCache().setProcessor(msg -> {
try {
logger.finer("Sending cached " + msg);
client.sendMessage(msg);
// Update message state to SENT in local db
localDb.getMessage(msg.getId()).nextStatus();
localDB.getMessage(msg.getId()).nextStatus();
} catch (IOException e) {
logger.log(Level.SEVERE, "Could not send cached message", e);
}
});
localDb.getStatusCache().setProcessor(evt -> {
localDB.getStatusCache().setProcessor(evt -> {
logger.finer("Sending cached " + evt);
try {
client.sendEvent(evt);
@ -72,10 +72,10 @@ public class WriteProxy {
*/
public void flushCache() {
// Send messages
localDb.getMessageCache().relay();
localDB.getMessageCache().relay();
// Send message status change events
localDb.getStatusCache().relay();
localDB.getStatusCache().relay();
}
/**
@ -88,7 +88,7 @@ public class WriteProxy {
*/
public void writeMessage(Message message) throws IOException {
if (client.isOnline()) client.sendMessage(message);
else localDb.getMessageCache().accept(message);
else localDB.getMessageCache().accept(message);
}
/**
@ -101,6 +101,6 @@ public class WriteProxy {
*/
public void writeMessageStatusChangeEvent(MessageStatusChangeEvent evt) throws IOException {
if (client.isOnline()) client.sendEvent(evt);
else localDb.getStatusCache().accept(evt);
else localDB.getStatusCache().accept(evt);
}
}

View File

@ -17,7 +17,7 @@ import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import envoy.client.data.Chat;
import envoy.client.data.LocalDb;
import envoy.client.data.LocalDB;
import envoy.client.data.Settings;
import envoy.client.event.MessageCreationEvent;
import envoy.client.event.ThemeChangeEvent;
@ -63,7 +63,7 @@ public class ChatWindow extends JFrame {
// User specific objects
private Client client;
private WriteProxy writeProxy;
private LocalDb localDb;
private LocalDB localDB;
private Chat currentChat;
// GUI components
@ -132,7 +132,7 @@ public class ChatWindow extends JFrame {
Map<String, ActionListener> commands = Map.of("forward selected message", evt -> {
final Message selectedMessage = messageList.getSingleSelectedElement();
List<User> chosenContacts = ContactsChooserDialog
.showForwardingDialog("Forward selected message to", null, selectedMessage, localDb.getUsers().values());
.showForwardingDialog("Forward selected message to", null, selectedMessage, localDB.getUsers().values());
if (chosenContacts != null && chosenContacts.size() > 0) forwardMessage(selectedMessage, chosenContacts.toArray(new User[0]));
}, "copy", evt -> {
// TODO should be enhanced to allow also copying of message attachments,
@ -242,7 +242,7 @@ public class ChatWindow extends JFrame {
userList.setCellRenderer(new UserListRenderer());
userList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
userList.addListSelectionListener((listSelectionEvent) -> {
if (client != null && localDb != null && !listSelectionEvent.getValueIsAdjusting()) {
if (client != null && localDB != null && !listSelectionEvent.getValueIsAdjusting()) {
final JList<User> selectedUserList = (JList<User>) listSelectionEvent.getSource();
final User user = selectedUserList.getSelectedValue();
@ -250,7 +250,7 @@ public class ChatWindow extends JFrame {
if (contentPane.getComponent(i).equals(searchPane)) drawChatBox(gbc_scrollPane);
if (user != null) {
// Select current chat
currentChat = localDb.getChats().stream().filter(chat -> chat.getRecipient().getId() == user.getId()).findFirst().get();
currentChat = localDB.getChats().stream().filter(chat -> chat.getRecipient().getId() == user.getId()).findFirst().get();
// Read current chat
readCurrentChat();
@ -412,7 +412,7 @@ public class ChatWindow extends JFrame {
// Listen to received messages
EventBus.getInstance().register(MessageCreationEvent.class, evt -> {
Message message = evt.get();
Chat chat = localDb.getChats().stream().filter(c -> c.getRecipient().getId() == message.getSenderId()).findFirst().get();
Chat chat = localDB.getChats().stream().filter(c -> c.getRecipient().getId() == message.getSenderId()).findFirst().get();
chat.appendMessage(message);
// Read message and update UI if in current chat
@ -427,7 +427,7 @@ public class ChatWindow extends JFrame {
final long id = evt.getId();
final MessageStatus status = evt.get();
for (Chat c : localDb.getChats())
for (Chat c : localDB.getChats())
for (Message m : c.getModel())
if (m.getId() == id) {
@ -466,8 +466,8 @@ public class ChatWindow extends JFrame {
// Update LocalDB
userListModel.addElement(contact);
localDb.getUsers().put(contact.getName(), contact);
localDb.getChats().add(new Chat(contact));
localDB.getUsers().put(contact.getName(), contact);
localDB.getChats().add(new Chat(contact));
revalidate();
repaint();
@ -543,7 +543,7 @@ public class ChatWindow extends JFrame {
if (!text.isEmpty()) checkMessageTextLength();
// Create message
final Message message = new MessageBuilder(localDb.getUser().getId(), currentChat.getRecipient().getId(), localDb.getIdGenerator())
final Message message = new MessageBuilder(localDB.getUser().getId(), currentChat.getRecipient().getId(), localDB.getIdGenerator())
.setText(text)
.build();
sendMessage(message);
@ -561,7 +561,7 @@ public class ChatWindow extends JFrame {
*/
private void forwardMessage(Message message, User... recipients) {
Arrays.stream(recipients).forEach(recipient -> {
if (message != null && recipients != null) sendMessage(new MessageBuilder(message, recipient.getId(), localDb.getIdGenerator()).build());
if (message != null && recipients != null) sendMessage(new MessageBuilder(message, recipient.getId(), localDB.getIdGenerator()).build());
else throw new NullPointerException("No recipient or no message selected");
});
@ -583,7 +583,7 @@ public class ChatWindow extends JFrame {
// Send message
writeProxy.writeMessage(message);
// Add message to PersistentLocalDb and update UI
// Add message to PersistentLocalDB and update UI
currentChat.appendMessage(message);
// Update UI
@ -591,7 +591,7 @@ public class ChatWindow extends JFrame {
repaint();
// Request a new id generator if all IDs were used
if (!localDb.getIdGenerator().hasNext()) client.requestIdGenerator();
if (!localDB.getIdGenerator().hasNext()) client.requestIdGenerator();
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "Error sending message:\n" + e.toString(), "Message sending error", JOptionPane.ERROR_MESSAGE);
@ -634,27 +634,27 @@ public class ChatWindow extends JFrame {
* This will trigger the display of the contact list.
*
* @param client the client used to send and receive messages
* @param localDb the local database used to manage stored messages
* @param localDB the local database used to manage stored messages
* and users
* @param writeProxy the write proxy used to send messages and status change
* events to the server or cache them inside the local
* database
* @since Envoy Client v0.3-alpha
*/
public void initContent(Client client, LocalDb localDb, WriteProxy writeProxy) {
public void initContent(Client client, LocalDB localDB, WriteProxy writeProxy) {
this.client = client;
this.localDb = localDb;
this.localDB = localDB;
this.writeProxy = writeProxy;
messageList.setRenderer((list, message) -> new MessageComponent(list, message, client.getSender().getId()));
// Load users and chats
new Thread(() -> {
localDb.getUsers().values().forEach(user -> {
localDB.getUsers().values().forEach(user -> {
userListModel.addElement(user);
// Check if user exists in local DB
if (localDb.getChats().stream().noneMatch(c -> c.getRecipient().getId() == user.getId())) localDb.getChats().add(new Chat(user));
if (localDB.getChats().stream().noneMatch(c -> c.getRecipient().getId() == user.getId())) localDB.getChats().add(new Chat(user));
});
SwingUtilities.invokeLater(() -> userList.setModel(userListModel));

View File

@ -60,7 +60,7 @@ public class LoginDialog extends JDialog {
private LoginCredentials credentials;
private final Client client;
private final LocalDb localDb;
private final LocalDB localDB;
private final Cache<Message> receivedMessageCache;
private static final ClientConfig config = ClientConfig.getInstance();
@ -71,18 +71,18 @@ public class LoginDialog extends JDialog {
* Displays a dialog enabling the user to enter their user name and password.
*
* @param client the client used to perform the handshake
* @param localDb the local database in which data is persisted
* @param localDB the local database in which data is persisted
* @param receivedMessageCache the cache that stored messages received during
* the handshake
* @since Envoy Client v0.3-alpha
*/
public LoginDialog(Client client, LocalDb localDb, Cache<Message> receivedMessageCache) {
public LoginDialog(Client client, LocalDB localDB, Cache<Message> receivedMessageCache) {
this.client = client;
this.localDb = localDb;
this.localDB = localDB;
this.receivedMessageCache = receivedMessageCache;
// Prepare handshake
localDb.loadIdGenerator();
localDB.loadIdGenerator();
addWindowListener(new WindowAdapter() {
@ -134,7 +134,7 @@ public class LoginDialog extends JDialog {
try {
client.performHandshake(credentials, receivedMessageCache);
if (client.isOnline()) {
client.initReceiver(localDb, receivedMessageCache);
client.initReceiver(localDB, receivedMessageCache);
dispose();
}
} catch (IOException | InterruptedException | TimeLimitExceededException e) {
@ -142,8 +142,8 @@ public class LoginDialog extends JDialog {
e.printStackTrace();
try {
// Try entering offline mode
localDb.loadUsers();
User clientUser = localDb.getUsers().get(credentials.getIdentifier());
localDB.loadUsers();
User clientUser = localDB.getUsers().get(credentials.getIdentifier());
if (clientUser == null) throw new EnvoyException("Could not enter offline mode: user name unknown");
client.setSender(clientUser);
JOptionPane.showMessageDialog(null,