refactore: Renamed `PersistenceStrategy` to `ChatroomService` -- Move
authorKai Moritz <kai@juplo.de>
Sun, 8 Jan 2023 09:10:34 +0000 (10:10 +0100)
committerKai Moritz <kai@juplo.de>
Sun, 15 Jan 2023 18:35:59 +0000 (19:35 +0100)
src/main/java/de/juplo/kafka/chat/backend/domain/ChatroomService.java [new file with mode: 0644]
src/main/java/de/juplo/kafka/chat/backend/domain/PersistenceStrategy.java [deleted file]
src/main/java/de/juplo/kafka/chat/backend/persistence/InMemoryChatroomService.java [new file with mode: 0644]
src/main/java/de/juplo/kafka/chat/backend/persistence/InMemoryPersistenceStrategy.java [deleted file]

diff --git a/src/main/java/de/juplo/kafka/chat/backend/domain/ChatroomService.java b/src/main/java/de/juplo/kafka/chat/backend/domain/ChatroomService.java
new file mode 100644 (file)
index 0000000..d3a8364
--- /dev/null
@@ -0,0 +1,19 @@
+package de.juplo.kafka.chat.backend.domain;
+
+import reactor.core.publisher.Flux;
+import reactor.core.publisher.Mono;
+
+import java.time.LocalDateTime;
+
+
+public interface PersistenceStrategy
+{
+  Mono<Message> persistMessage(
+      Message.MessageKey key,
+      LocalDateTime timestamp,
+      String text);
+
+  Mono<Message> getMessage(Message.MessageKey key);
+
+  Flux<Message> getMessages(long first, long last);
+}
diff --git a/src/main/java/de/juplo/kafka/chat/backend/domain/PersistenceStrategy.java b/src/main/java/de/juplo/kafka/chat/backend/domain/PersistenceStrategy.java
deleted file mode 100644 (file)
index d3a8364..0000000
+++ /dev/null
@@ -1,19 +0,0 @@
-package de.juplo.kafka.chat.backend.domain;
-
-import reactor.core.publisher.Flux;
-import reactor.core.publisher.Mono;
-
-import java.time.LocalDateTime;
-
-
-public interface PersistenceStrategy
-{
-  Mono<Message> persistMessage(
-      Message.MessageKey key,
-      LocalDateTime timestamp,
-      String text);
-
-  Mono<Message> getMessage(Message.MessageKey key);
-
-  Flux<Message> getMessages(long first, long last);
-}
diff --git a/src/main/java/de/juplo/kafka/chat/backend/persistence/InMemoryChatroomService.java b/src/main/java/de/juplo/kafka/chat/backend/persistence/InMemoryChatroomService.java
new file mode 100644 (file)
index 0000000..4b522a8
--- /dev/null
@@ -0,0 +1,76 @@
+package de.juplo.kafka.chat.backend.persistence;
+
+import de.juplo.kafka.chat.backend.domain.Message;
+import de.juplo.kafka.chat.backend.domain.MessageMutationException;
+import de.juplo.kafka.chat.backend.domain.PersistenceStrategy;
+import lombok.extern.slf4j.Slf4j;
+import reactor.core.publisher.Flux;
+import reactor.core.publisher.Mono;
+
+import java.time.LocalDateTime;
+import java.util.LinkedHashMap;
+
+
+@Slf4j
+public class InMemoryPersistenceStrategy implements PersistenceStrategy
+{
+  private final LinkedHashMap<Message.MessageKey, Message> messages;
+
+
+  public InMemoryPersistenceStrategy(LinkedHashMap<Message.MessageKey, Message> messages)
+  {
+    this.messages = messages;
+  }
+
+  public InMemoryPersistenceStrategy(Flux<Message> messageFlux)
+  {
+    log.debug("Creating InMemoryPersistenceStrategy");
+    messages = new LinkedHashMap<>();
+    messageFlux.subscribe(message -> persistMessage(message));
+  }
+
+  @Override
+  public Mono<Message> persistMessage(
+      Message.MessageKey key,
+      LocalDateTime timestamp,
+      String text)
+  {
+    Message message = new Message(key, (long)messages.size(), timestamp, text);
+    return Mono.justOrEmpty(persistMessage(message));
+  }
+
+  private Message persistMessage(Message message)
+  {
+    Message.MessageKey key = message.getKey();
+    Message existing = messages.get(key);
+    if (existing != null)
+    {
+      log.info("Message with key {} already exists; {}", key, existing);
+      if (!message.equals(existing))
+        throw new MessageMutationException(message, existing);
+      return null;
+    }
+
+    messages.put(key, message);
+    return message;
+  }
+
+  @Override
+  public Mono<Message> getMessage(Message.MessageKey key)
+  {
+    return Mono.fromSupplier(() -> messages.get(key));
+  }
+
+  @Override
+  public Flux<Message> getMessages(long first, long last)
+  {
+    return Flux.fromStream(messages
+        .values()
+        .stream()
+        .filter(message ->
+        {
+          long serial = message.getSerialNumber();
+          return serial >= first && serial <= last;
+        }));
+  }
+}
diff --git a/src/main/java/de/juplo/kafka/chat/backend/persistence/InMemoryPersistenceStrategy.java b/src/main/java/de/juplo/kafka/chat/backend/persistence/InMemoryPersistenceStrategy.java
deleted file mode 100644 (file)
index 4b522a8..0000000
+++ /dev/null
@@ -1,76 +0,0 @@
-package de.juplo.kafka.chat.backend.persistence;
-
-import de.juplo.kafka.chat.backend.domain.Message;
-import de.juplo.kafka.chat.backend.domain.MessageMutationException;
-import de.juplo.kafka.chat.backend.domain.PersistenceStrategy;
-import lombok.extern.slf4j.Slf4j;
-import reactor.core.publisher.Flux;
-import reactor.core.publisher.Mono;
-
-import java.time.LocalDateTime;
-import java.util.LinkedHashMap;
-
-
-@Slf4j
-public class InMemoryPersistenceStrategy implements PersistenceStrategy
-{
-  private final LinkedHashMap<Message.MessageKey, Message> messages;
-
-
-  public InMemoryPersistenceStrategy(LinkedHashMap<Message.MessageKey, Message> messages)
-  {
-    this.messages = messages;
-  }
-
-  public InMemoryPersistenceStrategy(Flux<Message> messageFlux)
-  {
-    log.debug("Creating InMemoryPersistenceStrategy");
-    messages = new LinkedHashMap<>();
-    messageFlux.subscribe(message -> persistMessage(message));
-  }
-
-  @Override
-  public Mono<Message> persistMessage(
-      Message.MessageKey key,
-      LocalDateTime timestamp,
-      String text)
-  {
-    Message message = new Message(key, (long)messages.size(), timestamp, text);
-    return Mono.justOrEmpty(persistMessage(message));
-  }
-
-  private Message persistMessage(Message message)
-  {
-    Message.MessageKey key = message.getKey();
-    Message existing = messages.get(key);
-    if (existing != null)
-    {
-      log.info("Message with key {} already exists; {}", key, existing);
-      if (!message.equals(existing))
-        throw new MessageMutationException(message, existing);
-      return null;
-    }
-
-    messages.put(key, message);
-    return message;
-  }
-
-  @Override
-  public Mono<Message> getMessage(Message.MessageKey key)
-  {
-    return Mono.fromSupplier(() -> messages.get(key));
-  }
-
-  @Override
-  public Flux<Message> getMessages(long first, long last)
-  {
-    return Flux.fromStream(messages
-        .values()
-        .stream()
-        .filter(message ->
-        {
-          long serial = message.getSerialNumber();
-          return serial >= first && serial <= last;
-        }));
-  }
-}