假设我有一个如下所示的枚举:
public enum Env {
holding, integration, staging, production;
public enum Holding {
THE_SAME_ENUM("Some String1, "Another String1");
String name;
String uuid;
Holding(String name, String uuid) { this.name = name; this.uuid = uuid; }
public String getName() {return name;}
public String getData() {return uuid;}
}
public enum Integration {
THE_SAME_ENUM("Some String2, "Another String2");
String name;
String uuid;
Integration(String name, String uuid) { this.name = name; this.uuid = uuid; }
public String getName() {return name;}
public String getData() {return uuid;}
}
public enum Staging {
THE_SAME_ENUM("Some String3, "Another String3");
String name;
String uuid;
Staging(String name, String uuid) { this.name = name; this.uuid = uuid; }
public String getName() {return name;}
public String getData() {return uuid;}
}
public enum Production {
THE_SAME_ENUM("Some String4, "Another String4");
String name;
String uuid;
Production(String name, String uuid) { this.name = name; this.uuid = uuid; }
public String getName() {return name;}
public String getData() {return uuid;}
}
}
然后在一个方法中,我想做这样的事情:
Env env;
switch(System.getEnv()) {
case holding:
env = Env.holding;
case integration:
env = Env.integration;
case staging:
env = Env.staging;
case production:
env = Env.production;
//Expecting "Some String(1)or(2)or(3)or(4)"
System.out.println(env.THE_SAME_ENUM.getName());
}
我真的只想根据环境变量更改类型,然后根据环境使用我硬编码的设置。我还没有找到实现这个目标的方法。任何帮助将不胜感激!
答案 0 :(得分:1)
您可能想要一些与此相关的内容
public enum Env {
holding("Some String1", "Another String1"),
integration("Some String2", "Another String2"),
staging("Some String3", "Another String3"),
production("Some String4", "Another String4");
String name;
String uuid;
Env(String name, String uuid) { this.name = name; this.uuid = uuid; }
public String getName() {return name;}
public String getData() {return uuid;}
}
}
答案 1 :(得分:1)
这就是我最终解决问题的方法。枚举的每个参数代表不同的环境,我只使用getter方法为每个环境返回正确的id。
public enum EnvironmentIds {
USER_MANAGER_CHANGED("e29e902c","28a6e01c", "281916f6"),
USER_ADDED_TO_GROUP("5059020b", "f039fc37", "0e480029"),
USER_CREATED("d9bc6a0c", "f26f0a23", "9261f53a"),
USER_REMOVED_FROM_GROUP("fc30b97c", "15528295", "c08af3b2");
String holdingId;
String productionId;
String stagingId;
EnvironmentIds(String holdingId, String stagingId, String productionId) {
this.holdingId = holdingId;
this.stagingId = stagingId;
this.productionId = productionId;
}
public String getId(String environment) {
switch(environment) {
case "holding":
return getHoldingId();
case "production":
return getProductionId();
case "staging":
return getStagingId();
default:
return null;
}
}
public String getHoldingId() {return holdingId;}
public String getProductionId() {return productionId;}
public String getStagingId() {return stagingId;}
}