Revised message related classes
Added GroupMessage class as subclass from Message and adjusted the MessageBuilder
This commit is contained in:
		
							
								
								
									
										46
									
								
								src/main/java/envoy/data/GroupMessage.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										46
									
								
								src/main/java/envoy/data/GroupMessage.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,46 @@
 | 
			
		||||
package envoy.data;
 | 
			
		||||
 | 
			
		||||
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ä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 Map<Long, MessageStatus> getMemberStatuses() { return memberStatuses; }
 | 
			
		||||
}
 | 
			
		||||
@@ -1,6 +1,7 @@
 | 
			
		||||
package envoy.data;
 | 
			
		||||
 | 
			
		||||
import java.util.Date;
 | 
			
		||||
import java.util.HashMap;
 | 
			
		||||
 | 
			
		||||
import envoy.data.Message.MessageStatus;
 | 
			
		||||
 | 
			
		||||
@@ -97,12 +98,22 @@ public class MessageBuilder {
 | 
			
		||||
	 * @since Envoy Common v0.2-alpha
 | 
			
		||||
	 */
 | 
			
		||||
	public Message build() {
 | 
			
		||||
		// Supplement default values
 | 
			
		||||
		supplyDefaults();
 | 
			
		||||
		return new Message(id, senderID, recipientID, creationDate, text, attachment, status, forwarded);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public GroupMessage buildGroupMessage(Group group) {
 | 
			
		||||
		if (group == null) throw new NullPointerException();
 | 
			
		||||
		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);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	private void supplyDefaults() {
 | 
			
		||||
		if (creationDate == null) creationDate = new Date();
 | 
			
		||||
		if (text == null) text = "";
 | 
			
		||||
		if (status == null) status = MessageStatus.WAITING;
 | 
			
		||||
 | 
			
		||||
		return new Message(id, senderID, recipientID, creationDate, text, attachment, status, forwarded);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user