INI loading generics

Hi, I have a few methods to easily get a value from a HashMap which I load into from an .ini file.
The first method returns a float value from the ini if the key exists, otherwise returns the default value. The second method returns an enum of any type depending on what default value you put in. I’m not sure about the second method. It works, but is it correct?


	public float GetFloat(String key, float defaultValue)
	{
		String value = m_mapValues.get(key);
		if (value == null)
			return defaultValue;
		else
			return Float.parseFloat(value);	
	}

	public Enum<?> GetEnum(String key, Enum<?> defaultValue)
	{
		String value = m_mapValues.get(key);
		
		if (value == null)
			return defaultValue;
		else
		{
			value = value.toUpperCase();
			return Enum.valueOf(defaultValue.getDeclaringClass(), value);
		}
	}

It looks fine. Why are you worried about it if it works?

well I just wanted to make sure I wasn’t doing something bad. You never know with generics ;D

Hahahahaha ;D

If you want to avoid casting when the Enum is returned:


public <T extends Enum<T>> Enum<T> getEnum(String key, Enum<T> defaultValue) {
    .....
}

With this, you must return an Enum of the same type as the defaultValue, so no casting needed:


Enum<MyEnums> e = getEnum("myKey",MyEnums.DEFAULT);

Of course, you will still have to cast to get it to the original enum type:


MyEnums myE = (MyEnums)e;

Hmm… so you do need to cast :wink: seems like more effort to me!

Aha! To completely remove the need for casting:


public <T extends Enum<T>> T getEnum(String key, Enum<T> defaultValue) {
    ....
}

so


MyEnums e = getEnum("myEnum",MyEnums.DEFAULT);

I LOVE generics :slight_smile:

That didn’t work for me :clue:

Why not? It works for me. What error did you get?

well, just a warning actually.

return defaultValue; gives Type mismatch: cannot convert from Enum to T

or return (T)defaultValue; Type safety: Unchecked cast from Enum to T


return Enum.valueOf(defaultValue.getDeclaringClass(),value);

That should work with my latest method signature so your method should look like:


public <T extends Enum<T>> T getEnum(String key, T defaultValue) {
{
    String value = m_mapValues.get(key);
    
    if (value == null)
        return defaultValue;
    else
    {
        value = value.toUpperCase();
        return Enum.valueOf(defaultValue.getDeclaringClass(), value);
    }
}

I copied your method and am still getting that type mismatch, then having to cast to T giving me the warning :frowning:

OH! Change “Enum defaultValue” to “T defaultValue”

Works perfectly :slight_smile: thanks ra4king!

Glad to help ;D

I would add some explicit exception throwing in your code.

When the value you want to read can’t be converted to the type you want(float, enum …), I would suggest to either use the default value or throw an own exception like “TypeMissmatchException”, so the user of this function don’t have to remember to catch the runtime exceptions which might be thrown.

Thanks :slight_smile:

public float getFloat(String key, float defaultValue)
	{
		String value = m_mapValues.get(key);
		if (value == null)
			return defaultValue;
		else
		{
			try
			{
				return Float.parseFloat(value);	
			}
			catch(NumberFormatException e)
			{
				System.out.println(e.getMessage());
				return defaultValue;
			}
		}	
	}

Why are you simply printing the exception’s message, instead of e.printStackTrace()? You’re losing an awful lot of helpful debugging information there, such as the line number where the error occurred.

@BoBear2681
There is only 1 line and 1 condition where this error might occur :wink:

If you have 100 getFloat(…)s after each other you wouldn’t know which code line failed, only what the value of the read parameter was.