2019-12-28 21:03:41 +01:00
|
|
|
package envoy.data;
|
|
|
|
|
2020-04-01 18:09:07 +02:00
|
|
|
import java.io.ObjectInputStream;
|
|
|
|
import java.io.ObjectOutputStream;
|
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.Set;
|
|
|
|
|
2019-12-28 21:03:41 +01:00
|
|
|
/**
|
|
|
|
* Represents a unique user with a unique, numeric ID, a name and a current
|
|
|
|
* {@link UserStatus}.<br>
|
|
|
|
* <br>
|
|
|
|
* Project: <strong>envoy-common</strong><br>
|
|
|
|
* File: <strong>User.java</strong><br>
|
|
|
|
* Created: <strong>28.12.2019</strong><br>
|
2019-12-30 18:49:48 +01:00
|
|
|
*
|
2019-12-28 21:03:41 +01:00
|
|
|
* @author Kai S. K. Engelbart
|
|
|
|
* @since Envoy Common v0.2-alpha
|
|
|
|
*/
|
2020-04-01 18:09:07 +02:00
|
|
|
public final class User extends Contact {
|
|
|
|
|
|
|
|
private UserStatus status;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used to serialize contact list to a maximum depth of one
|
|
|
|
*/
|
|
|
|
private transient boolean serializeContacts = true;
|
|
|
|
|
|
|
|
private static final long serialVersionUID = 1L;
|
2019-12-28 21:03:41 +01:00
|
|
|
|
2019-12-30 18:49:48 +01:00
|
|
|
/**
|
2019-12-31 10:16:52 +01:00
|
|
|
* This enumeration defines all possible statuses a user can have.
|
2019-12-30 18:49:48 +01:00
|
|
|
*
|
|
|
|
* @since Envoy Common v0.2-alpha
|
|
|
|
*/
|
|
|
|
public static enum UserStatus {
|
2019-12-28 21:03:41 +01:00
|
|
|
|
2019-12-30 18:49:48 +01:00
|
|
|
/**
|
|
|
|
* select this, if a user is online and can be interacted with
|
|
|
|
*/
|
|
|
|
ONLINE,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* select this, if a user is online but unavailable at the moment (sudden
|
|
|
|
* interruption)
|
|
|
|
*/
|
|
|
|
AWAY,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* select this, if a user is online but unavailable at the moment (polite way)
|
|
|
|
*/
|
|
|
|
BUSY,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* select this, if a user is offline
|
|
|
|
*/
|
|
|
|
OFFLINE;
|
|
|
|
}
|
2019-12-28 21:03:41 +01:00
|
|
|
|
|
|
|
/**
|
2020-03-24 19:06:56 +01:00
|
|
|
* Initializes a {@link User}. <br>
|
|
|
|
* The {@link UserStatus} is set to {@link UserStatus#ONLINE}.
|
2020-04-01 18:09:07 +02:00
|
|
|
* No contacts are initialized.
|
2019-12-30 18:49:48 +01:00
|
|
|
*
|
2019-12-28 21:03:41 +01:00
|
|
|
* @param id unique ID
|
|
|
|
* @param name user name
|
2020-01-01 19:16:40 +01:00
|
|
|
* @since Envoy Common v0.2-alpha
|
2019-12-28 21:03:41 +01:00
|
|
|
*/
|
|
|
|
public User(long id, String name) {
|
2020-04-01 18:09:07 +02:00
|
|
|
super(id, name, new HashSet<>());
|
|
|
|
status = UserStatus.ONLINE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes a {@link User}. <br>
|
|
|
|
* The {@link UserStatus} is set to {@link UserStatus#ONLINE}.
|
|
|
|
*
|
|
|
|
* @param id unique ID
|
|
|
|
* @param name user name
|
|
|
|
* @param contacts the contacts of this user
|
|
|
|
* @since Envoy Common v0.2-alpha
|
|
|
|
*/
|
|
|
|
public User(long id, String name, Set<Contact> contacts) {
|
|
|
|
super(id, name, contacts);
|
2020-03-24 19:06:56 +01:00
|
|
|
status = UserStatus.ONLINE;
|
2019-12-28 21:03:41 +01:00
|
|
|
}
|
|
|
|
|
2020-01-18 09:37:35 +01:00
|
|
|
/**
|
|
|
|
* Initializes a {@link User}.
|
|
|
|
*
|
2020-04-01 18:09:07 +02:00
|
|
|
* @param id unique ID
|
|
|
|
* @param name user name
|
|
|
|
* @param status the status of this user
|
|
|
|
* @param contacts the contacts of this user
|
2020-01-18 09:37:35 +01:00
|
|
|
* @since Envoy Common v0.2-alpha
|
|
|
|
*/
|
2020-04-01 18:09:07 +02:00
|
|
|
public User(long id, String name, UserStatus status, Set<Contact> contacts) {
|
|
|
|
super(id, name, contacts);
|
2020-03-22 14:52:38 +01:00
|
|
|
this.status = status;
|
2020-01-18 09:37:35 +01:00
|
|
|
}
|
|
|
|
|
2019-12-31 10:20:35 +01:00
|
|
|
@Override
|
2020-04-01 18:09:07 +02:00
|
|
|
public String toString() { return String.format("User[id=%d,name=%s,status=%s,contacts=%s]", getID(), getName(), status, getContacts()); }
|
2019-12-28 21:03:41 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return the current status of this user
|
2020-01-01 19:16:40 +01:00
|
|
|
* @since Envoy Common v0.2-alpha
|
2019-12-28 21:03:41 +01:00
|
|
|
*/
|
|
|
|
public UserStatus getStatus() { return status; }
|
|
|
|
|
|
|
|
/**
|
2020-01-02 17:50:04 +01:00
|
|
|
* @param status the next status of this user
|
2020-01-01 19:16:40 +01:00
|
|
|
* @since Envoy Common v0.2-alpha
|
2019-12-28 21:03:41 +01:00
|
|
|
*/
|
|
|
|
public void setStatus(UserStatus status) { this.status = status; }
|
2020-04-01 18:09:07 +02:00
|
|
|
|
|
|
|
private void readObject(ObjectInputStream inputStream) throws Exception {
|
|
|
|
inputStream.defaultReadObject();
|
|
|
|
var contacts = Contact.class.getDeclaredField("contacts");
|
|
|
|
contacts.setAccessible(true);
|
|
|
|
contacts.set(this, inputStream.readObject());
|
|
|
|
}
|
|
|
|
|
|
|
|
private void writeObject(ObjectOutputStream outputStream) throws Exception {
|
|
|
|
outputStream.defaultWriteObject();
|
|
|
|
if (serializeContacts) {
|
|
|
|
getContacts().stream().filter(User.class::isInstance).map(User.class::cast).forEach(user -> user.serializeContacts = false);
|
|
|
|
outputStream.writeObject(getContacts());
|
2020-04-02 09:06:10 +02:00
|
|
|
} else outputStream.writeObject(new HashSet<>());
|
2020-04-01 18:09:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param serializeContacts whether the contacts of this {@link User} should be
|
|
|
|
* serialized
|
|
|
|
* @since Envoy Common v0.1-beta
|
|
|
|
*/
|
|
|
|
public void serializeContacts(boolean serializeContacts) { this.serializeContacts = serializeContacts; }
|
2020-03-22 14:52:38 +01:00
|
|
|
}
|