Skip to content

Commit

Permalink
Added findMessageById in inbox
Browse files Browse the repository at this point in the history
  • Loading branch information
Robin Strand Prestmo authored and Robin Strand Prestmo committed Feb 20, 2026
1 parent 9b82784 commit 73fdb9e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
package ntnu.sytemutvikling.team6.models;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.*;

// Unntakshåndtering mangler
// Enhetstester mangler
// Message får kanskje en UUID, da er det mer fornuftig å bruke denne, vertfall for removeMessage

/**
* Represents a users inbox that contains messages.
* Provides methods to add, remove and get messages.
*
* @Author Robin Strand Prestmo
* @author Robin Strand Prestmo
*/
public class Inbox {
private final List<Message> messages;

/**
* Creates an empty inbox with no messages.
*/
public Inbox(){
public Inbox() {
this.messages = new ArrayList<>();
}

Expand All @@ -33,17 +30,35 @@ public List<Message> getMessages() {
return Collections.unmodifiableList(messages);
}

/**
* Finds a specific message by id.
*
* @param messageId is the ID to the message that is searching after.
* @return message with matching id if the id exists.
*/
public Optional<Message> findMessageById(UUID messageId) {
if (messageId == null) {
throw new IllegalArgumentException("MessageId cannot be null.");
}
return messages.stream().filter(message -> messageId.equals(message.getId())).findFirst();
}

/**
* Add´s message to the messages list.
*
* @param message to be added
*/
public void addMessage(Message message) {
messages.add(message);
}

/**
* Removes a message from the inbox list.
*
* @param message the message to be removed
* @param messageId the id to the message to be removed
* @return true if the message was removed, false if not found.
*/
public boolean removeMessage(Message message) {
return messages.remove(message);
public boolean removeMessage(UUID messageId) {
return messages.removeIf(message -> messageId.equals(message.getId()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// Enhetstester mangler

/**
* Represents a message
* Represents a message.
*
* @author Robin Strand Prestmo
*/
Expand Down Expand Up @@ -69,5 +69,4 @@ public String getContent() {
public LocalDateTime getTimeAndDate() {
return timeAndDate;
}

}

0 comments on commit 73fdb9e

Please sign in to comment.