refactor: Removed `ChatHomeFactory`
authorKai Moritz <kai@juplo.de>
Sat, 14 Jan 2023 15:01:55 +0000 (16:01 +0100)
committerKai Moritz <kai@juplo.de>
Wed, 25 Jan 2023 21:00:58 +0000 (22:00 +0100)
- `ChatHomeFactory` was only used during initialization.
- By moving the instanciation of `chatHomes` into the implementation-
  specific configuration, the necessity of the factory vanished.

src/main/java/de/juplo/kafka/chat/backend/ChatBackendConfiguration.java
src/main/java/de/juplo/kafka/chat/backend/domain/ChatHomeFactory.java [deleted file]
src/main/java/de/juplo/kafka/chat/backend/persistence/inmemory/InMemoryChatHomeFactory.java [deleted file]
src/main/java/de/juplo/kafka/chat/backend/persistence/inmemory/InMemoryServicesConfiguration.java
src/test/java/de/juplo/kafka/chat/backend/api/ChatBackendControllerTest.java

index 6e74bb0..f49e385 100644 (file)
@@ -1,9 +1,5 @@
 package de.juplo.kafka.chat.backend;
 
-import de.juplo.kafka.chat.backend.domain.ChatHome;
-import de.juplo.kafka.chat.backend.domain.ChatHomeFactory;
-import de.juplo.kafka.chat.backend.domain.ChatHomeService;
-import de.juplo.kafka.chat.backend.persistence.StorageStrategy;
 import org.springframework.boot.context.properties.EnableConfigurationProperties;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
@@ -15,24 +11,6 @@ import java.time.Clock;
 @EnableConfigurationProperties(ChatBackendProperties.class)
 public class ChatBackendConfiguration
 {
-  @Bean
-  ChatHome[] chatHomes(
-      ChatHomeFactory factory,
-      ChatBackendProperties properties,
-      StorageStrategy storageStrategy)
-  {
-    ChatHome[] chatHomes = new ChatHome[properties.getInmemory().getNumShards()];
-    storageStrategy
-        .read()
-        .subscribe(chatRoom ->
-        {
-          int shard = chatRoom.getShard();
-          if (chatHomes[shard] == null)
-            chatHomes[shard] = factory.createChatHome(shard);
-        });
-    return chatHomes;
-  }
-
   @Bean
   Clock clock()
   {
diff --git a/src/main/java/de/juplo/kafka/chat/backend/domain/ChatHomeFactory.java b/src/main/java/de/juplo/kafka/chat/backend/domain/ChatHomeFactory.java
deleted file mode 100644 (file)
index fb20cc7..0000000
+++ /dev/null
@@ -1,6 +0,0 @@
-package de.juplo.kafka.chat.backend.domain;
-
-public interface ChatHomeFactory
-{
-  ChatHome createChatHome(int shard);
-}
diff --git a/src/main/java/de/juplo/kafka/chat/backend/persistence/inmemory/InMemoryChatHomeFactory.java b/src/main/java/de/juplo/kafka/chat/backend/persistence/inmemory/InMemoryChatHomeFactory.java
deleted file mode 100644 (file)
index d2c6b63..0000000
+++ /dev/null
@@ -1,19 +0,0 @@
-package de.juplo.kafka.chat.backend.persistence.inmemory;
-
-import de.juplo.kafka.chat.backend.domain.ChatHome;
-import de.juplo.kafka.chat.backend.domain.ChatHomeFactory;
-import lombok.RequiredArgsConstructor;
-
-
-@RequiredArgsConstructor
-public class InMemoryChatHomeFactory implements ChatHomeFactory
-{
-  private final InMemoryChatHomeService service;
-
-
-  @Override
-  public ChatHome createChatHome(int shard)
-  {
-    return new ChatHome(service, shard);
-  }
-}
index 94dd5d8..e40d950 100644 (file)
@@ -3,6 +3,7 @@ package de.juplo.kafka.chat.backend.persistence.inmemory;
 import de.juplo.kafka.chat.backend.ChatBackendProperties;
 import de.juplo.kafka.chat.backend.api.KafkaLikeShardingStrategy;
 import de.juplo.kafka.chat.backend.api.ShardingStrategy;
+import de.juplo.kafka.chat.backend.domain.ChatHome;
 import de.juplo.kafka.chat.backend.persistence.StorageStrategy;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
 import org.springframework.context.annotation.Bean;
@@ -19,6 +20,24 @@ import java.time.Clock;
 @Configuration
 public class InMemoryServicesConfiguration
 {
+  @Bean
+  ChatHome[] chatHomes(
+      ChatBackendProperties properties,
+      InMemoryChatHomeService chatHomeService,
+      StorageStrategy storageStrategy)
+  {
+    ChatHome[] chatHomes = new ChatHome[properties.getInmemory().getNumShards()];
+    storageStrategy
+        .read()
+        .subscribe(chatRoom ->
+        {
+          int shard = chatRoom.getShard();
+          if (chatHomes[shard] == null)
+            chatHomes[shard] = new ChatHome(chatHomeService, shard);
+        });
+    return chatHomes;
+  }
+
   @Bean
   InMemoryChatHomeService chatHomeService(
       ChatBackendProperties properties,
@@ -30,12 +49,6 @@ public class InMemoryServicesConfiguration
         storageStrategy.read());
   }
 
-  @Bean
-  InMemoryChatHomeFactory chatHomeFactory(InMemoryChatHomeService service)
-  {
-    return new InMemoryChatHomeFactory(service);
-  }
-
   @Bean
   InMemoryChatRoomFactory chatRoomFactory(
       ShardingStrategy strategy,
index 12d34be..5ebd08d 100644 (file)
@@ -262,12 +262,12 @@ public class ChatBackendControllerTest
     @Bean
     ChatHome[] chatHomes(
         ChatBackendProperties properties,
-        ChatHomeFactory factory)
+        InMemoryChatHomeService service)
     {
       ChatHome[] chatHomes = new ChatHome[properties.getInmemory().getNumShards()];
       Arrays
           .stream(properties.getInmemory().getOwnedShards())
-          .forEach(i -> chatHomes[i] = factory.createChatHome(i));
+          .forEach(i -> chatHomes[i] = new ChatHome(service, i));
       return chatHomes;
     }
   }