deleted Contacts object, added contacts as part of Contact class
additionally: * updated serialization method to not recurse endlessly * refactored type of contacts from List to Set
This commit is contained in:
		@@ -24,5 +24,6 @@
 | 
				
			|||||||
			<attribute name="maven.pomderived" value="true"/>
 | 
								<attribute name="maven.pomderived" value="true"/>
 | 
				
			||||||
		</attributes>
 | 
							</attributes>
 | 
				
			||||||
	</classpathentry>
 | 
						</classpathentry>
 | 
				
			||||||
 | 
						<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/5"/>
 | 
				
			||||||
	<classpathentry kind="output" path="target/classes"/>
 | 
						<classpathentry kind="output" path="target/classes"/>
 | 
				
			||||||
</classpath>
 | 
					</classpath>
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										1
									
								
								pom.xml
									
									
									
									
									
								
							
							
						
						
									
										1
									
								
								pom.xml
									
									
									
									
									
								
							@@ -23,6 +23,7 @@
 | 
				
			|||||||
				<directory>src/main/resources</directory>
 | 
									<directory>src/main/resources</directory>
 | 
				
			||||||
			</resource>
 | 
								</resource>
 | 
				
			||||||
		</resources>
 | 
							</resources>
 | 
				
			||||||
 | 
					 		<testSourceDirectory>src/test/java</testSourceDirectory>
 | 
				
			||||||
		<pluginManagement>
 | 
							<pluginManagement>
 | 
				
			||||||
			<plugins>
 | 
								<plugins>
 | 
				
			||||||
				<plugin>
 | 
									<plugin>
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -2,6 +2,7 @@ package envoy.data;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import java.io.Serializable;
 | 
					import java.io.Serializable;
 | 
				
			||||||
import java.util.Objects;
 | 
					import java.util.Objects;
 | 
				
			||||||
 | 
					import java.util.Set;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * This class is the superclass for both {@link User} and {@link Group}.<br>
 | 
					 * This class is the superclass for both {@link User} and {@link Group}.<br>
 | 
				
			||||||
@@ -17,6 +18,7 @@ import java.util.Objects;
 | 
				
			|||||||
public abstract class Contact implements Serializable {
 | 
					public abstract class Contact implements Serializable {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	private final long								id;
 | 
						private final long								id;
 | 
				
			||||||
 | 
						private final transient Set<? extends Contact>	contacts;
 | 
				
			||||||
	private String									name;
 | 
						private String									name;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	private static final long serialVersionUID = 0L;
 | 
						private static final long serialVersionUID = 0L;
 | 
				
			||||||
@@ -26,11 +28,13 @@ public abstract class Contact implements Serializable {
 | 
				
			|||||||
	 *
 | 
						 *
 | 
				
			||||||
	 * @param id       the ID of this contact
 | 
						 * @param id       the ID of this contact
 | 
				
			||||||
	 * @param name     the name of this contact
 | 
						 * @param name     the name of this contact
 | 
				
			||||||
 | 
						 * @param contacts the {@link Contacts} of this {@link Contact}
 | 
				
			||||||
	 * @since Envoy Common v0.1-beta
 | 
						 * @since Envoy Common v0.1-beta
 | 
				
			||||||
	 */
 | 
						 */
 | 
				
			||||||
	public Contact(long id, String name) {
 | 
						public Contact(long id, String name, Set<? extends Contact> contacts) {
 | 
				
			||||||
		this.id			= id;
 | 
							this.id			= id;
 | 
				
			||||||
		this.name		= name;
 | 
							this.name		= name;
 | 
				
			||||||
 | 
							this.contacts	= contacts;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
@@ -55,7 +59,7 @@ public abstract class Contact implements Serializable {
 | 
				
			|||||||
	 * {@inheritDoc}
 | 
						 * {@inheritDoc}
 | 
				
			||||||
	 */
 | 
						 */
 | 
				
			||||||
	@Override
 | 
						@Override
 | 
				
			||||||
	public String toString() { return String.format("Contact[id=%d,name=%s]", id, name); }
 | 
						public String toString() { return String.format("Contact[id=%d,name=%s, contacts=%s]", id, name, contacts); }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
	 * {@inheritDoc}
 | 
						 * {@inheritDoc}
 | 
				
			||||||
@@ -72,4 +76,10 @@ public abstract class Contact implements Serializable {
 | 
				
			|||||||
		if (!(obj instanceof Contact)) return false;
 | 
							if (!(obj instanceof Contact)) return false;
 | 
				
			||||||
		return id == ((Contact) obj).id;
 | 
							return id == ((Contact) obj).id;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/**
 | 
				
			||||||
 | 
						 * @return the contacts of this {@link Contact}
 | 
				
			||||||
 | 
						 * @since Envoy Common v0.1-beta
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						public Set<? extends Contact> getContacts() { return contacts; }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,36 +0,0 @@
 | 
				
			|||||||
package envoy.data;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
import java.io.Serializable;
 | 
					 | 
				
			||||||
import java.util.List;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
/**
 | 
					 | 
				
			||||||
 * Project: <strong>envoy-common</strong><br>
 | 
					 | 
				
			||||||
 * File: <strong>Contacts.java</strong><br>
 | 
					 | 
				
			||||||
 * Created: <strong>02.01.2020</strong><br>
 | 
					 | 
				
			||||||
 *
 | 
					 | 
				
			||||||
 * @author Kai S. K. Engelbart
 | 
					 | 
				
			||||||
 * @since Envoy Common v0.2-alpha
 | 
					 | 
				
			||||||
 */
 | 
					 | 
				
			||||||
public class Contacts implements Serializable {
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	private final List<Contact> contacts;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	private static final long serialVersionUID = 0L;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	/**
 | 
					 | 
				
			||||||
	 * Creates an instance of {@link Contacts}.
 | 
					 | 
				
			||||||
	 *
 | 
					 | 
				
			||||||
	 * @param contacts the contact list
 | 
					 | 
				
			||||||
	 * @since Envoy Common v0.2-alpha
 | 
					 | 
				
			||||||
	 */
 | 
					 | 
				
			||||||
	public Contacts(List<Contact> contacts) { this.contacts = contacts; }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	@Override
 | 
					 | 
				
			||||||
	public String toString() { return String.format("Contacts[%s]", contacts); }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	/**
 | 
					 | 
				
			||||||
	 * @return a list of users messages can be sent to
 | 
					 | 
				
			||||||
	 * @since Envoy Common v0.2-alpha
 | 
					 | 
				
			||||||
	 */
 | 
					 | 
				
			||||||
	public List<Contact> getContacts() { return contacts; }
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
@@ -1,8 +1,9 @@
 | 
				
			|||||||
package envoy.data;
 | 
					package envoy.data;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import java.util.ArrayList;
 | 
					import java.io.ObjectInputStream;
 | 
				
			||||||
import java.util.List;
 | 
					import java.io.ObjectOutputStream;
 | 
				
			||||||
import java.util.StringJoiner;
 | 
					import java.util.HashSet;
 | 
				
			||||||
 | 
					import java.util.Set;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * Project: <strong>envoy-common</strong><br>
 | 
					 * Project: <strong>envoy-common</strong><br>
 | 
				
			||||||
@@ -12,50 +13,44 @@ import java.util.StringJoiner;
 | 
				
			|||||||
 * @author Leon Hofmeister
 | 
					 * @author Leon Hofmeister
 | 
				
			||||||
 * @since Envoy Common v0.1-beta
 | 
					 * @since Envoy Common v0.1-beta
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
public class Group extends Contact {
 | 
					public final class Group extends Contact {
 | 
				
			||||||
 | 
					 | 
				
			||||||
	// TODO add admins
 | 
					 | 
				
			||||||
	private final List<Long> memberIDs;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	private static final long serialVersionUID = 0L;
 | 
						private static final long serialVersionUID = 0L;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
	 * Creates a new instance of a {@link Group}.
 | 
						 * Creates a new instance of a {@link Group} without any members.
 | 
				
			||||||
	 *
 | 
						 *
 | 
				
			||||||
	 * @param id   the ID of this group
 | 
						 * @param id   the ID of this group
 | 
				
			||||||
	 * @param name the name of this group
 | 
						 * @param name the name of this group
 | 
				
			||||||
	 * @since Envoy Common v0.1-beta
 | 
						 * @since Envoy Common v0.1-beta
 | 
				
			||||||
	 */
 | 
						 */
 | 
				
			||||||
	public Group(long id, String name) { this(id, name, new ArrayList<>()); }
 | 
						public Group(long id, String name) { this(id, name, new HashSet<User>()); }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
	 * Creates a new instance of a {@link Group}.
 | 
						 * Creates an instance of a {@link Group}.
 | 
				
			||||||
	 *
 | 
						 *
 | 
				
			||||||
	 * @param id      the ID of this group
 | 
						 * @param id      the ID of this group
 | 
				
			||||||
	 * @param name    the name of this group
 | 
						 * @param name    the name of this group
 | 
				
			||||||
	 * @param memberIDs the IDs of all members that should be preinitialized
 | 
						 * @param members all members that should be preinitialized
 | 
				
			||||||
	 * @since Envoy Common v0.1-beta
 | 
						 * @since Envoy Common v0.1-beta
 | 
				
			||||||
	 */
 | 
						 */
 | 
				
			||||||
	public Group(long id, String name, List<Long> memberIDs) {
 | 
						public Group(long id, String name, Set<User> members) { super(id, name, members); }
 | 
				
			||||||
		super(id, name);
 | 
					
 | 
				
			||||||
		this.memberIDs = memberIDs;
 | 
						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 {
 | 
				
			||||||
	 * @return the IDs of all members of this group
 | 
							outputStream.defaultWriteObject();
 | 
				
			||||||
	 * @since Envoy Common v0.1-beta
 | 
							getContacts().forEach(user -> user.serializeContacts(false));
 | 
				
			||||||
	 */
 | 
							outputStream.writeObject(getContacts());
 | 
				
			||||||
	public List<Long> getMemberIDs() { return memberIDs; }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/**
 | 
						}
 | 
				
			||||||
	 * {@inheritDoc}
 | 
					
 | 
				
			||||||
	 */
 | 
						@SuppressWarnings("unchecked")
 | 
				
			||||||
	@Override
 | 
						@Override
 | 
				
			||||||
	public String toString() {
 | 
						public Set<User> getContacts() { return (Set<User>) super.getContacts(); }
 | 
				
			||||||
		var joiner = new StringJoiner(",", "Group[id=", "]");
 | 
					 | 
				
			||||||
		joiner.add("id=" + getID());
 | 
					 | 
				
			||||||
		joiner.add("name=" + getName());
 | 
					 | 
				
			||||||
		joiner.add("members=" + getMemberIDs().size());
 | 
					 | 
				
			||||||
		return joiner.toString();
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -2,6 +2,7 @@ package envoy.data;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import java.util.Date;
 | 
					import java.util.Date;
 | 
				
			||||||
import java.util.HashMap;
 | 
					import java.util.HashMap;
 | 
				
			||||||
 | 
					import java.util.Map;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import envoy.data.Message.MessageStatus;
 | 
					import envoy.data.Message.MessageStatus;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -102,6 +103,35 @@ public class MessageBuilder {
 | 
				
			|||||||
		return new Message(id, senderID, recipientID, creationDate, text, attachment, status, forwarded);
 | 
							return new Message(id, senderID, recipientID, creationDate, text, attachment, status, forwarded);
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/**
 | 
				
			||||||
 | 
						 * Creates an instance of {@link GroupMessage} with the previously supplied
 | 
				
			||||||
 | 
						 * values. <br>
 | 
				
			||||||
 | 
						 * <b> Sets all member statuses to {@link MessageStatus#WAITING}.</b><br>
 | 
				
			||||||
 | 
						 * If a mandatory value is not set, a default value will be used
 | 
				
			||||||
 | 
						 * instead:<br>
 | 
				
			||||||
 | 
						 * <br>
 | 
				
			||||||
 | 
						 * <table border="1">
 | 
				
			||||||
 | 
						 * <tr>
 | 
				
			||||||
 | 
						 * <td>{@code date}</td>
 | 
				
			||||||
 | 
						 * <td>{@code new Date()}</td>
 | 
				
			||||||
 | 
						 * <tr>
 | 
				
			||||||
 | 
						 * <tr>
 | 
				
			||||||
 | 
						 * <td>{@code text}</td>
 | 
				
			||||||
 | 
						 * <td>{@code ""}</td>
 | 
				
			||||||
 | 
						 * <tr>
 | 
				
			||||||
 | 
						 * </table>
 | 
				
			||||||
 | 
						 *
 | 
				
			||||||
 | 
						 * @param group the {@link Group} that is used to fill the map of member
 | 
				
			||||||
 | 
						 *              statuses
 | 
				
			||||||
 | 
						 * @return a new instance of {@link GroupMessage}
 | 
				
			||||||
 | 
						 * @since Envoy Common v0.2-alpha
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						public GroupMessage buildGroupMessage(Group group) {
 | 
				
			||||||
 | 
							var memberStatuses = new HashMap<Long, Message.MessageStatus>();
 | 
				
			||||||
 | 
							group.getContacts().forEach(user -> memberStatuses.put(user.getID(), MessageStatus.WAITING));
 | 
				
			||||||
 | 
							return buildGroupMessage(group, memberStatuses);
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
	 * Creates an instance of {@link GroupMessage} with the previously supplied
 | 
						 * Creates an instance of {@link GroupMessage} with the previously supplied
 | 
				
			||||||
	 * values. If a mandatory value is not set, a default value will be used
 | 
						 * values. If a mandatory value is not set, a default value will be used
 | 
				
			||||||
@@ -116,22 +146,17 @@ public class MessageBuilder {
 | 
				
			|||||||
	 * <td>{@code text}</td>
 | 
						 * <td>{@code text}</td>
 | 
				
			||||||
	 * <td>{@code ""}</td>
 | 
						 * <td>{@code ""}</td>
 | 
				
			||||||
	 * <tr>
 | 
						 * <tr>
 | 
				
			||||||
	 * <tr>
 | 
					 | 
				
			||||||
	 * <td>{@code status}</td>
 | 
					 | 
				
			||||||
	 * <td>{@code MessageStatus.WAITING}</td>
 | 
					 | 
				
			||||||
	 * <tr>
 | 
					 | 
				
			||||||
	 * </table>
 | 
						 * </table>
 | 
				
			||||||
	 *
 | 
						 *
 | 
				
			||||||
	 * @param group the {@link Group} that is used to fill the map of member
 | 
						 * @param group          the {@link Group} that is used to fill the map of
 | 
				
			||||||
	 *              statuses
 | 
						 *                       member statuses
 | 
				
			||||||
 | 
						 * @param memberStatuses the map of all current statuses
 | 
				
			||||||
	 * @return a new instance of {@link GroupMessage}
 | 
						 * @return a new instance of {@link GroupMessage}
 | 
				
			||||||
	 * @since Envoy Common v0.2-alpha
 | 
						 * @since Envoy Common v0.1-beta
 | 
				
			||||||
	 */
 | 
						 */
 | 
				
			||||||
	public GroupMessage buildGroupMessage(Group group) {
 | 
						public GroupMessage buildGroupMessage(Group group, Map<Long, MessageStatus> memberStatuses) {
 | 
				
			||||||
		if (group == null) throw new NullPointerException();
 | 
							if (group == null || memberStatuses == null) throw new NullPointerException();
 | 
				
			||||||
		supplyDefaults();
 | 
							supplyDefaults();
 | 
				
			||||||
		var memberStatuses = new HashMap<Long, Message.MessageStatus>();
 | 
					 | 
				
			||||||
		group.getMemberIDs().forEach(id -> memberStatuses.put(id, MessageStatus.WAITING));
 | 
					 | 
				
			||||||
		return new GroupMessage(id, senderID, recipientID, creationDate, text, attachment, status, forwarded, memberStatuses);
 | 
							return new GroupMessage(id, senderID, recipientID, creationDate, text, attachment, status, forwarded, memberStatuses);
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,5 +1,11 @@
 | 
				
			|||||||
package envoy.data;
 | 
					package envoy.data;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import java.io.ObjectInputStream;
 | 
				
			||||||
 | 
					import java.io.ObjectOutputStream;
 | 
				
			||||||
 | 
					import java.util.ArrayList;
 | 
				
			||||||
 | 
					import java.util.HashSet;
 | 
				
			||||||
 | 
					import java.util.Set;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * Represents a unique user with a unique, numeric ID, a name and a current
 | 
					 * Represents a unique user with a unique, numeric ID, a name and a current
 | 
				
			||||||
 * {@link UserStatus}.<br>
 | 
					 * {@link UserStatus}.<br>
 | 
				
			||||||
@@ -11,7 +17,16 @@ package envoy.data;
 | 
				
			|||||||
 * @author Kai S. K. Engelbart
 | 
					 * @author Kai S. K. Engelbart
 | 
				
			||||||
 * @since Envoy Common v0.2-alpha
 | 
					 * @since Envoy Common v0.2-alpha
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
public class User extends Contact {
 | 
					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;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
	 * This enumeration defines all possible statuses a user can have.
 | 
						 * This enumeration defines all possible statuses a user can have.
 | 
				
			||||||
@@ -42,9 +57,19 @@ public class User extends Contact {
 | 
				
			|||||||
		OFFLINE;
 | 
							OFFLINE;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	private UserStatus status;
 | 
						/**
 | 
				
			||||||
 | 
						 * Initializes a {@link User}. <br>
 | 
				
			||||||
	private static final long serialVersionUID = 1L;
 | 
						 * The {@link UserStatus} is set to {@link UserStatus#ONLINE}.
 | 
				
			||||||
 | 
						 * No contacts are initialized.
 | 
				
			||||||
 | 
						 *
 | 
				
			||||||
 | 
						 * @param id   unique ID
 | 
				
			||||||
 | 
						 * @param name user name
 | 
				
			||||||
 | 
						 * @since Envoy Common v0.2-alpha
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						public User(long id, String name) {
 | 
				
			||||||
 | 
							super(id, name, new HashSet<>());
 | 
				
			||||||
 | 
							status = UserStatus.ONLINE;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
	 * Initializes a {@link User}. <br>
 | 
						 * Initializes a {@link User}. <br>
 | 
				
			||||||
@@ -52,10 +77,11 @@ public class User extends Contact {
 | 
				
			|||||||
	 *
 | 
						 *
 | 
				
			||||||
	 * @param id       unique ID
 | 
						 * @param id       unique ID
 | 
				
			||||||
	 * @param name     user name
 | 
						 * @param name     user name
 | 
				
			||||||
 | 
						 * @param contacts the contacts of this user
 | 
				
			||||||
	 * @since Envoy Common v0.2-alpha
 | 
						 * @since Envoy Common v0.2-alpha
 | 
				
			||||||
	 */
 | 
						 */
 | 
				
			||||||
	public User(long id, String name) {
 | 
						public User(long id, String name, Set<Contact> contacts) {
 | 
				
			||||||
		super(id, name);
 | 
							super(id, name, contacts);
 | 
				
			||||||
		status = UserStatus.ONLINE;
 | 
							status = UserStatus.ONLINE;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -64,16 +90,17 @@ public class User extends Contact {
 | 
				
			|||||||
	 *
 | 
						 *
 | 
				
			||||||
	 * @param id       unique ID
 | 
						 * @param id       unique ID
 | 
				
			||||||
	 * @param name     user name
 | 
						 * @param name     user name
 | 
				
			||||||
	 * @param status the status of the user
 | 
						 * @param status   the status of this user
 | 
				
			||||||
 | 
						 * @param contacts the contacts of this user
 | 
				
			||||||
	 * @since Envoy Common v0.2-alpha
 | 
						 * @since Envoy Common v0.2-alpha
 | 
				
			||||||
	 */
 | 
						 */
 | 
				
			||||||
	public User(long id, String name, UserStatus status) {
 | 
						public User(long id, String name, UserStatus status, Set<Contact> contacts) {
 | 
				
			||||||
		super(id, name);
 | 
							super(id, name, contacts);
 | 
				
			||||||
		this.status = status;
 | 
							this.status = status;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	@Override
 | 
						@Override
 | 
				
			||||||
	public String toString() { return String.format("User[id=%d,name=%s,status=%s]", getID(), getName(), status); }
 | 
						public String toString() { return String.format("User[id=%d,name=%s,status=%s,contacts=%s]", getID(), getName(), status, getContacts()); }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
	 * @return the current status of this user
 | 
						 * @return the current status of this user
 | 
				
			||||||
@@ -86,4 +113,26 @@ public class User extends Contact {
 | 
				
			|||||||
	 * @since Envoy Common v0.2-alpha
 | 
						 * @since Envoy Common v0.2-alpha
 | 
				
			||||||
	 */
 | 
						 */
 | 
				
			||||||
	public void setStatus(UserStatus status) { this.status = status; }
 | 
						public void setStatus(UserStatus status) { this.status = status; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						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());
 | 
				
			||||||
 | 
							} else outputStream.writeObject(new ArrayList<>());
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/**
 | 
				
			||||||
 | 
						 * @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; }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,5 +1,10 @@
 | 
				
			|||||||
package envoy.event;
 | 
					package envoy.event;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import java.util.HashSet;
 | 
				
			||||||
 | 
					import java.util.Set;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import envoy.data.User;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * This event creates a group with the given name.<br>
 | 
					 * This event creates a group with the given name.<br>
 | 
				
			||||||
 * <br>
 | 
					 * <br>
 | 
				
			||||||
@@ -12,11 +17,26 @@ package envoy.event;
 | 
				
			|||||||
 */
 | 
					 */
 | 
				
			||||||
public class GroupCreationEvent extends Event<String> {
 | 
					public class GroupCreationEvent extends Event<String> {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						private final Set<Long> initialMemberIDs;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	private static final long serialVersionUID = 0L;
 | 
						private static final long serialVersionUID = 0L;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
	 * @param value            the name of this group at creation time
 | 
						 * @param value            the name of this group at creation time
 | 
				
			||||||
 | 
						 * @param initialMemberIDs the IDs of all {@link User}s that should be group
 | 
				
			||||||
 | 
						 *                         members from the beginning on (excluding the creator
 | 
				
			||||||
 | 
						 *                         of this group)
 | 
				
			||||||
	 * @since Envoy Common v0.1-beta
 | 
						 * @since Envoy Common v0.1-beta
 | 
				
			||||||
	 */
 | 
						 */
 | 
				
			||||||
	public GroupCreationEvent(String value) { super(value); }
 | 
						public GroupCreationEvent(String value, Set<Long> initialMemberIDs) {
 | 
				
			||||||
 | 
							super(value);
 | 
				
			||||||
 | 
							this.initialMemberIDs = (initialMemberIDs != null) ? initialMemberIDs : new HashSet<>();
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/**
 | 
				
			||||||
 | 
						 * @return the IDs of all {@link User}s that are members from the beginning
 | 
				
			||||||
 | 
						 *         (excluding the creator of this group)
 | 
				
			||||||
 | 
						 * @since Envoy Common v0.1-beta
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						public Set<Long> getInitialMemberIDs() { return initialMemberIDs; }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -36,7 +36,7 @@ public class GroupResizeEvent extends Event<Long> {
 | 
				
			|||||||
	 */
 | 
						 */
 | 
				
			||||||
	public GroupResizeEvent(User user, Group group, ElementOperation operation) {
 | 
						public GroupResizeEvent(User user, Group group, ElementOperation operation) {
 | 
				
			||||||
		super(user.getID());
 | 
							super(user.getID());
 | 
				
			||||||
		if (group.getMemberIDs().contains(user.getID())) {
 | 
							if (group.getContacts().contains(user)) {
 | 
				
			||||||
			if (operation.equals(ElementOperation.ADD)) throw new IllegalStateException(
 | 
								if (operation.equals(ElementOperation.ADD)) throw new IllegalStateException(
 | 
				
			||||||
					"Cannot add " + user + " to group " + group.getID() + " because he is already a member of this group");
 | 
										"Cannot add " + user + " to group " + group.getID() + " because he is already a member of this group");
 | 
				
			||||||
		} else if (operation.equals(ElementOperation.REMOVE))
 | 
							} else if (operation.equals(ElementOperation.REMOVE))
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										35
									
								
								src/test/java/envoy/data/UserTest.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								src/test/java/envoy/data/UserTest.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,35 @@
 | 
				
			|||||||
 | 
					package envoy.data;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import static org.junit.jupiter.api.Assertions.assertEquals;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import java.io.IOException;
 | 
				
			||||||
 | 
					import java.util.Set;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import org.junit.jupiter.api.Test;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import envoy.data.User.UserStatus;
 | 
				
			||||||
 | 
					import envoy.util.SerializationUtils;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * Project: <strong>envoy-common</strong><br>
 | 
				
			||||||
 | 
					 * File: <strong>UserTest.java</strong><br>
 | 
				
			||||||
 | 
					 * Created: <strong>31 Mar 2020</strong><br>
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * @author Leon Hofmeister
 | 
				
			||||||
 | 
					 * @since Envoy Common v0.1-beta
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					class UserTest {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						@Test
 | 
				
			||||||
 | 
						void test() throws IOException, ClassNotFoundException {
 | 
				
			||||||
 | 
							User	user2				= new User(2, "kai");
 | 
				
			||||||
 | 
							User	user3				= new User(3, "ai");
 | 
				
			||||||
 | 
							User	user4				= new User(4, "ki", Set.of(user2, user3));
 | 
				
			||||||
 | 
							User	user5				= new User(5, "ka", Set.of(user2, user3, user4));
 | 
				
			||||||
 | 
							User	user				= new User(1, "maxi", UserStatus.AWAY, Set.of(user2, user3, user4, user5));
 | 
				
			||||||
 | 
							var		serializedUser		= SerializationUtils.writeToByteArray(user);
 | 
				
			||||||
 | 
							var		deserializedUser	= SerializationUtils.read(serializedUser, User.class);
 | 
				
			||||||
 | 
							assertEquals(user.getContacts(), deserializedUser.getContacts());
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user