62

I'm not 100% convinced that this is a good idea, but I bumped into some code today that's currently implemented as:

class MyWidget <T extends Enum<T> > {
  MyWidget(Map<T, Integer> valueMap) {
    mValueMap = valueMap;
  }

  Map<T, Integer> mValueMap;
}

where MyWidget then offers methods that use mValueMap to convert the passed-in Enum to/from an Integer.

What I was considering doing was trying to refactor this, so that I'd declare my enumeration:

interface MyInterface {
  public Integer getValue();
}

enum MyEnum implements MyInterface {
  foo, bar;
  public Integer getValue() {
    return ordinal();
  }
}

And I'd then be able to rewrite MyWidget into something that looked vaguely like this:

public class MyWidget<T extends Enum<T> extends MyInterface> {
  ...
}

and would then be able to call the getValue() method from MyInterface on T-type objects within MyWidget. The problem, of course, is that "<T extends Enum<T> extends MyInterface>" isn't valid syntax. Is there any way to pull this off?

I don't want to just have MyWidget<T extends MyInterface>, because it's also important that T be an enumeration.

Thanks in advance!

2 Answers 2

108

Use an '&' instead:

public class MyWidget<T extends Enum<T> & MyInterface> {
    ...
}

The JLS calls this an "intersection type", but I can find no mention of it in the Java tutorials. I'll just say that it does exactly what you were wishing that "extends" would do.

Also, I should mention that you can have as many types as you want in the intersection type. So if you wanted, you could do:

public class MyWidget<T extends Enum<T> & MyInterface & Serializable & Cloneable> {
    ...
}

[Note: this code sample should not be construed as an endorsement of the Cloneable interface; it was merely handy at the time.]

6
  • The & syntax is neat, but I find that using it usually turns into a code smell, and is an indication that something is not quite right with your model.
    – skaffman
    Commented Jul 1, 2009 at 19:33
  • 2
    I tend to agree (I think I've used it maybe once or twice, if that). But in the case of enums implementing an interface, I think it is appropriate.
    – Michael Myers
    Commented Jul 1, 2009 at 19:41
  • 3
    Thanks. Your post somehow made me realize, that what I really needed was along the lines of ... doSomething(Enum<? extends SomeInterface>)
    – Jörg
    Commented Jun 8, 2011 at 8:17
  • 1
    @skaffmann if you'd find time to share your experience WRT related code smells, I'd really appreciate
    – Jörg
    Commented Jun 8, 2011 at 8:17
  • 4
    +10 Never would have figured this out on my own. The only way to handle enums with interfaces. Commented Sep 11, 2012 at 21:57
1

The JSR 203 (new new IO) stuff for JDK 7 is making a lot of use of enums that implement interfaces (for example: http://openjdk.java.net/projects/nio/javadoc/java/nio/file/FileVisitOption.html) to allow them some wiggle room in the future for future additional sets of enum options. So that is a feasible approach and obviously one that was chosen after a lot of thought in one large Sun project.

3
  • Neat. Hopefully java 7 will appear before my beard goes grey.
    – skaffman
    Commented Jul 1, 2009 at 20:16
  • From writing a small amount of code that uses this stuff, my impression is that it's a little weird. Can't say I've seen it anywhere else. Something with a 7 in the name will release in 2010 I'm pretty sure. Whether it will be "Java 7" as endorsed by the JCP, I don't know. Only Oracle can answer that now... Commented Jul 1, 2009 at 20:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.