Extending Enum

Discussion in 'Java' started by lroland@gmail.com, May 15, 2007.

  1. Guest

    Hi all

    Due to a somewhat strange external requirement I need to ensure that
    most of our enums can be treated as a single type that contains a
    getDescription() method. Given the enum keyword is just syntactic
    sugar for extending the Enum abstract class my initial idea was to do
    something like this:
    --
    public interface EnumDescription<E extends Enum<E>> {
    public String getDescription();
    }
    --

    With the implementing enum ending up as
    --
    public enum Flag implements EnumDescription<Flag> {
    ALL("All"), UNFLAGGED("Unflagged"), RED("Flagged with red flag"),
    GREEN(
    "Flagged with green flag"), BLUE("Flagged with blue flag");
    private String description;
    Flag(String description) {
    this.description = description;
    }
    public String getDescription() {
    return description;
    }
    }
    --

    Which of cause looses all the enum type information - i.e. the
    following will not work because "ordinal" does not exists in the
    interface.

    --
    public static void main(String[] args) {
    // full enums
    Flag[] flags = Flag.values();

    // print description and ordinal
    String s = new String();
    for (EnumDescription<?> d : flags) {
    s += " '" + d.getDescription() + " (id: " + d.ordinal() + ")' ";
    }
    System.out.println(s);
    }
    --

    Given i also need the other enum methods (valueOf...) it does not
    seams like a sensible solution to just add then to the interface
    (valueOf is also static which creates its own set of problems). So
    basically I need a way to keep the type information from Enum in my
    new type - is this even possible ?.
     
    , May 15, 2007
    #1
    1. Advertising

  2. Tom Hawtin Guest

    wrote:
    >
    > Due to a somewhat strange external requirement I need to ensure that
    > most of our enums can be treated as a single type that contains a
    > getDescription() method. Given the enum keyword is just syntactic


    Yes, very strange.

    > public interface EnumDescription<E extends Enum<E>> {
    > public String getDescription();
    > }


    > Given i also need the other enum methods (valueOf...) it does not
    > seams like a sensible solution to just add then to the interface
    > (valueOf is also static which creates its own set of problems).


    Very strange, because valueOf doesn't make any sense as a non-static on
    an enum. You might want to have a separate class to represent each enum
    (with all of its elements) as a single instance. Instances of that class
    could then act as a repository.

    > So
    > basically I need a way to keep the type information from Enum in my
    > new type - is this even possible ?.


    I think you need to work out exactly what you are trying to achieve, and
    then model accordingly.

    Tom Hawtin
     
    Tom Hawtin, May 15, 2007
    #2
    1. Advertising

  3. Daniel Pitts Guest

    On May 15, 10:00 am, "" <> wrote:
    > Hi all
    >
    > Due to a somewhat strange external requirement I need to ensure that
    > most of our enums can be treated as a single type that contains a
    > getDescription() method. Given the enum keyword is just syntactic
    > sugar for extending the Enum abstract class my initial idea was to do
    > something like this:
    > --
    > public interface EnumDescription<E extends Enum<E>> {
    > public String getDescription();}
    >
    > --
    >
    > With the implementing enum ending up as
    > --
    > public enum Flag implements EnumDescription<Flag> {
    > ALL("All"), UNFLAGGED("Unflagged"), RED("Flagged with red flag"),
    > GREEN(
    > "Flagged with green flag"), BLUE("Flagged with blue flag");
    > private String description;
    > Flag(String description) {
    > this.description = description;
    > }
    > public String getDescription() {
    > return description;
    > }}
    >
    > --
    >
    > Which of cause looses all the enum type information - i.e. the
    > following will not work because "ordinal" does not exists in the
    > interface.
    >
    > --
    > public static void main(String[] args) {
    > // full enums
    > Flag[] flags = Flag.values();
    >
    > // print description and ordinal
    > String s = new String();
    > for (EnumDescription<?> d : flags) {
    > s += " '" + d.getDescription() + " (id: " + d.ordinal() + ")' ";
    > }
    > System.out.println(s);}
    >
    > --
    >
    > Given i also need the other enum methods (valueOf...) it does not
    > seams like a sensible solution to just add then to the interface
    > (valueOf is also static which creates its own set of problems). So
    > basically I need a way to keep the type information from Enum in my
    > new type - is this even possible ?.


    Check all this out:

    public class Test {
    public static interface HasDescription<Type extends Enum<Type> &
    HasDescription<Type>> {
    String getDescription();
    int ordinal();
    Class<Type> getDeclaringClass();

    }

    public enum MyEnum implements HasDescription<MyEnum> {
    MINE {
    public String getDescription() {
    return "What's mine is mine.";
    }
    },
    YOURS {
    public String getDescription() {
    return "What's your is mine.";
    }
    }
    }

    public static void main(String[] args) {
    HasDescription<? extends HasDescription> thing = MyEnum.YOURS;
    System.out.println(thing.getDescription() + thing.ordinal());
    HasDescription[] enumConstants =
    thing.getDeclaringClass().getEnumConstants();
    for (HasDescription e: enumConstants) {
    System.out.println(e + ": " + e.getDescription() +
    e.ordinal());
    }
    }
    }
     
    Daniel Pitts, May 16, 2007
    #3
  4. Guest

    On May 16, 1:45 am, Daniel Pitts <> wrote:
    > On May 15, 10:00 am, "" <> wrote:
    >
    >
    >
    > > Hi all

    >
    > > Due to a somewhat strange external requirement I need to ensure that
    > > most of our enums can be treated as a single type that contains a
    > > getDescription() method. Given the enum keyword is just syntactic
    > > sugar for extending the Enum abstract class my initial idea was to do
    > > something like this:
    > > --
    > > public interface EnumDescription<E extends Enum<E>> {
    > > public String getDescription();}

    >
    > > --

    >
    > > With the implementing enum ending up as
    > > --
    > > public enum Flag implements EnumDescription<Flag> {
    > > ALL("All"), UNFLAGGED("Unflagged"), RED("Flagged with red flag"),
    > > GREEN(
    > > "Flagged with green flag"), BLUE("Flagged with blue flag");
    > > private String description;
    > > Flag(String description) {
    > > this.description = description;
    > > }
    > > public String getDescription() {
    > > return description;
    > > }}

    >
    > > --

    >
    > > Which of cause looses all the enum type information - i.e. the
    > > following will not work because "ordinal" does not exists in the
    > > interface.

    >
    > > --
    > > public static void main(String[] args) {
    > > // full enums
    > > Flag[] flags = Flag.values();

    >
    > > // print description and ordinal
    > > String s = new String();
    > > for (EnumDescription<?> d : flags) {
    > > s += " '" + d.getDescription() + " (id: " + d.ordinal() + ")' ";
    > > }
    > > System.out.println(s);}

    >
    > > --

    >
    > > Given i also need the other enum methods (valueOf...) it does not
    > > seams like a sensible solution to just add then to the interface
    > > (valueOf is also static which creates its own set of problems). So
    > > basically I need a way to keep the type information from Enum in my
    > > new type - is this even possible ?.

    >
    > Check all this out:
    >
    > public class Test {
    > public static interface HasDescription<Type extends Enum<Type> &
    > HasDescription<Type>> {
    > String getDescription();
    > int ordinal();
    > Class<Type> getDeclaringClass();
    >
    > }
    >
    > public enum MyEnum implements HasDescription<MyEnum> {
    > MINE {
    > public String getDescription() {
    > return "What's mine is mine.";
    > }
    > },
    > YOURS {
    > public String getDescription() {
    > return "What's your is mine.";
    > }
    > }
    > }
    >
    > public static void main(String[] args) {
    > HasDescription<? extends HasDescription> thing = MyEnum.YOURS;
    > System.out.println(thing.getDescription() + thing.ordinal());
    > HasDescription[] enumConstants =
    > thing.getDeclaringClass().getEnumConstants();
    > for (HasDescription e: enumConstants) {
    > System.out.println(e + ": " + e.getDescription() +
    > e.ordinal());
    > }
    > }
    >
    > }


    Thanks - this does exactly what I need.
     
    , May 16, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roedy Green

    extending enum

    Roedy Green, Jul 31, 2005, in forum: Java
    Replies:
    18
    Views:
    36,896
    chrismay
    Jun 25, 2011
  2. Simon Elliott

    extending enum's: what operators?

    Simon Elliott, Jan 9, 2006, in forum: C++
    Replies:
    7
    Views:
    462
    Pete Becker
    Jan 9, 2006
  3. Roedy Green

    Extending an enum

    Roedy Green, Jul 16, 2007, in forum: Java
    Replies:
    4
    Views:
    702
    Roedy Green
    Jul 16, 2007
  4. Christopher

    deriving a class - extending enum

    Christopher, Dec 5, 2007, in forum: C++
    Replies:
    15
    Views:
    1,240
    Roland Pibinger
    Dec 6, 2007
  5. Laura Schmidt
    Replies:
    8
    Views:
    100
    Roedy Green
    Jun 16, 2014
Loading...

Share This Page