I have a generic interface like this:
interface A<T> {
T getValue();
}
This interface has limited instances, hence it would be best to implement them as enum values. The problem is those instances have different type of values, so I tried the following approach but it does not compile:
public enum B implements A {
A1<String> {
@Override
public String getValue() {
return "value";
}
},
A2<Integer> {
@Override
public Integer getValue() {
return 0;
}
};
}
Any idea about this?
If JEP 301: Enhanced Enums gets accepted, then you will be able to use syntax like this (taken from proposal):
As Java developers designing certain APIs, we come across this issue frequently. I was reconfirming my own doubts when I came across this post, but I have a verbose workaround to it:
At that point, you gain the benefit of being a truly constant
enum
eration value (and all of the perks that go with that), as well being an unique implementation of theinterface
, but you have the global accessibility desired by theenum
.Clearly, this adds verbosity, which creates the potential for copy/paste mistakes. You could make the
enum
spublic
and simply add an extra layer to their access.Designs that tend to use these features tend to suffer from brittle
equals
implementations because they are usually coupled with some other unique value, such as a name, which can be unwittingly duplicated across the codebase for a similar, yet different purpose. By usingenum
s across the board, equality is a freebie that is immune to such brittle behavior.The major drawback to such as system, beyond verbosity, is the idea of converting back and forth between the globally unique keys (e.g., marshaling to and from JSON). If they're just keys, then they can be safely reinstantiated (duplicated) at the cost of wasting memory, but using what was previously a weakness--
equals
--as an advantage.There is a workaround to this that provides global implementation uniqueness by cluttering it with an anonymous type per global instance:
Note that each instance uses an anonymous implementation, but nothing else is needed to implement it, so the
{}
are empty. This is both confusing and annoying, but it works if instance references are preferable and clutter is kept to a minimum, although it may be a bit cryptic to less experienced Java developers, thereby making it harder to maintain.Finally, the only way to provide global uniqueness and reassignment is to be a little more creative with what is happening. The most common use for globally shared interfaces that I have seen are for MetaData buckets that tend to mix a lot of different values, with different types (the
T
, on a per key basis):This provides the same flexibility as the first option, and it provides a mechanism for obtaining a reference via reflection, if it becomes necessary later, therefore avoiding the need for instantiable later. It also avoids a lot of the error prone copy/paste mistakes that the first option provides because it won't compile if the first method is wrong, and the second method does not need to change. The only note is that you should ensure that the
enum
s meant to be used in that fashion arepublic
to avoid anyone getting access errors because they do not have access to the innerenum
; if you did not want to have thoseMetaDataKey
s going across a marshaled wire, then keeping them hidden from outside packages could be used to automatically discard them (during marshaling, reflectively check to see if theenum
is accessible, and if it is not, then ignore the key/value). There is nothing gained or lost by making itpublic
except providing two ways to access the instance, if the more obviousstatic
references are maintained (as theenum
instances are just that anyway).I just wish that they made it so that
enum
s could extend objects in Java. Maybe in Java 9?The final option does not really solve your need, as you were asking for values, but I suspect that this gets toward the actual goal.
You can't. Java doesn't allow generic types on enum constants. They are allowed on enum types, though:
What you could do in this case is either have an enum type for each generic type, or 'fake' having an enum by just making it a class:
Unfortunately, they both have drawbacks.