roland
Full Member   Posts: 235 Medals: 6
|
 |
«
on:
2012-01-15 23:44:15 » |
|
Hi, I have a few methods to easily get a value from a HashMap<String key><String value> 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? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| 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); } } |
|
|
|
|
ra4king
JGO Kernel      Posts: 3155 Medals: 196
I'm the King!
|
 |
«
Reply #1 on:
2012-01-15 23:56:25 » |
|
It looks fine. Why are you worried about it if it works?
|
|
|
|
roland
Full Member   Posts: 235 Medals: 6
|
 |
«
Reply #2 on:
2012-01-15 23:58:16 » |
|
well I just wanted to make sure I wasn't doing something bad. You never know with generics 
|
|
|
|
Games published by our own members! Go get 'em!
|
|
ra4king
JGO Kernel      Posts: 3155 Medals: 196
I'm the King!
|
 |
«
Reply #3 on:
2012-01-16 00:02:46 » |
|
Hahahahaha 
|
|
|
|
ra4king
JGO Kernel      Posts: 3155 Medals: 196
I'm the King!
|
 |
«
Reply #4 on:
2012-01-16 00:04:23 » |
|
If you want to avoid casting when the Enum is returned: 1 2 3
| 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: 1
| Enum<MyEnums> e = getEnum("myKey",MyEnums.DEFAULT); |
Of course, you will still have to cast to get it to the original enum type: 1
| MyEnums myE = (MyEnums)e; |
|
|
|
|
roland
Full Member   Posts: 235 Medals: 6
|
 |
«
Reply #5 on:
2012-01-16 00:13:16 » |
|
Hmm.. so you do need to cast  seems like more effort to me!
|
|
|
|
ra4king
JGO Kernel      Posts: 3155 Medals: 196
I'm the King!
|
 |
«
Reply #6 on:
2012-01-16 00:16:01 » |
|
Aha! To completely remove the need for casting: 1 2 3
| public <T extends Enum<T>> T getEnum(String key, Enum<T> defaultValue) { .... } |
so 1
| MyEnums e = getEnum("myEnum",MyEnums.DEFAULT); |
I LOVE generics 
|
|
|
|
roland
Full Member   Posts: 235 Medals: 6
|
 |
«
Reply #7 on:
2012-01-16 00:21:27 » |
|
That didn't work for me 
|
|
|
|
ra4king
JGO Kernel      Posts: 3155 Medals: 196
I'm the King!
|
 |
«
Reply #8 on:
2012-01-16 00:24:06 » |
|
Why not? It works for me. What error did you get?
|
|
|
|
roland
Full Member   Posts: 235 Medals: 6
|
 |
«
Reply #9 on:
2012-01-16 00:30:35 » |
|
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<T> to T or return (T)defaultValue; Type safety: Unchecked cast from Enum<T> to T
|
|
|
|
Games published by our own members! Go get 'em!
|
|
ra4king
JGO Kernel      Posts: 3155 Medals: 196
I'm the King!
|
 |
«
Reply #10 on:
2012-01-16 00:33:26 » |
|
1
| return Enum.valueOf(defaultValue.getDeclaringClass(),value); |
That should work with my latest method signature so your method should look like: 1 2 3 4 5 6 7 8 9 10 11 12
| 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); } } |
|
|
|
|
roland
Full Member   Posts: 235 Medals: 6
|
 |
«
Reply #11 on:
2012-01-16 00:36:06 » |
|
I copied your method and am still getting that type mismatch, then having to cast to T giving me the warning 
|
|
|
|
ra4king
JGO Kernel      Posts: 3155 Medals: 196
I'm the King!
|
 |
«
Reply #12 on:
2012-01-16 00:45:32 » |
|
OH! Change "Enum<T> defaultValue" to "T defaultValue"
|
|
|
|
roland
Full Member   Posts: 235 Medals: 6
|
 |
«
Reply #13 on:
2012-01-16 00:48:19 » |
|
OH! Change "Enum<T> defaultValue" to "T defaultValue"
Works perfectly  thanks ra4king!
|
|
|
|
ra4king
JGO Kernel      Posts: 3155 Medals: 196
I'm the King!
|
 |
«
Reply #14 on:
2012-01-16 00:53:20 » |
|
Glad to help 
|
|
|
|
Danny02
Full Member   Posts: 123 Medals: 8
|
 |
«
Reply #15 on:
2012-01-16 12:14:44 » |
|
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.
|
|
|
|
|
roland
Full Member   Posts: 235 Medals: 6
|
 |
«
Reply #16 on:
2012-01-16 21:56:31 » |
|
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  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| 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; } } } |
|
|
|
|
BoBear2681
Full Member   Posts: 238 Medals: 8
|
 |
«
Reply #17 on:
2012-01-16 23:49:29 » |
|
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.
|
|
|
|
|
ra4king
JGO Kernel      Posts: 3155 Medals: 196
I'm the King!
|
 |
«
Reply #18 on:
2012-01-17 00:17:26 » |
|
@BoBear2681 There is only 1 line and 1 condition where this error might occur 
|
|
|
|
theagentd
JGO Wizard     Posts: 1392 Medals: 88
|
 |
«
Reply #19 on:
2012-01-17 04:58:24 » |
|
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.
|
There is no god.
|
|
|
ra4king
JGO Kernel      Posts: 3155 Medals: 196
I'm the King!
|
 |
«
Reply #20 on:
2012-01-17 05:09:21 » |
|
Point taken 
|
|
|
|
|