This discussion is archived
8 Replies Latest reply: Apr 4, 2013 12:13 PM by TPD-Opitz-Consulting-com RSS

Declaring Argument as Class of Enum That Implements Interface

aksarben Journeyer
Currently Being Moderated
I'm trying to declare a method parameter that is the class of an enum which implements a specified interface. I can't figure out the required syntax, or if this is even possible.
_________________________________________________

Here is the interface:
public interface Describer { String describe(); }
_________________________________________________

Here' s an enum that implements the interface:
public enum DeviceProperty implements Describer {

TYPE("Device Type"),
USE("Device Use");

private String description;

private DeviceProperty(final String description) { this.description = description; }

@Override public String describe() { return description; }

}
_________________________________________________

Here's my unsuccessful attempt to declare the method parameter:
public <E extends Enum<E extends Describer>> void addChanges(final Class<E> properties) {

     final EnumSet<E> set = EnumSet.allOf(properties);
     for (final Enum<E> property : set) {
          // Do something with the property
     }
}
_________________________________________________

The problem, of course, is that addChanges() doesn't compile. If I remove "extends Describer" it compiles, but then I can't detect compile time errors (i.e., an enum which doesn't implement Describer).

Is what I'm attempting even possible in Java? If so, I'd be very grateful if someone could explain the correct syntax.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points