refactore: Renamed `Chatroom` to `ChatRoom` -- Rename
authorKai Moritz <kai@juplo.de>
Sun, 8 Jan 2023 09:19:47 +0000 (10:19 +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/api/ChatBackendController.java
src/main/java/de/juplo/kafka/chat/backend/api/ChatroomTo.java
src/main/java/de/juplo/kafka/chat/backend/domain/ChatHome.java
src/main/java/de/juplo/kafka/chat/backend/domain/ChatHomeService.java
src/main/java/de/juplo/kafka/chat/backend/domain/ChatRoom.java [new file with mode: 0644]
src/main/java/de/juplo/kafka/chat/backend/domain/Chatroom.java [deleted file]
src/main/java/de/juplo/kafka/chat/backend/persistence/InMemoryChatHomeService.java
src/main/java/de/juplo/kafka/chat/backend/persistence/LocalJsonFilesStorageStrategy.java
src/main/java/de/juplo/kafka/chat/backend/persistence/StorageStrategy.java

index 12f8d67..e7fc483 100644 (file)
@@ -1,10 +1,9 @@
 package de.juplo.kafka.chat.backend.api;
 
 import de.juplo.kafka.chat.backend.domain.ChatHome;
-import de.juplo.kafka.chat.backend.domain.Chatroom;
+import de.juplo.kafka.chat.backend.domain.ChatRoom;
 import de.juplo.kafka.chat.backend.persistence.StorageStrategy;
 import lombok.RequiredArgsConstructor;
-import org.springframework.http.MediaType;
 import org.springframework.http.codec.ServerSentEvent;
 import org.springframework.web.bind.annotation.*;
 import reactor.core.publisher.Flux;
@@ -70,7 +69,7 @@ public class ChatBackendController
   }
 
   public Mono<MessageTo> put(
-      Chatroom chatroom,
+      ChatRoom chatroom,
       String username,
       Long messageId,
       String text)
@@ -100,7 +99,7 @@ public class ChatBackendController
   }
 
   private Mono<MessageTo> get(
-      Chatroom chatroom,
+      ChatRoom chatroom,
       String username,
       Long messageId)
   {
@@ -119,7 +118,7 @@ public class ChatBackendController
         .orElseThrow(() -> new UnknownChatroomException(chatroomId));
   }
 
-  private Flux<ServerSentEvent<MessageTo>> listen(Chatroom chatroom)
+  private Flux<ServerSentEvent<MessageTo>> listen(ChatRoom chatroom)
   {
     return chatroom
         .listen()
index c4ad784..773c968 100644 (file)
@@ -1,6 +1,6 @@
 package de.juplo.kafka.chat.backend.api;
 
-import de.juplo.kafka.chat.backend.domain.Chatroom;
+import de.juplo.kafka.chat.backend.domain.ChatRoom;
 import lombok.Data;
 
 import java.util.UUID;
@@ -12,7 +12,7 @@ public class ChatroomTo
   private String name;
 
 
-  public static ChatroomTo from(Chatroom chatroom)
+  public static ChatroomTo from(ChatRoom chatroom)
   {
     ChatroomTo info = new ChatroomTo();
     info.id = chatroom.getId();
index 773cd4c..b31dede 100644 (file)
@@ -10,10 +10,10 @@ import java.util.stream.Stream;
 @Slf4j
 public class ChatHome
 {
-  private final Map<UUID, Chatroom> chatrooms;
+  private final Map<UUID, ChatRoom> chatrooms;
   private final ChatHomeService service;
 
-  public ChatHome(ChatHomeService service, Flux<Chatroom> chatroomFlux)
+  public ChatHome(ChatHomeService service, Flux<ChatRoom> chatroomFlux)
   {
     log.debug("Creating ChatHome with factory: {}", service);
     this.service = service;
@@ -21,19 +21,19 @@ public class ChatHome
     chatroomFlux.subscribe(chatroom -> chatrooms.put(chatroom.getId(), chatroom));
   }
 
-  public Chatroom createChatroom(String name)
+  public ChatRoom createChatroom(String name)
   {
-    Chatroom chatroom = service.createChatroom(UUID.randomUUID(), name);
+    ChatRoom chatroom = service.createChatroom(UUID.randomUUID(), name);
     chatrooms.put(chatroom.getId(), chatroom);
     return chatroom;
   }
 
-  public Optional<Chatroom> getChatroom(UUID id)
+  public Optional<ChatRoom> getChatroom(UUID id)
   {
     return Optional.ofNullable(chatrooms.get(id));
   }
 
-  public Stream<Chatroom> list()
+  public Stream<ChatRoom> list()
   {
     return chatrooms.values().stream();
   }
index 987839c..5508f24 100644 (file)
@@ -5,5 +5,5 @@ import java.util.UUID;
 
 public interface ChatHomeService<T extends ChatroomService>
 {
-  Chatroom createChatroom(UUID id, String name);
+  ChatRoom createChatroom(UUID id, String name);
 }
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
new file mode 100644 (file)
index 0000000..bed91c7
--- /dev/null
@@ -0,0 +1,87 @@
+package de.juplo.kafka.chat.backend.domain;
+
+import lombok.Getter;
+import lombok.extern.slf4j.Slf4j;
+import reactor.core.publisher.Flux;
+import reactor.core.publisher.Mono;
+import reactor.core.publisher.Sinks;
+
+import java.time.LocalDateTime;
+import java.util.*;
+
+
+@Slf4j
+public class ChatRoom
+{
+  @Getter
+  private final UUID id;
+  @Getter
+  private final String name;
+  private final ChatroomService chatroomService;
+  private final int bufferSize;
+  private Sinks.Many<Message> sink;
+
+  public ChatRoom(
+      UUID id,
+      String name,
+      ChatroomService chatroomService,
+      int bufferSize)
+  {
+    this.id = id;
+    this.name = name;
+    this.chatroomService = chatroomService;
+    this.bufferSize = bufferSize;
+    this.sink = createSink();
+  }
+
+
+  synchronized public Mono<Message> addMessage(
+      Long id,
+      LocalDateTime timestamp,
+      String user,
+      String text)
+  {
+    return chatroomService
+        .persistMessage(Message.MessageKey.of(user, id), timestamp, text)
+        .doOnNext(message ->
+        {
+          Sinks.EmitResult result = sink.tryEmitNext(message);
+          if (result.isFailure())
+          {
+            log.warn("Emitting of message failed with {} for {}", result.name(), message);
+          }
+        });
+  }
+
+
+  public Mono<Message> getMessage(String username, Long messageId)
+  {
+    Message.MessageKey key = Message.MessageKey.of(username, messageId);
+    return chatroomService.getMessage(key);
+  }
+
+  synchronized public Flux<Message> listen()
+  {
+    return sink
+        .asFlux()
+        .doOnCancel(() -> sink = createSink()); // Sink hast to be recreated on auto-cancel!
+  }
+
+  public Flux<Message> getMessages()
+  {
+    return getMessages(0, Long.MAX_VALUE);
+  }
+
+  public Flux<Message> getMessages(long first, long last)
+  {
+    return chatroomService.getMessages(first, last);
+  }
+
+  private Sinks.Many<Message> createSink()
+  {
+    return Sinks
+        .many()
+        .multicast()
+        .onBackpressureBuffer(bufferSize);
+  }
+}
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
deleted file mode 100644 (file)
index 2261e02..0000000
+++ /dev/null
@@ -1,87 +0,0 @@
-package de.juplo.kafka.chat.backend.domain;
-
-import lombok.Getter;
-import lombok.extern.slf4j.Slf4j;
-import reactor.core.publisher.Flux;
-import reactor.core.publisher.Mono;
-import reactor.core.publisher.Sinks;
-
-import java.time.LocalDateTime;
-import java.util.*;
-
-
-@Slf4j
-public class Chatroom
-{
-  @Getter
-  private final UUID id;
-  @Getter
-  private final String name;
-  private final ChatroomService chatroomService;
-  private final int bufferSize;
-  private Sinks.Many<Message> sink;
-
-  public Chatroom(
-      UUID id,
-      String name,
-      ChatroomService chatroomService,
-      int bufferSize)
-  {
-    this.id = id;
-    this.name = name;
-    this.chatroomService = chatroomService;
-    this.bufferSize = bufferSize;
-    this.sink = createSink();
-  }
-
-
-  synchronized public Mono<Message> addMessage(
-      Long id,
-      LocalDateTime timestamp,
-      String user,
-      String text)
-  {
-    return chatroomService
-        .persistMessage(Message.MessageKey.of(user, id), timestamp, text)
-        .doOnNext(message ->
-        {
-          Sinks.EmitResult result = sink.tryEmitNext(message);
-          if (result.isFailure())
-          {
-            log.warn("Emitting of message failed with {} for {}", result.name(), message);
-          }
-        });
-  }
-
-
-  public Mono<Message> getMessage(String username, Long messageId)
-  {
-    Message.MessageKey key = Message.MessageKey.of(username, messageId);
-    return chatroomService.getMessage(key);
-  }
-
-  synchronized public Flux<Message> listen()
-  {
-    return sink
-        .asFlux()
-        .doOnCancel(() -> sink = createSink()); // Sink hast to be recreated on auto-cancel!
-  }
-
-  public Flux<Message> getMessages()
-  {
-    return getMessages(0, Long.MAX_VALUE);
-  }
-
-  public Flux<Message> getMessages(long first, long last)
-  {
-    return chatroomService.getMessages(first, last);
-  }
-
-  private Sinks.Many<Message> createSink()
-  {
-    return Sinks
-        .many()
-        .multicast()
-        .onBackpressureBuffer(bufferSize);
-  }
-}
index 5c7c4b9..006f976 100644 (file)
@@ -1,6 +1,6 @@
 package de.juplo.kafka.chat.backend.persistence;
 
-import de.juplo.kafka.chat.backend.domain.Chatroom;
+import de.juplo.kafka.chat.backend.domain.ChatRoom;
 import de.juplo.kafka.chat.backend.domain.ChatHomeService;
 import lombok.RequiredArgsConstructor;
 
@@ -15,18 +15,18 @@ public class InMemoryChatHomeService implements ChatHomeService<InMemoryChatroom
 
 
   @Override
-  public Chatroom createChatroom(UUID id, String name)
+  public ChatRoom createChatroom(UUID id, String name)
   {
     InMemoryChatroomService chatroomService =
         new InMemoryChatroomService(new LinkedHashMap<>());
-    return new Chatroom(id, name, chatroomService, bufferSize);
+    return new ChatRoom(id, name, chatroomService, bufferSize);
   }
 
-  public Chatroom restoreChatroom(
+  public ChatRoom restoreChatroom(
       UUID id,
       String name,
       InMemoryChatroomService chatroomService)
   {
-    return new Chatroom(id, name, chatroomService, bufferSize);
+    return new ChatRoom(id, name, chatroomService, bufferSize);
   }
 }
index d2092b2..d66c544 100644 (file)
@@ -5,7 +5,7 @@ import com.fasterxml.jackson.databind.JavaType;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import de.juplo.kafka.chat.backend.api.ChatroomTo;
 import de.juplo.kafka.chat.backend.api.MessageTo;
-import de.juplo.kafka.chat.backend.domain.Chatroom;
+import de.juplo.kafka.chat.backend.domain.ChatRoom;
 import de.juplo.kafka.chat.backend.domain.Message;
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
@@ -32,7 +32,7 @@ public class LocalJsonFilesStorageStrategy implements StorageStrategy
 
 
   @Override
-  public void writeChatrooms(Flux<Chatroom> chatroomFlux)
+  public void writeChatrooms(Flux<ChatRoom> chatroomFlux)
   {
     Path path = chatroomsPath();
     log.info("Writing chatrooms to {}", path);
@@ -92,7 +92,7 @@ public class LocalJsonFilesStorageStrategy implements StorageStrategy
   }
 
   @Override
-  public Flux<Chatroom> readChatrooms()
+  public Flux<ChatRoom> readChatrooms()
   {
     JavaType type = mapper.getTypeFactory().constructType(ChatroomTo.class);
     return Flux
index 38a9e98..a337b61 100644 (file)
@@ -1,15 +1,15 @@
 package de.juplo.kafka.chat.backend.persistence;
 
 import de.juplo.kafka.chat.backend.api.ChatroomTo;
-import de.juplo.kafka.chat.backend.domain.Chatroom;
+import de.juplo.kafka.chat.backend.domain.ChatRoom;
 import de.juplo.kafka.chat.backend.domain.Message;
 import reactor.core.publisher.Flux;
 
 
 public interface StorageStrategy
 {
-  void writeChatrooms(Flux<Chatroom> chatroomFlux);
-  Flux<Chatroom> readChatrooms();
+  void writeChatrooms(Flux<ChatRoom> chatroomFlux);
+  Flux<ChatRoom> readChatrooms();
   void writeMessages(ChatroomTo chatroomTo, Flux<Message> messageFlux);
   Flux<Message> readMessages(ChatroomTo chatroomTo);
 }