On 05/30/2013 09:26 AM, Remi Forax wrote:
On 05/30/2013 09:06 AM, Victor Polischuk wrote:
Good day Joe,
Thank you for your prompt reply. If I understood you correctly, it is
not the solution which can help me distinguish enum elements by type
parameters. I would like to have restrictions on types which can be
passed to a enum instance methods and reduce numbers of casts or
"instanceof" checks for return values. In case of implementation of
an interface I need to write something like:
//----
public enum Color implements ColorAspect<Pixel> {...}
//----
which is redundant since I can hardcode Pixel type everywhere within
the enumeration but the initial intention is to specify type
parameters for each enum value independently.
the idea is to do something like this:
interface Color<T extends PurePixel> {
public boolean filter(T pixel) {...}
public T make() {...}
}
public enum PurePixelColor implements Color<PurePixel> {
RED("Red"),
GREEN("Green"),
BLUE("Blue");
private PurePixelColor(String color) {...}
public boolean filter(PurePixel pixel) {...}
public PurePixel make() {...}
}
public enum MixedPixelColor implements Color<MixedPixel> {
WHITE("White");
private MixedPixelColor(String color) {...}
public boolean filter(MixedPixel pixel) {...}
public MixedPixel make() {...}
}
if you compare with your current code,
Color objects are not comparable to themselve anymore.
and if the code of filter is the same for PurePixelColor and
MixedPixelColor you have to extract it somewhere in a static method.
Or code it as a "default" method in Color interface if you can use JDK8 ;-)
Regards, Peter
The feature you ask was asked several times when java5 was released
but was considered as a corner case that doesn't worth the added
complexity to the implementation.
Sincerely yours,
Victor Polischuk
cheers,
Rémi
--- Original message ---
From: "Joe Darcy" <[email protected]>
Date: 30 May 2013, 09:44:57
Hello Victor,
On 5/29/2013 11:25 PM, Victor Polischuk wrote:
Greetings,
I beg pardon for the previous HTML mail.
Some time ago I wanted to migrate our "commons-lang" enums to "java
5" enumerations, but I encountered an issue that it cannot be done
without discarding generics since java enums do not support them.
Let me show an example:
//------
public final class ColorEnum<T extends Pixel> extends
org.apache.commons.lang.enums.Enum {
public static final ColorEnum<PurePixel> RED = new
ColorEnum<PurePixel>("Red");
public static final ColorEnum<PurePixel> GREEN = new
ColorEnum<PurePixel>("Green");
public static final ColorEnum<PurePixel>�BLUE = new
ColorEnum<PurePixel>("Blue");
public static final ColorEnum<MixedPixel> WHITE = new
ColorEnum<MixedPixel>("White") {
@Override
public MixedPixel make() {...}�
};
private ColorEnum(String color) {super(color);}
public boolean filter(T pixel) {...}
public T make() {...}
}
//------
And I wonder if there is a specific reason why I cannot convert it
into something like:�
//------
public enum Color<T extends Pixel> {
RED<PurePixel>("Red"),
GREEN<PurePixel>("Green"),
BLUE<PurePixel>("Blue"),
WHITE<MixedPixel>("White") {
@Override
public MixedPixel make() {...}�
};
private Color(String color) {...}
public boolean filter(T pixel) {...}
public T make() {...}
}
//------
Thank you in advance.
Sincerely yours,
Victor Polischuk
You can approximate this effect by having your enum implement an
interface or even a generic interface. For some examples in the JDK
see,
javax.tools.StandardLocation and java.nio.file.LinkOption.
HTH,
-Joe