Java-Gaming.org Java4K winners: [ by our judges | by the community ]         
Featured games (67)
games approved by the League of Dukes
Games in Showcase (∞)
games submitted by our members



News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  Print  
  INI loading generics  (Read 678 times)
0 Members and 1 Guest are viewing this topic.
Offline 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);
      }
   }

Offline 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?

Offline 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  Grin

Games published by our own members! Go get 'em!
Offline ra4king

JGO Kernel
*****

Posts: 3155
Medals: 196


I'm the King!


« Reply #3 on: 2012-01-16 00:02:46 »

Hahahahaha Grin

Offline 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;

Offline roland

Full Member
**

Posts: 235
Medals: 6



« Reply #5 on: 2012-01-16 00:13:16 »

Hmm.. so you do need to cast  Wink seems like more effort to me!

Offline 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 Smiley

Offline roland

Full Member
**

Posts: 235
Medals: 6



« Reply #7 on: 2012-01-16 00:21:27 »

That didn't work for me  Clueless

Offline 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?

Offline 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!
Offline 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);
    }
}

Offline 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  Sad

Offline 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"

Offline 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  Smiley thanks ra4king!

Offline ra4king

JGO Kernel
*****

Posts: 3155
Medals: 196


I'm the King!


« Reply #14 on: 2012-01-16 00:53:20 »

Glad to help Grin

Offline 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.
Offline 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 Smiley

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;
         }
      }  
   }

Offline 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.
Offline 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 Wink

Offline 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.
Offline ra4king

JGO Kernel
*****

Posts: 3155
Medals: 196


I'm the King!


« Reply #20 on: 2012-01-17 05:09:21 »

Point taken Tongue

Pages: [1]
  Print  
 
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.115 seconds with 20 queries.