我在博客上看到它建议以下是使用getCode(int)Java枚举进行"反向查找"的合理方法:
public enum Status {
WAITING(0),
READY(1),
SKIPPED(-1),
COMPLETED(5);
private static final Map<Integer,Status> lookup
= new HashMap<Integer,Status>();
static {
for(Status s : EnumSet.allOf(Status.class))
lookup.put(s.getCode(), s);
}
private int code;
private Status(int code) {
this.code = code;
}
public int getCode() { return code; }
public static Status get(int code) {
return lookup.get(code);
}
}
Run Code Online (Sandbox Code Playgroud)
对我来说,静态地图和静态初始化程序看起来都是个坏主意,我的第一个想法就是将查询编码为:
public enum Status {
WAITING(0),
READY(1),
SKIPPED(-1),
COMPLETED(5);
private int code;
private Status(int code) {
this.code = code;
} …Run Code Online (Sandbox Code Playgroud) 我正在尝试执行以下操作:
Class<?> cls = unknownClass;
if(cls.isEnum()){
@SuppressWarnings("unchecked")
Class<? extends Enum<?>> enumClass = (Class<? extends Enum<?>>) cls;
Object val = Enum.valueOf(enumClass, "NAME1");
}
Run Code Online (Sandbox Code Playgroud)
但是我收到以下错误:
Bound mismatch: The generic method valueOf(Class<T>, String) of type Enum<E> is
not applicable for the arguments (Class<capture#5-of ? extends Enum<?>>, String).
The inferred type capture#5-of ? extends Enum<?> is not a valid substitute for
the bounded parameter <T extends Enum<T>>
Run Code Online (Sandbox Code Playgroud)
有人能告诉我我做错了什么吗?
我正在尝试使用泛型来支持委托对象(装饰器,包装器)的可配置结构.我想构建一个实现目标接口和通用委托接口的委托链.
我有这个大纲:
class Test {
static interface Delegator<T> {}
static class DelegatorChain<T extends Delegator<T>> {}
static interface Foo {}
static class FooDelegator implements Delegator<Foo>, Foo {}
public static void main(String[] args) {
DelegatorChain<FooDelegator> chain = new DelegatorChain<FooDelegator>();
}
}
Run Code Online (Sandbox Code Playgroud)
但是,在尝试实例化chain变量时,编译器抱怨:
绑定不匹配:类型Test.FooDelegator不是该类型的有界参数
<T extends Test.Delegator<T>>的有效替代Test.DelegatorChain<T>
我承认泛型对我来说就像魔术一样,但我可以以某种方式承认FooDelegator不是扩展 Delegator <Foo>的Foo,它只是实现了两个接口.
鉴于我很清楚我想要完成什么,有什么我可以用gertics来修复它,或者我只是更好地忘记了它?
我想将任何枚举值传递给实用程序类中的方法,并获取相同枚举类型的另一个枚举值.像这样的东西:
public class XMLUtils {
public static Enum<?> getEnumAttribute(Element element, String name,
Enum<?> defaultValue) {
if (element.hasAttribute(name)) {
String valueName = element.getAttribute(name);
// search for value
for (Enum<?> value: defaultValue.getClass().getEnumConstants())
if (value.toString().equalsIgnoreCase(valueName))
return value;
}
// not found, return default value
return defaultValue;
}
}
Run Code Online (Sandbox Code Playgroud)
使用方法getEnumAttribute():
// simple enum
public enum EUploadMethod {
INSERT, UPDATE, DELETE
}
// read enum value from XML config file
EUploadMethod method = XMLUtils.getEnumAttribute(element, "method",
EUploadMethod.INSERT);
Run Code Online (Sandbox Code Playgroud)
这段代码功能齐全,Eclipse编译并运行它没有警告或错误,它就像一个魅力.
但是当我通过Maven2从命令行清理和编译项目时,它会因为 …
我会尽量保持这个简短.我正在尝试做这样的事情:
public enum Fruit {
APPLE("Apple", appleHelper::doAppleThing),
ORANGE("Orange", orangeHelper::doOrangeThing);
private String name;
private Function<String, List<T>> fruitFunction;
Fruit(String name, Function<String, List<T>> fruitFunction) {
this.name = name;
this.fruitFunction = fruitFunction;
}
public String getName() {
return name;
}
public <T> List<T> applyFruitFunction(String someString) {
return fruitFunction.apply(someString);
}
}
Run Code Online (Sandbox Code Playgroud)
这样以后,我可以有一个像这样的方法
private <T> List<T> doFruitThing(String someString, Fruit fruit) {
List<T> transformedFruits = fruit.applyFruitFunction(someString);
if (transformedFruits.isEmpty()) {
throw new FruitException("There was no fruit of type " + fruit.getName());
}
return transformedFruits;
}
Run Code Online (Sandbox Code Playgroud)
我遇到了两个问题.
doAppleThing而 …