Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By jim829

Thread: Iterate enum type without instance

  1. #1
    maca is offline Member
    Join Date
    Jul 2014
    Location
    UK
    Posts
    20
    Rep Power
    0

    Default Iterate enum type without instance

    Is there anyway to iterate an enum type without an instance. As some context, consider the following code:

    Java Code:
    public interface GenericChecker
    {
    	public bool isValid(String str);
    }
    Java Code:
    public class EnumChecker<T extends Enum<T> >  extends GenericChecker
    {
    	private Class<T>           enumType; //No instance
    
    	public bool isValid(String str)
    	{
    		return EnumResolver.isValid(enumType, str);
    	}
    }
    The above class uses an enum resolver, part of the class implementation is below.

    Java Code:
    public final class EnumResolver {	
    	static public <T extends Enum<T>> boolean isValid(Class<T> enumType, String s)
    	{
    		boolean result = false;
    			
    		for(T value : enumType.getEnumConstants())
    		{
    			if(s.equalsIgnoreCase(value.toString()))
    			{
    				result = true;
    				break;
    			}
    		}
    		return result;
    	}
    }
    Please note that the toString method of the enum types has been overridden so that it returns the name assigned to the enum rather than the enum name itself. For example an enum might be SOME_ENUM("Assigned name"), therefore toString returns "Assigned name" rather than "SOME_ENUM". The idea is that a field from a table can be handed to the isValid(String) function on the GenericChecker base, and the derived class will then check to see if the field matches valid data as far as it is concerned.

    Thus, I can create a whole bunch of checkers easliy:

    Java Code:
    GenericChecker checker1 = EnumChecker<EnumType1>();
    GenericChecker checker2 = EnumChecker<EnumType2>();
    GenericChecker checker3 = EnumChecker<EnumType3>();
    GenericChecker checker4 = SomeOtherChecker();
    The problem is, if I use the EnumChecker then the expression "enumType.getEnumConstants()" in the isValid function blows up because enumType is null. Is there a way round this issue?

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Iterate enum type without instance

    I don't fully the purpose of all of this including your enum changes and assigning a new name, etc.
    A complete example including one of your enums might help.

    But you could not have run that code because it won't compile. You don't extend interfaces you
    implement them. And bool is not a valid type.

    Regards,
    Jim
    Last edited by jim829; 09-16-2014 at 04:09 PM.
    gimbal2 likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Iterate enum type without instance

    isValid function blows up because enumType is null. Is there a way round this issue?
    Make sure enumType has a value.
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    maca is offline Member
    Join Date
    Jul 2014
    Location
    UK
    Posts
    20
    Rep Power
    0

    Default Re: Iterate enum type without instance

    Quote Originally Posted by jim829 View Post
    I don't fully the purpose of all of this including your enum changes and assigning a new name, etc.
    A complete example including one of your enums might help.
    Ok, I'll give you an example, I'm reading a text file full of identifiers, those identifiers are three characters long, to the average person, those identifiers mean nothing, but what they represent does mean something. So I didn't want to name the enums the same as the identifiers, but instead named the enums something meaningful. The idea being that the code becomes more readable for the person following/maintaining the code. This meant that I ended up assigning the identifier name to enum as a string parameter. The text identifier from the text file therefore matches the assigned identifier name rather than the enum name. Anyway...

    Quote Originally Posted by jim829 View Post
    But you could not have run that code because it won't compile. You don't extend interfaces you
    implement them. And bool is not a valid type.
    Yeah sorry about that, that was an artifact of me creating a minimal example from my code base for this thread (an abstract base became an interface and I forgot to change extends to implements in the above example code).

  5. #5
    maca is offline Member
    Join Date
    Jul 2014
    Location
    UK
    Posts
    20
    Rep Power
    0

    Default Re: Iterate enum type without instance

    Quote Originally Posted by Norm View Post
    Make sure enumType has a value.
    Yeah, I came to that conclusion too :D

    I have fixed the problem by adding a user defined constructor to EnumChecker

    Java Code:
    public class EnumChecker<T extends Enum<T> >  implements GenericChecker 
    {     
    	private final Class<T>           enumType;       
    
    	public EnumChecker( Class<T> enumType)
    	{
    		this.enumType = enumType;
    	}
    
    	public bool isValid(String str)     
    	{         
    		return EnumResolver.isValid(enumType, str);     
    	} 
    }
    and then creating instances of EnumChecker like

    Java Code:
    GenericChecker checker = EnumChecker<EnumType1>(EnumType1.class);
    It's not as clean, but it works.

Similar Threads

  1. Enum Type
    By kashyapkeshav in forum New To Java
    Replies: 3
    Last Post: 08-19-2012, 04:08 PM
  2. java enum : type safety
    By rsai in forum New To Java
    Replies: 2
    Last Post: 07-05-2012, 03:45 AM
  3. Casting Enum Type arrays to object type arrays
    By nmvictor in forum Advanced Java
    Replies: 4
    Last Post: 02-17-2012, 12:49 PM
  4. Setting values from One Enum type to another enum type.
    By reach2sudhakar in forum New To Java
    Replies: 3
    Last Post: 09-23-2010, 06:02 PM
  5. passing an enum type as a parameter ??!
    By SCS17 in forum New To Java
    Replies: 11
    Last Post: 07-13-2008, 01:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •