Created ContactDeletionEvent and equals- + hashcode- method for Contact

additionally extracted enum ElementOperation and fixed wrong
toString-method in Message
This commit is contained in:
delvh 2020-03-28 15:21:02 +01:00
parent cf9198781f
commit f93b01f65a
13 changed files with 202 additions and 130 deletions

View File

@ -1,6 +1,7 @@
package envoy.data; package envoy.data;
import java.io.Serializable; import java.io.Serializable;
import java.util.Objects;
/** /**
* 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>
@ -16,7 +17,7 @@ import java.io.Serializable;
public abstract class Contact implements Serializable { public abstract class Contact implements Serializable {
private final long id; private final long id;
private String name; private String name;
private static final long serialVersionUID = 0L; private static final long serialVersionUID = 0L;
@ -51,9 +52,24 @@ public abstract class Contact implements Serializable {
public void setName(String name) { this.name = name; } public void setName(String name) { this.name = name; }
/** /**
*
* {@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]", id, name); }
/**
* {@inheritDoc}
*/
@Override
public int hashCode() { return Objects.hash(id); }
/**
* {@inheritDoc}
*/
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (!(obj instanceof Contact)) return false;
return id == ((Contact) obj).id;
}
} }

View File

@ -43,6 +43,11 @@ public final class GroupMessage extends Message {
this.memberStatuses = memberStatuses; this.memberStatuses = memberStatuses;
} }
/**
* Sets the status to be the minimum of all members.
*
* @since Envoy Common v0.1-beta
*/
public void updateStatus() { public void updateStatus() {
setStatus(Collections.min(memberStatuses.values())); setStatus(Collections.min(memberStatuses.values()));
switch (getStatus()) { switch (getStatus()) {
@ -56,5 +61,9 @@ public final class GroupMessage extends Message {
} }
} }
/**
* @return the map of all statuses in this {@link GroupMessage}
* @since Envoy Common v0.1-beta
*/
public Map<Long, MessageStatus> getMemberStatuses() { return memberStatuses; } public Map<Long, MessageStatus> getMemberStatuses() { return memberStatuses; }
} }

View File

@ -107,8 +107,7 @@ public class Message implements Serializable {
@Override @Override
public String toString() { public String toString() {
return String.format( return String.format("Message[id=%d,sender=%s,recipient=%s,date=%s,status=%s,text=%s,forwarded=%b,hasAttachment=%b]",
"TextMessage[id=%d,sender=%s,recipient=%s,date=%s,status=%s,text=%s,forwarded,hasAttachment=%b]",
id, id,
senderID, senderID,
recipientID, recipientID,

View File

@ -102,6 +102,31 @@ 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. 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>
* <tr>
* <td>{@code status}</td>
* <td>{@code MessageStatus.WAITING}</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) { public GroupMessage buildGroupMessage(Group group) {
if (group == null) throw new NullPointerException(); if (group == null) throw new NullPointerException();
supplyDefaults(); supplyDefaults();

View File

@ -0,0 +1,26 @@
package envoy.event;
/**
* This enum declares all modification possibilities for a given container.<br>
* These can be: {@link ElementOperation#ADD} or
* {@link ElementOperation#REMOVE}.<br>
* <br>
* Project: <strong>envoy-common</strong><br>
* File: <strong>ElementOperation.java</strong><br>
* Created: <strong>25 Mar 2020</strong><br>
*
* @author Leon Hofmeister
* @since Envoy Common v0.1-beta
*/
public enum ElementOperation {
/**
* Select this element, if the given element should be added to the given
* container
*/
ADD,
/**
* Select this element, if the given element should be removed from the given
* container
*/
REMOVE
}

View File

@ -1,8 +1,8 @@
package envoy.event; package envoy.event;
/** /**
* This event creates a group with the given name. * This event creates a group with the given name.<br>
* * <br>
* Project: <strong>envoy-common</strong><br> * Project: <strong>envoy-common</strong><br>
* File: <strong>GroupCreationEvent.java</strong><br> * File: <strong>GroupCreationEvent.java</strong><br>
* Created: <strong>25 Mar 2020</strong><br> * Created: <strong>25 Mar 2020</strong><br>

View File

@ -19,54 +19,29 @@ import envoy.data.User;
*/ */
public class GroupResizeEvent extends Event<Long> { public class GroupResizeEvent extends Event<Long> {
private final long groupID; private final long groupID;
private final Operation operation; private final ElementOperation operation;
private static final long serialVersionUID = 0L; private static final long serialVersionUID = 0L;
/**
* This enum defines all possibilities for handling
* {@link GroupResizeEvent}s.<br>
* These can be: {@link Operation#ADD} or {@link Operation#REMOVE}.<br>
* <br>
* Project: <strong>envoy-common</strong><br>
* File: <strong>GroupResizeEvent.java</strong><br>
* Created: <strong>25 Mar 2020</strong><br>
*
* @author Leon Hofmeister
* @since Envoy Common v0.1-beta
*/
public enum Operation {
/**
* Select this element, if the given {@link User} should be added to the given
* {@link Group}
*/
ADD,
/**
* Select this element, if the given {@link User} should be removed from the
* given {@link Group}
*/
REMOVE
}
/** /**
* Initializes a {@link GroupResizeEvent} through a Contact where the name has * Initializes a {@link GroupResizeEvent} through a Contact where the name has
* already been set. * already been set.
* *
* @param user the {@link User} who wants to join or leave a group * @param user the {@link User} who wants to join or leave a group
* @param group the {@link Group} he wants to join or leave * @param group the {@link Group} he wants to join or leave
* @param operation whether the user should be removed from this group. If * @param operation describes what to do with the given user:<br>
* false, this user will be added. * add him to this group or remove him from it
* @since Envoy Common v0.2-alpha * @since Envoy Common v0.2-alpha
*/ */
public GroupResizeEvent(User user, Group group, Operation operation) { public GroupResizeEvent(User user, Group group, ElementOperation operation) {
super(user.getID()); super(user.getID());
if (group.getMemberIDs().contains(user.getID())) { if (group.getMemberIDs().contains(user.getID())) {
if (operation.equals(Operation.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(Operation.REMOVE)) } else if (operation.equals(ElementOperation.REMOVE))
throw new IllegalStateException("Cannot remove " + user + " from group " + group.getID() + " because he is no part of this group"); throw new IllegalStateException("Cannot remove " + user + " from group " + group.getID() + " because he is no part of this group");
groupID = group.getID(); groupID = group.getID();
this.operation = operation; this.operation = operation;
} }
@ -80,7 +55,7 @@ public class GroupResizeEvent extends Event<Long> {
* @return the operationType * @return the operationType
* @since Envoy Common v0.1-beta * @since Envoy Common v0.1-beta
*/ */
public Operation getOperation() { return operation; } public ElementOperation getOperation() { return operation; }
/** /**
* {@inheritDoc} * {@inheritDoc}

View File

@ -0,0 +1,26 @@
package envoy.event.contact;
import envoy.data.Contact;
import envoy.event.Event;
/**
* This event is sent if a {@link Contact} has been deleted/ has decided to
* delete himself.<br>
* <br>
* Project: <strong>envoy-common</strong><br>
* File: <strong>ContactDeletionEvent.java</strong><br>
* Created: <strong>28 Mar 2020</strong><br>
*
* @author Leon Hofmeister
* @since Envoy Common v0.1-beta
*/
public class ContactDeletionEvent extends Event<Long> {
private static final long serialVersionUID = 0L;
/**
* @param value the id of the {@link Contact} being deleted
* @since Envoy Common v0.1-beta
*/
protected ContactDeletionEvent(Long value) { super(value); }
}

View File

@ -1,6 +1,8 @@
package envoy.event; package envoy.event.contact;
import envoy.data.User; import envoy.data.User;
import envoy.event.ElementOperation;
import envoy.event.Event;
/** /**
* Signifies the modification of a contact list.<br> * Signifies the modification of a contact list.<br>
@ -14,32 +16,9 @@ import envoy.data.User;
*/ */
public class ContactOperationEvent extends Event<User> { public class ContactOperationEvent extends Event<User> {
/** private final ElementOperation operationType;
* Specifies the operation performed on a contact list.<br>
* <br>
* Project: <strong>envoy-common</strong><br>
* File: <strong>ContactOperationEvent.java</strong><br>
* Created: <strong>05.02.2020</strong><br>
*
* @author Maximilian K&auml;fer
* @since Envoy Common v0.2-alpha
*/
public enum Operation {
/** private static final long serialVersionUID = 1L;
* Adds a user to the contact list.
*/
ADD,
/**
* Removes a user from the contact list.
*/
REMOVE;
}
private final Operation operationType;
private static final long serialVersionUID = 0L;
/** /**
* Initializes a {@link ContactOperationEvent}. * Initializes a {@link ContactOperationEvent}.
@ -48,7 +27,7 @@ public class ContactOperationEvent extends Event<User> {
* @param operationType the type of operation to perform * @param operationType the type of operation to perform
* @since Envoy Common v0.2-alpha * @since Envoy Common v0.2-alpha
*/ */
public ContactOperationEvent(User contact, Operation operationType) { public ContactOperationEvent(User contact, ElementOperation operationType) {
super(contact); super(contact);
this.operationType = operationType; this.operationType = operationType;
} }
@ -57,5 +36,5 @@ public class ContactOperationEvent extends Event<User> {
* @return the type of operation to perform * @return the type of operation to perform
* @since Envoy Common v0.2-alpha * @since Envoy Common v0.2-alpha
*/ */
public Operation getOperationType() { return operationType; } public ElementOperation getOperationType() { return operationType; }
} }

View File

@ -1,4 +1,6 @@
package envoy.event; package envoy.event.contact;
import envoy.event.Event;
/** /**
* Requests a contact search from the server.<br> * Requests a contact search from the server.<br>

View File

@ -1,8 +1,9 @@
package envoy.event; package envoy.event.contact;
import java.util.List; import java.util.List;
import envoy.data.User; import envoy.data.User;
import envoy.event.Event;
/** /**
* Contains a list of {@link User}s for which a search was performed.<br> * Contains a list of {@link User}s for which a search was performed.<br>

View File

@ -0,0 +1,13 @@
/**
* This package contains all contact-related events.<br>
* <br>
* Project: <strong>envoy-common</strong><br>
* File: <strong>package-info.java</strong><br>
* Created: <strong>28 Mar 2020</strong><br>
*
* @author Leon Hofmeister
* @author Maximilian K&auml;fer
* @author Kai S.K. Engelbart
* @since Envoy Common v0.1-beta
*/
package envoy.event.contact;

View File

@ -13,6 +13,7 @@ module envoy.common {
exports envoy.util; exports envoy.util;
exports envoy.exception; exports envoy.exception;
exports envoy.event; exports envoy.event;
exports envoy.event.contact;
requires transitive java.logging; requires transitive java.logging;
} }