Skip to content

Commit

Permalink
Added exeption handling to 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 73fdb9e commit b143d6a
Showing 1 changed file with 14 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// Enhetstester mangler

/**
* Represents a users inbox that contains messages.
* Represents a user's inbox that contains messages.
* Provides methods to add, remove and get messages.
*
* @author Robin Strand Prestmo
Expand All @@ -24,7 +24,7 @@ public Inbox() {
/**
* Returns an unmodifiable view of the messages in this inbox.
*
* @return an unmodifiable list of messages.
* @return an unmodifiable list of messages
*/
public List<Message> getMessages() {
return Collections.unmodifiableList(messages);
Expand All @@ -33,8 +33,9 @@ public List<Message> getMessages() {
/**
* 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.
* @param messageId the id of the message to search for
* @return messageId the id of the message to remove
* @throws IllegalArgumentException if messageId is null
*/
public Optional<Message> findMessageById(UUID messageId) {
if (messageId == null) {
Expand All @@ -47,18 +48,26 @@ public Optional<Message> findMessageById(UUID messageId) {
* Add´s message to the messages list.
*
* @param message to be added
* @throws IllegalArgumentException if message is null
*/
public void addMessage(Message message) {
if (message == null) {
throw new IllegalArgumentException("Message cannot be null");
}
messages.add(message);
}

/**
* Removes a message from the inbox list.
*
* @param messageId the id to the message to be removed
* @return true if the message was removed, false if not found.
* @return true if the message was removed, false if not found
* @throws IllegalArgumentException if messageId is null
*/
public boolean removeMessage(UUID messageId) {
if (messageId == null) {
throw new IllegalArgumentException("MessageId cannot be null");
}
return messages.removeIf(message -> messageId.equals(message.getId()));
}
}

0 comments on commit b143d6a

Please sign in to comment.