Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (535)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  INI loading generics  (Read 1543 times)
0 Members and 1 Guest are viewing this topic.
Offline roland
« Posted 2012-01-16 05: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


Medals: 337
Projects: 2
Exp: 5 years


I'm the King!


« Reply #1 - Posted 2012-01-16 05:56:25 »

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

Offline roland
« Reply #2 - Posted 2012-01-16 05: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! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ra4king

JGO Kernel


Medals: 337
Projects: 2
Exp: 5 years


I'm the King!


« Reply #3 - Posted 2012-01-16 06:02:46 »

Hahahahaha Grin

Offline ra4king

JGO Kernel


Medals: 337
Projects: 2
Exp: 5 years


I'm the King!


« Reply #4 - Posted 2012-01-16 06: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
« Reply #5 - Posted 2012-01-16 06:13:16 »

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

JGO Kernel


Medals: 337
Projects: 2
Exp: 5 years


I'm the King!


« Reply #6 - Posted 2012-01-16 06: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
« Reply #7 - Posted 2012-01-16 06:21:27 »

That didn't work for me  Clueless
Offline ra4king

JGO Kernel


Medals: 337
Projects: 2
Exp: 5 years


I'm the King!


« Reply #8 - Posted 2012-01-16 06:24:06 »

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

Offline roland
« Reply #9 - Posted 2012-01-16 06: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! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ra4king

JGO Kernel


Medals: 337
Projects: 2
Exp: 5 years


I'm the King!


« Reply #10 - Posted 2012-01-16 06: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
« Reply #11 - Posted 2012-01-16 06: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


Medals: 337
Projects: 2
Exp: 5 years


I'm the King!


« Reply #12 - Posted 2012-01-16 06:45:32 »

OH! Change "Enum<T> defaultValue" to "T defaultValue"

Offline roland
« Reply #13 - Posted 2012-01-16 06:48:19 »

OH! Change "Enum<T> defaultValue" to "T defaultValue"

Works perfectly  Smiley thanks ra4king!
Offline ra4king

JGO Kernel


Medals: 337
Projects: 2
Exp: 5 years


I'm the King!


« Reply #14 - Posted 2012-01-16 06:53:20 »

Glad to help Grin

Offline Danny02
« Reply #15 - Posted 2012-01-16 18: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
« Reply #16 - Posted 2012-01-17 03: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

JGO Coder


Medals: 18



« Reply #17 - Posted 2012-01-17 05: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


Medals: 337
Projects: 2
Exp: 5 years


I'm the King!


« Reply #18 - Posted 2012-01-17 06:17:26 »

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

Offline theagentd
« Reply #19 - Posted 2012-01-17 10: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.

Myomyomyo.
Offline ra4king

JGO Kernel


Medals: 337
Projects: 2
Exp: 5 years


I'm the King!


« Reply #20 - Posted 2012-01-17 11:09:21 »

Point taken Tongue

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

E.R. Fleming (13 views)
2014-07-29 03:07:13

E.R. Fleming (4 views)
2014-07-29 03:06:25

pw (39 views)
2014-07-24 01:59:36

Riven (39 views)
2014-07-23 21:16:32

Riven (26 views)
2014-07-23 21:07:15

Riven (28 views)
2014-07-23 20:56:16

ctomni231 (59 views)
2014-07-18 06:55:21

Zero Volt (50 views)
2014-07-17 23:47:54

danieldean (42 views)
2014-07-17 23:41:23

MustardPeter (44 views)
2014-07-16 23:30:00
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!