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 c511e482d3
commit 2d99a3785e
13 changed files with 202 additions and 130 deletions

View File

@ -1,6 +1,7 @@
package envoy.data;
import java.io.Serializable;
import java.util.Objects;
/**
* 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 {
private final long id;
private String name;
private String name;
private static final long serialVersionUID = 0L;
@ -51,9 +52,24 @@ public abstract class Contact implements Serializable {
public void setName(String name) { this.name = name; }
/**
*
* {@inheritDoc}
*/
@Override
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

@ -1,60 +1,69 @@
package envoy.data;
import java.util.Collections;
import java.util.Date;
import java.util.Map;
/**
* Project: <strong>envoy-common</strong><br>
* File: <strong>GroupMessage.java</strong><br>
* Created: <strong>26.03.2020</strong><br>
*
* @author Maximilian K&auml;fer
* @since Envoy Common v0.1-beta
*/
public final class GroupMessage extends Message {
private final Map<Long, MessageStatus> memberStatuses;
private static final long serialVersionUID = 0L;
/**
* Initializes a {@link GroupMessage} with values for all of its properties. The
* use
* of this constructor is only intended for the {@link MessageBuilder} class, as
* this class provides {@code null} checks and default values for all
* properties.
*
* @param id unique ID
* @param senderID the ID of the user who sends the message
* @param recipientID the ID of the user who receives the message
* @param creationDate the creation date of the message
* @param text the text content of the message
* @param attachment the attachment of the message, if present
* @param status the current {@link MessageStatus} of the message
* @param forwarded whether this message was forwarded
* @param memberStatuses a map of all members and their status according to this
* {@link GroupMessage}
* @since Envoy Common v0.1-beta
*/
GroupMessage(long id, long senderID, long groupID, Date creationDate, String text, MessageAttachment<?> attachment, MessageStatus status,
boolean forwarded, Map<Long, MessageStatus> memberStatuses) {
super(id, senderID, groupID, creationDate, text, attachment, status, forwarded);
this.memberStatuses = memberStatuses;
}
public void updateStatus() {
setStatus(Collections.min(memberStatuses.values()));
switch (getStatus()) {
case RECEIVED:
setReceivedDate(new Date());
break;
case READ:
setReadDate(new Date());
break;
}
}
public Map<Long, MessageStatus> getMemberStatuses() { return memberStatuses; }
}
package envoy.data;
import java.util.Collections;
import java.util.Date;
import java.util.Map;
/**
* Project: <strong>envoy-common</strong><br>
* File: <strong>GroupMessage.java</strong><br>
* Created: <strong>26.03.2020</strong><br>
*
* @author Maximilian K&auml;fer
* @since Envoy Common v0.1-beta
*/
public final class GroupMessage extends Message {
private final Map<Long, MessageStatus> memberStatuses;
private static final long serialVersionUID = 0L;
/**
* Initializes a {@link GroupMessage} with values for all of its properties. The
* use
* of this constructor is only intended for the {@link MessageBuilder} class, as
* this class provides {@code null} checks and default values for all
* properties.
*
* @param id unique ID
* @param senderID the ID of the user who sends the message
* @param recipientID the ID of the user who receives the message
* @param creationDate the creation date of the message
* @param text the text content of the message
* @param attachment the attachment of the message, if present
* @param status the current {@link MessageStatus} of the message
* @param forwarded whether this message was forwarded
* @param memberStatuses a map of all members and their status according to this
* {@link GroupMessage}
* @since Envoy Common v0.1-beta
*/
GroupMessage(long id, long senderID, long groupID, Date creationDate, String text, MessageAttachment<?> attachment, MessageStatus status,
boolean forwarded, Map<Long, MessageStatus> memberStatuses) {
super(id, senderID, groupID, creationDate, text, attachment, status, forwarded);
this.memberStatuses = memberStatuses;
}
/**
* Sets the status to be the minimum of all members.
*
* @since Envoy Common v0.1-beta
*/
public void updateStatus() {
setStatus(Collections.min(memberStatuses.values()));
switch (getStatus()) {
case RECEIVED:
setReceivedDate(new Date());
break;
case READ:
setReadDate(new Date());
break;
}
}
/**
* @return the map of all statuses in this {@link GroupMessage}
* @since Envoy Common v0.1-beta
*/
public Map<Long, MessageStatus> getMemberStatuses() { return memberStatuses; }
}

View File

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

View File

@ -102,6 +102,31 @@ public class MessageBuilder {
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) {
if (group == null) throw new NullPointerException();
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;
/**
* 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>
* File: <strong>GroupCreationEvent.java</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> {
private final long groupID;
private final Operation operation;
private final long groupID;
private final ElementOperation operation;
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
* already been set.
*
* @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 operation whether the user should be removed from this group. If
* false, this user will be added.
* @param operation describes what to do with the given user:<br>
* add him to this group or remove him from it
* @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());
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");
} 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");
groupID = group.getID();
groupID = group.getID();
this.operation = operation;
}
@ -80,7 +55,7 @@ public class GroupResizeEvent extends Event<Long> {
* @return the operationType
* @since Envoy Common v0.1-beta
*/
public Operation getOperation() { return operation; }
public ElementOperation getOperation() { return operation; }
/**
* {@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.event.ElementOperation;
import envoy.event.Event;
/**
* Signifies the modification of a contact list.<br>
@ -14,32 +16,9 @@ import envoy.data.User;
*/
public class ContactOperationEvent extends Event<User> {
/**
* 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 final ElementOperation operationType;
/**
* 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;
private static final long serialVersionUID = 1L;
/**
* Initializes a {@link ContactOperationEvent}.
@ -48,7 +27,7 @@ public class ContactOperationEvent extends Event<User> {
* @param operationType the type of operation to perform
* @since Envoy Common v0.2-alpha
*/
public ContactOperationEvent(User contact, Operation operationType) {
public ContactOperationEvent(User contact, ElementOperation operationType) {
super(contact);
this.operationType = operationType;
}
@ -57,5 +36,5 @@ public class ContactOperationEvent extends Event<User> {
* @return the type of operation to perform
* @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>

View File

@ -1,8 +1,9 @@
package envoy.event;
package envoy.event.contact;
import java.util.List;
import envoy.data.User;
import envoy.event.Event;
/**
* 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.exception;
exports envoy.event;
exports envoy.event.contact;
requires transitive java.logging;
}