DS-Lab / src / main / java / dslab / protocol / Message.java
Message.java
Raw
package dslab.protocol;

import dslab.routing.Address;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toUnmodifiableList;

/**
 * Represents a message. Used throughout all server impls, as well as the DMTP and DMAP impls.
 */
public class Message {

    protected final List<Address> recipients = new ArrayList<>();
    private Address sender;
    private String subject;
    private String data;
    private String hash;

    //copy constructor
    public Message(Message message) {
        this.recipients.addAll(message.getRecipients());
        this.sender = message.getSender();
        this.subject = message.getSubject();
        this.data = message.getData();
        this.hash = message.getHash();
    }

    public Message() {}

    public void addRecipients(Collection<Address> addresses) {
        recipients.addAll(addresses);
    }

    public Integer countRecipients() {
        return recipients.size();
    }

    public List<Address> getRecipients() {
        return recipients;
    }

    public void setSender(Address sender) {
        this.sender = sender;
    }

    public Address getSender() {
        return sender;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public String getSubject() {
        return subject;
    }

    public void setData(String data) {
        this.data = data;
    }

    public String getData() {
        return data;
    }

    /**
     * @return a string representation fit for logging
     */
    @Override
    public String toString() { //use for logging
        return getClass().getSimpleName() + "{" + toGuiString().replace("\n"," ") + '}';
    }

    /**
     * @return a string representation fit for output on the console
     */
    public String toGuiString(){
        return "from " + sender + "\n" +
                "to " + recipients.stream().map(Address::toString).collect(joining(",")) + "\n" +
                "subject " + subject + "\n" +
                "data " + data + "\n" +
                "hash " + hash + "\n";
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Message that = (Message) o;
        return Objects.equals(recipients, that.recipients) && Objects.equals(sender, that.sender) && Objects.equals(subject, that.subject) && Objects.equals(data, that.data);
    }

    @Override
    public int hashCode() {
        return Objects.hash(recipients, sender, subject, data);
    }

    /**
     * @return names of the missing fields
     */
    public List<String> getMissingFields() {
        List<String> missingFieldNames = new ArrayList<>();

        if (recipients.isEmpty())
            missingFieldNames.add("to");

        if (sender == null)
            missingFieldNames.add("from");

        if (subject == null || subject.isEmpty())
            missingFieldNames.add("subject");

        if (data == null || data.isEmpty())
            missingFieldNames.add("data");

        return missingFieldNames;
    }

    public boolean hasMissingFields() {
        return !getMissingFields().isEmpty();
    }

    public void setRecipients(Collection<Address> recipientsArg) {
        recipients.clear();
        recipients.addAll(recipientsArg);
    }

    public void setHash(String hash) {
        this.hash = hash;
    }

    public String getHash() {
        return hash;
    }
}