From c43495a5562f29ad2dfbef81ca5733d9493db955 Mon Sep 17 00:00:00 2001
From: huangchengxing <841396397@qq.com>
Date: Wed, 2 Aug 2023 19:05:39 +0800
Subject: [PATCH] feat(ContainerProvider): add PartitionContainerProvider
(GitHub #122)
---
.../container/PartitionContainerProvider.java | 65 +++++++++++++++++++
.../AbstractAssembleAnnotationHandler.java | 22 ++++++-
2 files changed, 84 insertions(+), 3 deletions(-)
create mode 100644 crane4j-core/src/main/java/cn/crane4j/core/container/PartitionContainerProvider.java
diff --git a/crane4j-core/src/main/java/cn/crane4j/core/container/PartitionContainerProvider.java b/crane4j-core/src/main/java/cn/crane4j/core/container/PartitionContainerProvider.java
new file mode 100644
index 00000000..e0e58d59
--- /dev/null
+++ b/crane4j-core/src/main/java/cn/crane4j/core/container/PartitionContainerProvider.java
@@ -0,0 +1,65 @@
+package cn.crane4j.core.container;
+
+import lombok.Setter;
+import org.checkerframework.checker.nullness.qual.NonNull;
+import org.checkerframework.checker.nullness.qual.Nullable;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
+import java.util.function.Function;
+
+/**
+ *
A {@link ContainerProvider} implementation for conveniently registering container.
+ * When get container by given namespace, it will return the container registered by {@link #registerContainer(Container)},
+ * if not exist, it will return the container created by {@link #defaultContainerFactory}.
+ *
+ * @author huangchengxing
+ */
+public class PartitionContainerProvider implements ContainerProvider {
+
+ /**
+ * Container map.
+ */
+ private final Map> containerMap = new HashMap<>();
+
+ /**
+ * Default container factory for non-existent container.
+ */
+ @NonNull
+ @Setter
+ private Function> defaultContainerFactory = namespace -> Containers.empty();
+
+ /**
+ * Get container comparator by given namespace
+ *
+ * @param namespace namespace of container
+ * @return container comparator
+ */
+ @SuppressWarnings("unchecked")
+ @Override
+ public @Nullable Container getContainer(String namespace) {
+ return (Container)containerMap.getOrDefault(namespace, defaultContainerFactory.apply(namespace));
+ }
+
+ /**
+ * Register container.
+ *
+ * @param container container
+ */
+ public void registerContainer(@NonNull Container