From 6df4d75042bf7dc6737b5de8ef2bc9d3a79e9a89 Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Wed, 28 Dec 2022 17:48:01 +0100 Subject: [PATCH] refactor: A `Message` now explicitly has a `MessageKey` --- .../kafka/chat/backend/domain/Chatroom.java | 17 +++--------- .../kafka/chat/backend/domain/Message.java | 26 ++++++++++++++----- 2 files changed, 24 insertions(+), 19 deletions(-) diff --git a/src/main/java/de/juplo/kafka/chat/backend/domain/Chatroom.java b/src/main/java/de/juplo/kafka/chat/backend/domain/Chatroom.java index e1638aa9..df6794aa 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/domain/Chatroom.java +++ b/src/main/java/de/juplo/kafka/chat/backend/domain/Chatroom.java @@ -2,7 +2,6 @@ package de.juplo.kafka.chat.backend.domain; import lombok.Getter; import lombok.RequiredArgsConstructor; -import lombok.Value; import lombok.extern.slf4j.Slf4j; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; @@ -20,7 +19,7 @@ public class Chatroom private final UUID id; @Getter private final String name; - private final LinkedHashMap messages = new LinkedHashMap<>(); + private final LinkedHashMap messages = new LinkedHashMap<>(); private final Sinks.Many sink = Sinks.many().multicast().onBackpressureBuffer(); synchronized public Mono addMessage( @@ -39,9 +38,9 @@ public class Chatroom String user, String text) { - Message message = new Message(id, (long)messages.size(), timestamp, user, text); + Message.MessageKey key = Message.MessageKey.of(user, id); + Message message = new Message(key, (long)messages.size(), timestamp, text); - MessageKey key = new MessageKey(user, id); Message existing = messages.get(key); if (existing != null) { @@ -61,7 +60,7 @@ public class Chatroom { return Mono.fromSupplier(() -> { - MessageKey key = MessageKey.of(username, messageId); + Message.MessageKey key = Message.MessageKey.of(username, messageId); return messages.get(key); }); } @@ -82,12 +81,4 @@ public class Chatroom return serial >= first && serial <= last; })); } - - - @Value(staticConstructor = "of") - static class MessageKey - { - String username; - Long messageId; - } } diff --git a/src/main/java/de/juplo/kafka/chat/backend/domain/Message.java b/src/main/java/de/juplo/kafka/chat/backend/domain/Message.java index c5b738f1..c0dd861d 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/domain/Message.java +++ b/src/main/java/de/juplo/kafka/chat/backend/domain/Message.java @@ -1,9 +1,6 @@ package de.juplo.kafka.chat.backend.domain; -import lombok.EqualsAndHashCode; -import lombok.Getter; -import lombok.RequiredArgsConstructor; -import lombok.ToString; +import lombok.*; import java.time.LocalDateTime; @@ -14,9 +11,26 @@ import java.time.LocalDateTime; @ToString public class Message { - private final Long id; + private final MessageKey key; private final Long serialNumber; private final LocalDateTime timestamp; - private final String username; private final String messageText; + + public Long getId() + { + return key.messageId; + } + + public String getUsername() + { + return key.username; + } + + + @Value(staticConstructor = "of") + public static class MessageKey + { + String username; + Long messageId; + } } -- 2.20.1