From: Kai Moritz Date: Wed, 28 Dec 2022 16:48:01 +0000 (+0100) Subject: refactor: A `Message` now explicitly has a `MessageKey` X-Git-Tag: TEST~62 X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=db9da0fec6409fe7b8ff46a039e3403f0ea0e833;p=demos%2Fkafka%2Fchat refactor: A `Message` now explicitly has a `MessageKey` --- 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; + } }