This discussion is archived
14 Replies Latest reply: Feb 7, 2008 10:06 AM by 807603 RSS

multiple enums in 1 enum class

807603 Newbie
Currently Being Moderated
hello,

i'm trying to make a chatserver that receives multiple sorts of packets
and for determinating what sort of package it receives i'm using an enum class

now i'm trying to add icons to some status labels on the overview window, but when the server receives 'FILE' packages, it means someone is transferring a file to someone else

in order to get a better overview of the received 'FILE' packages i would like to use this as my file class, but it doesn't let it compile

can anyone help me how to get this piece of code to work?
public enum NFile{
     public enum MS{ WORD, EXCEL, POWERPOINT, OUTLOOK; },
     public enum Web{ HTML, PHP, XML, ASP, CHM, JS; },
     public enum Image{ BMP, JPG, GIF, PNG, TIFF; };
}
thanks in advance
  • 1. Re: multiple enums in 1 enum class
    807603 Newbie
    Currently Being Moderated
    Work, you darn code tags!
    public class NFile{
         public enum MS{ WORD, EXCEL, POWERPOINT, OUTLOOK; }
         public enum Web{ HTML, PHP, XML, ASP, CHM, JS; }
         public enum Image{ BMP, JPG, GIF, PNG, TIFF; }
    }
    NFile is not an enum itself -- what are it's enumerated elements? Now, as written, NFile is just being used as a namespace.
  • 2. Re: multiple enums in 1 enum class
    807603 Newbie
    Currently Being Moderated
    Or did you want WORD, ... HTML, ... BMP, ... to all be enumerated values in NFile, with a flavor (wrong word, I know) like MS, or WEB or IMAGE associated with each value? Like this?
    public enum NFile {
        WORD(Flavor.MS),
        EXCEL(Flavor.MS),
        POWERPOINT(Flavor.MS),
        OUTLOOK(Flavor.MS),
        HTML(Flavor.WEB),
        JPG(Flavor.IMAGE);
    
        private Flavor flavor;
    
        NFile(Flavor flavor) {
            this.flavor = flavor;
        }
    
        public Flavor getFlavor() {
            return flavor;
        }
    
        public enum Flavor {MS, WEB, IMAGE}
    }
  • 3. Re: multiple enums in 1 enum class
    807603 Newbie
    Currently Being Moderated
    currently i'm giving every packet i send to the server a PacketType

    PacketType is the enumerator that tells the server what to do with the packet
    public enum PacketType {
        STATUS, NICKNAME, DISPLAYPIC, CONTACTWINDOW, BUZZER, WEBCAM, MESSAGE, FILE;
    then when its a FILE packet i want to give it another value so that the server could make a statistical overview of what sort of file packages there are being send
    public enum ObjectType {
         public enum MS{ WORD, EXCEL, POWERPOINT, OUTLOOK; },
         public enum Web{ HTML, PHP, XML, ASP, CHM, JS; }
         public enum Image{ BMP, JPG, GIF, PNG, TIFF; }
    
        public int getObjectType(){ 
              // content not showed
        }
        
        public ImageIcon getObjectTypeIcon(ObjectType obj){ 
              // content not showed
        }
    }
    so in the file class i would like to do something like this here below
    public class NeoFile extends NeoObject{
        private byte[] filebytestring;
        private ObjectType filetype;
        
        public NeoMessage(Contact zender, Contact ontvanger, byte[] bericht){
            setSender(zender);
            setReceiver(ontvanger);
            this.filebytestring = bericht;
            
            setObjectType(PacketType.FILE);
         
            // based check on what the file extension is, lets say its a word file
            this.filetype = FileType.MS.Word;
        }
        
        public byte[] getFileByteString(){ return this.filebytestring; }
        public ObjectType getFileType(){ return this.filetype; }
    }
    this is what i'm trying to do, but i need help with those MS.WORD thingy, it doesn't wanna work in my code :)

    i hope i didnt type any wrong stuff but heh its getting quite large to look for typos
  • 4. Re: multiple enums in 1 enum class
    807603 Newbie
    Currently Being Moderated
    You probably want to do something like this with your enums
    public class FileType{
        public static enum MS{ WORD, EXCEL, POWERPOINT, OUTLOOK; }
        public static enum Web{ HTML, PHP, XML, ASP, CHM, JS; }
        public static enum Image{ BMP, JPG, GIF, PNG, TIFF; }
    }
    The key here is the "static" keyword in front of your enums, and the fact that the enclosing structure is a class (not an enum). This construct will allow you to write stuff file FileType.MS.WORD.

    You cannot syntactically define an enum of enums like you tried to do. And if you do not use "static", you will need an instance of the FileType type to refer to its members (ie: you would need to do (new FileType()).MS.WORD).
  • 5. Re: multiple enums in 1 enum class
    807603 Newbie
    Currently Being Moderated
    i'm gonna try out all the stuff you guys gave me

    Edit:
    ok i'm kinda stuck now with the getObjectType method
    how must that method be made now ? :)
    i'm getting more and more stuck by the second if i try it myself
        public int getObjectType(){
            switch(?){
                case ?: return 0;
                case ?: return 1;
                case ?: return 2;
                case ?: return 3;
            }
            return -1;
        }
    Edited by: NeoSniperkiller on Feb 6, 2008 1:44 PM
  • 6. Re: multiple enums in 1 enum class
    807603 Newbie
    Currently Being Moderated
    Hmm, I suggest you do it that way then:
    public interface ObjectType {
        public int getType();
    }
    
    public class FileType {
        public static enum MS implements ObjectType{ 
            WORD(0), 
            EXCEL(1), 
            POWERPOINT(2), 
            OUTLOOK(3); 
            private int type;
            MS(int type){
                this.type = type;
            }
            public int getType(){
               return this.type;
            }
        }
    
        public static enum Web{ 
            HTML(4), 
            PHP(5), 
            XML(6),
            ASP(7), 
            CHM(8), 
            JS(9); 
            private int type;
            Web(int type){
                this.type = type;
            }
            public int getType(){
               return this.type;
            }
        }
    
        // Do the same for you other enums
    }
    
    }
    now if you want your object type you can do FileType.MS.WORD.getType()
  • 7. Re: multiple enums in 1 enum class
    807603 Newbie
    Currently Being Moderated
    But really, is there any reasons you use 3 separate enums? It seems to me that you would be fine with just one enum with constants like MS_WORD, MS_EXCEL, WEB_JS, etc... Do you have a reason besides syntactical beauty to use 3 enums instead of 1. You should check if it possible to merge these 3 enums, if possible do it, or they will cause headaches all along your developement.

    If you want to keep the MS, Web, Image distinction, then consider doing as BigDaddyLoveHandles suggested.

    Edited by: npiguet on Feb 6, 2008 5:07 PM
  • 8. Re: multiple enums in 1 enum class
    807603 Newbie
    Currently Being Moderated
    if anyone could simplify my problem :) you'd be very welcome
    i'm already having seconds thoughts about it

    well the server's packet overview holds a window like this
    this is mainly how i started making the class:
    [ Packet Statistics ]
       [STATUS]                    [#]
       [NICKNAME]               [#]
       [DISPLAYPIC]             [#]
       [CONTACTWINDOW]   [#]
       [BUZZER]                    [#]
       [WEBCAM]                 [#]
       [MESSAGE]                [#]
       [FILE] => link to new window            [total #]
    
    [ File Packets Statistics ]
       [ MS ]                         [ Web ]                         [ Image ]
         [WORD] [#]                 [HTML] [#]                     [BMP] [#]
         [EXCEL] [#]                 [PHP]   [#]                     [JPG]  [#]
         [other enumvalues]...
    
       [other enums]...
    Edited by: NeoSniperkiller on Feb 6, 2008 2:11 PM
  • 9. Re: multiple enums in 1 enum class
    795426 Newbie
    Currently Being Moderated
    npiguet wrote:
    The key here is the "static" keyword in front of your enums, and the fact that the enclosing structure is a class (not an enum). This construct will allow you to write stuff file FileType.MS.WORD.

    You cannot syntactically define an enum of enums like you tried to do. And if you do not use "static", you will need an instance of the FileType type to refer to its members (ie: you would need to do (new FileType()).MS.WORD).
    Actually no.

    When an enum is nested inside another class, it automatically becomes static.
  • 10. Re: multiple enums in 1 enum class
    807603 Newbie
    Currently Being Moderated
    Okay, here's what I suggest:
    public enum FileType {
        WORD      (Flavor.MS,    0),
        EXCEL     (Flavor.MS,    1),
        POWERPOINT(Flavor.MS,    2),
        OUTLOOK   (Flavor.MS,    3),
        HTML      (Flavor.WEB,   4),
        JPG       (Flavor.IMAGE, 5);
     
        private Flavor flavor;
         private int code; 
    
        FileType(Flavor flavor, int code) {
            this.flavor = flavor;
              this.code = code;
        }
     
        public Flavor getFlavor() {
            return flavor;
        }
    
         public int getCode(){
              return this.code;
         }
    
         public static List<FileType> forFlavor(Flavor flavor){
              List<FileType> result = new ArrayList<FileType>();
              for(FileType ft : FileType.values()){
                   if(ft.flavor == flavor){
                        result.add(ft);
                   }
              }
              return result;
         }
    
         public static FileType forCode(int code){
              for(FileType ft : FileType.values()){
                   if(ft.code == code){
                        return ft;
                   }
              }
              return null;
         }
    }
    
    public enum Flavor {
         MS, WEB, IMAGE
    }
    now if when you build your UI, you can do the following:
    for(Flavor flavor : Flavor.values()){
        // construct some kind of column or group
        for(FileType ft : FileType.forFlavor(flavor)){
            // now add you counting UI to the column or group
        }
    }
    now when you receive a packet you could have something like
    EnumMap<FileType, Integer> countByType = new EnumMap<FileType, Integer>();
    EnumMap<Flavor, Integer> countByFlavor = new EnumMap<Flavor, Integer>();
    
    int fileTypeCode = myPacket.getFileType();
    FileType fileType = FileType.forCode(fileTypeCode);
    
    Integer count = countByType.get(fileType);
    if(count == null){
        count == 0;
    }
    countByType.put(fileType, count ++);
    
    count = countByFlavor.get(fileType.getFlavor());
    if(counr == null){
        count == 0;
    }
    countByFlavor.put(fileType.getFlavor(), count ++);
  • 11. Re: multiple enums in 1 enum class
    807603 Newbie
    Currently Being Moderated
    thanks man !!!
    you deserve a statue ^^
  • 12. Re: multiple enums in 1 enum class
    795426 Newbie
    Currently Being Moderated
    NeoSniperkiller wrote:
    if anyone could simplify my problem :) you'd be very welcome
    i'm already having seconds thoughts about it
    Instead of trying to nest enums (because that's silly), instead you only need two enum types.

    enum #1 --> Packet Category (MS, WEB, IMAGE)
    enum #2 --> Packet Type (the actual types)

    when you set up enum #2, give each enum value a category as part of the constructor and save it as an instance member. Then you just define a method to access the type category.
  • 13. Re: multiple enums in 1 enum class
    807603 Newbie
    Currently Being Moderated
    DeltaGeek wrote:
    When an enum is nested inside another class, it automatically becomes static.
    Interesting thanks for the precision. I guess this is because keeping a reference to the outer instance would make the enum members not immutable, and immutability is one of the important characteristics of members of an enum.
  • 14. Re: multiple enums in 1 enum class
    807603 Newbie
    Currently Being Moderated
    nvm thanks for the help guys !!

    Edited by: NeoSniperkiller on Feb 7, 2008 10:05 AM