crane4j
crane4j copied to clipboard
针对有界容器提供自动创建镜像容器的功能
基于 #119 ,我们应当基于 Container
接口提供一个新的子接口 LimitedContainer
,它用于表示一个仅包含了固定的、有限个数数据源对象的容器。
针对该类型的容器,我们可以提供一个自动生成镜像的功能,即自动为其创建一个 key-value 翻转的版本,就像 Guava 中的 BiMap 那样。比如:
// 原始容器
Map<String, String> origin = new HashMap<>();
origin.put("one", "1");
origin.put("two", "2");
origin.put("three", "3");
LimitedContainer<String> contanier = Containers.forMap("test", origin);
containerManager.registerContanier(contanier);
// 当注册到容器之后,自动为其生成一个镜像容器(若有必要),该镜像容器的命名空间与原始的容器有所区别
imitedContainer<String> mirrorContainer = containerManager.getContainer("test_reverse");
mirrorContainer.getAll(); // [{"1"="one"}, {"2"="two"}, {"3"="three"}]
该功能需要考虑两方面的因素:
- 在什么时候创建镜像容器?可以考虑基于
ContainerLifeProcessor
完成,在容器创建后自动为其创建,或者提供一个独立的ContainerProvider
负责为所有的容器提供镜像版本; - 如何知道是否要创建镜像容器?考虑在向
ContainerManager
注册容器的时候就加以区分,或者想办法给个注解作为元数据用于区分;