Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2007

Tip: Looking for answers? Try searching our database.

Extending Enum

Thread view: 
lroland@gmail.com - 15 May 2007 18:00 GMT
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 ?.
Tom Hawtin - 15 May 2007 18:21 GMT
> 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
Daniel Pitts - 16 May 2007 00:45 GMT
On May 15, 10:00 am, "lrol...@gmail.com" <lrol...@gmail.com> wrote:
> Hi all
>
[quoted text clipped - 48 lines]
> 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());
       }
   }
}
lroland@gmail.com - 16 May 2007 10:00 GMT
> On May 15, 10:00 am, "lrol...@gmail.com" <lrol...@gmail.com> wrote:
>
[quoted text clipped - 87 lines]
>
> }

Thanks - this does exactly what I need.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.