我有一个下面的枚举类,其中很少有字段stagePoolSize、prodPoolSize和enabled。
public enum Type {
process_caty(5, 8, false), process_misc1(5, 8, false), link_entry(5, 12, true);
private final int stagePoolSize;
private final int prodPoolSize;
private final boolean enabled;
private Type(int stagePoolSize, int prodPoolSize, boolean enabled) {
this.stagePoolSize = stagePoolSize;
this.prodPoolSize = prodPoolSize;
this.enabled = enabled;
}
public int getStagePoolSize() {
return stagePoolSize;
}
public int getProdPoolSize() {
return prodPoolSize;
}
public boolean isEnabled() {
return enabled;
}
@Override
public String toString() {
return name() + "=" + stagePoolSize + "=" + prodPoolSize + "=" + enabled;
}
}这就是我如何使用上面的枚举来初始化我的Handler类。
private final List<Handler> handlers = new ArrayList<>();
@PostConstruct
public void postInit() {
String datacenter = Utils.getDatacenter();
for (Type consType : Type.values()) {
if (!consType.isEnabled()) {
continue;
}
int poolSize = Utils.isProd() ? consType.getProdPoolSize() : consType.getStagePoolSize();
handlers.add(new Handler(consType, poolSize));
}
}如果启用了任何枚举,那么通过检查这个调用Utils.isProd(),取决于我们所处的环境(无论是Prod环境还是阶段环境),我们得到的是poolSize,然后使用该poolSize初始化Handler类。
问题陈述:
现在,我需要在同一个Type类中再添加几个枚举,但是我需要为它们做一些不同的事情。下面是我需要补充的内容:
abc_raw_pho
abc_raw_slq
abc_raw_lvk
abc_raw_pin_pho
abc_raw_pin_slq
abc_raw_pin_lvk以下是我们需要为上述新项目所做的工作:
Prod时,才应该使用它。它们将有一些prodPool大小值。pho数据,那么我们应该使用以pho结尾的枚举。如果我们在slq数据中心,那么我们应该使用以slq结尾的枚举。同样也适用于lvk。我可以通过调用以下方法来确定我们所在的数据中心:
String datacenter = Utils.getDatacenter();现在,我应该如何设计我的Type枚举类,这样我的原始枚举就可以正常工作了,它已经存在了,并且我的新枚举与数据中心一起使用了上述条件。因此,以下是整体的需求:
process_caty应该同时用于Stage和Prod,并且应该相应地使用poolSize,这取决于它所处的环境。process_misc1应该同时用于Stage和Prod,并且应该相应地使用poolSize,这取决于它所处的环境。link_entry应该同时用于Stage和Prod,并且应该相应地使用poolSize,这取决于它所处的环境。abc_raw_pho只适用于Prod,但取决于我们所在的数据中心,它应该只对Prod使用poolSize。abc_raw_slq只适用于Prod,但取决于我们所在的数据中心,它应该只对Prod使用poolSize。abc_raw_lvk只适用于Prod,但取决于我们所在的数据中心,它应该只对Prod使用poolSize。abc_raw_pin_pho只适用于Prod,但取决于我们所处的数据中心,它应该只对Prod使用poolSize。abc_raw_pin_slq只适用于Prod,但取决于我们所处的数据中心,它应该只对Prod使用poolSize。abc_raw_pin_lvk只适用于Prod,但取决于我们所处的数据中心,它应该只对Prod使用poolSize。发布于 2017-10-26 22:16:24
我将重构您的枚举并将其分解为更小的类,介绍Environment、DataCenter和PoolSizes的模型。
public enum Environment {PROD, STAGE}
public enum DataCenter {PHO, SLQ, LVK, NA /*Not applicable*/}
public class PoolSizes {
public static final int SIZE_UNDEFINED = -1;
private Map<Environment, Integer> environmentValues;
public PoolSizes() {
environmentValues = new HashMap<>();
}
public PoolSizes withStagePoolSize(int size) {
environmentValues.put(Environment.STAGE, size);
return this;
}
public PoolSizes withProductionPoolSize(int size) {
environmentValues.put(Environment.PROD, size);
return this;
}
public int getPoolSize(Environment environment) {
Integer size = environmentValues.get(environment);
return size != null ? size : SIZE_UNDEFINED;
}
}然后,Type枚举将使用这些来简化池大小查找的逻辑,并确定是否启用了该类型:
public enum Type {
PROCESS_CATY(new PoolSizes().withStagePoolSize(5).withProductionPoolSize(8),
DataCenter.NA, false),
PROCESS_MISC1(new PoolSizes().withStagePoolSize(5).withProductionPoolSize(8),
DataCenter.NA, false),
LINK_ENTRY(new PoolSizes().withStagePoolSize(5).withProductionPoolSize(12),
DataCenter.NA, true),
ABC_RAW_PHO(new PoolSizes().withProductionPoolSize(123), DataCenter.PHO, true),
ABC_RAW_SLQ(new PoolSizes().withProductionPoolSize(123), DataCenter.SLQ, true),
ABC_RAW_LVK(new PoolSizes().withProductionPoolSize(123), DataCenter.LVK, true),
ABC_RAW_PIN_PHO(new PoolSizes().withProductionPoolSize(123), DataCenter.PHO, true),
ABC_RAW_PIN_SLQ(new PoolSizes().withProductionPoolSize(123), DataCenter.SLQ, true),
ABC_RAW_PIN_LVK(new PoolSizes().withProductionPoolSize(123), DataCenter.LVK, true);
private final PoolSizes poolSizes;
private final DataCenter dataCenter;
private final boolean enabled;
private Type(PoolSizes poolSizes, DataCenter dataCenter, boolean enabled) {
this.poolSizes = poolSizes;
this.dataCenter = dataCenter;
this.enabled = enabled;
}
public int getPoolSize(Environment environment) {
return poolSizes.getPoolSize(environment);
}
public DataCenter getDataCenter() {
return this.dataCenter;
}
public boolean isEnabled(Environment environment, DataCenter dataCenter) {
return enabled && poolSizes.getPoolSize(environment) != PoolSizes.SIZE_UNDEFINED
&& (getDataCenter() == DataCenter.NA || getDataCenter() == dataCenter);
}
}最后,postInit()方法将变成如下所示:
public void postInit() {
DataCenter dataCenter = Utils.getDataCenter();
Environment environment = Utils.getEnvironment();
for (Type consType : Type.values()) {
if (!consType.isEnabled(dataCenter, environment)) {
continue;
}
handlers.add(new Handler(consType, consType.getPoolSize(environment)));
}
}https://stackoverflow.com/questions/46963999
复制相似问题