So I have this class containing all my Enum types, also including methods for fetching Enum types based on their title attribute:
The second method (getAttribute) is created by copy-paste and I also need to repeat this exercise with several other types of Enums. This seems like a waste of code however.abstract class Enums { static private Landmass getLandmass(String name) { for ( Landmass l : Landmass.values( ) ) { if(l.title.equals(name)){ return l; } } return null; } static private Attribute getAttribute(String name) { for ( Attribute a : Attribute.values( ) ) { if(a.title.equals(name)){ return a; } } return null; }
Instead, I thought I'd create a generic method for fetching any type of Enums. As far as I could follow the tutorial @ Oracle I need a generic class for this. Thus the EnumHelper class:
abstract class EnumHelper<T> { private T getEnum(T type, String name) { for ( type t : type.values( ) ) { if(t.title.equals(name)){ return t; } } return null; } }
This, however, doesn't compute:
To be honest I haven't been able to make much sense of the documentation on generics, thus its no surprise I'm stuck. Hopefully someone will be able to explain the logic behind this generics thing.horoscope\Enums.java:234: error: cannot find symbol for ( type t : type.values( ) ) { ^ symbol: class type location: class EnumHelper<T> where T is a type-variable: T extends Object declared in class EnumHelper horoscope\Enums.java:234: error: cannot find symbol for ( type t : type.values( ) ) { ^ symbol: method values() location: variable type of type T where T is a type-variable: T extends Object declared in class EnumHelper 2 errors