ShadowGeist
ShadowGeist

Reputation: 61

Instantiate EnumSet from one of two types of Enum

I have these two Enums declared as such:

public enum EnumOne implements EnumInterface {
    SomethingHere,
    SomethingThere;
    public void someMethod () { }
}

public enum EnumTwo implements EnumInterface {
    SomethingOverYonder;
    public void someMethod () { }
}

As seen here, they both implement this Interface.

public Interface EnumInterface {
     someMethod();
}

I have a class defined here that contains an EnumSet that will get all the elements from either Enum1 or Enum2. That will be defined via a method that has an index as a parameter. Something like this:

public class SomeClass {
    private EnumSet someEnum;
    public void setEnumType (int index) {
        switch (index) {
        case 1:
            someEnum = EnumSet.allOf(EnumOne.class);
            break;
        case 2:
            someEnum = EnumTwo.allOf(EnumTwo.class);
            break;
        }
    }
}

Now, I know I have to use generics to accomplish this somehow, but i don't know how to do it exactly since I'm not familiar enough with it and I'm hoping you can enlighten me a bit.

Thanks in advance for your answers.

Upvotes: 3

Views: 956

Answers (2)

newacct
newacct

Reputation: 122449

It is unclear what you are looking for, but if you just want someEnum to not be declared with a raw type, this works:

public class SomeClass {
    private EnumSet<? extends EnumInterface> someEnum;
    public void setEnumType (int index) {
        switch (index) {
        case 1:
            someEnum = EnumSet.allOf(EnumOne.class);
            break;
        case 2:
            someEnum = EnumSet.allOf(EnumTwo.class);
            break;
        }
    }
}

Upvotes: 1

Mordechai
Mordechai

Reputation: 16234

This is how I did it:

public class SomeClass {                                                                                                                                                  

    private EnumSet<? extends EnumInterface> someEnum;                                                                                                                    

    // I control it, so it's checked                                                                                                                                      
    @SuppressWarnings("unchecked")                                                                                                                                        
    public <E extends Enum<E> & EnumInterface> void setEnumType(int index) {                                                                                              
        switch (index) {                                                                                                                                                  
        case 1:                                                                                                                                                           
            someEnum = (EnumSet<E>) EnumSet.allOf(Enum1.class);                                                                                                           
            break;                                                                                                                                                        
        case 2:                                                                                                                                                           
            someEnum = (EnumSet<E>) EnumSet.allOf(Enum2.class);                                                                                                           
            break;                                                                                                                                                        
        default:                                                                                                                                                          
        }                                                                                                                                                                 

    }  


    public EnumSet<? extends EnumInterface> getEnum() {                                                                                                                   
        return someEnum;                                                                                                                                                  
    }     

}

Testing:

public static void main(String[] args) {                                                                                                                              
    SomeClass c = new SomeClass();                                                                                                                                    
    c.setEnumType(2);                                                                                                                                                 
    EnumSet<? extends EnumInterface> e = c.getEnum();                                                                                                                 
    System.out.println(e);                                                                                                                                            

    for (EnumInterface ei : e)                                                                                                                                        
        ei.someMethod();                                                                                                                                              

    }                                                                                                                                                                     

} 

Upvotes: 0

Related Questions