Configuration getList()

Discussion in 'Plugin Development' started by Magik, Feb 18, 2011.

Thread Status:
Not open for further replies.
  1. Offline

    Magik

    Is there any easy way to get a list of objects from a yaml file where the list contents contain mixed date-types ( e.g. strings/doubles/ints ), without having to run parseDouble/parseInt a bunch of times? It's been a while since I've used Java, and maybe I'm thinking about this the wrong way...

    are the getNode() function still broken? The javadoc appears to comment that they are?

    Right now I pulled the list using getStringList() and it's kind of a pain in the ass, because it gives me all the nodes in the list item as a string, and I had to token parse the string and set them into a HashMap to make it more node like and also run the parseDouble/parseInt's on them to get them to the format I want.

    If I use a HashMap<String,Object> then the compiler doesn't necessarily know at compile time that the Object is whatever I wanted it to be ( String/Double/Integer ). How do I get around this? Am I supposed to just type cast? Is there some way of using instanceof to not have compile time type mismatches?
     
  2. Offline

    toadmess

    It's a while since your post, and I don't have answers your questions. Thought I'd reply anyway as I've have been wondering about this too. I ended up just using instanceof to figure out the type. e.g.

    Code:
    final Object radiusProp = conf.getProperty(radiusPath);
    if(radiusProp instanceof Number) {
      this.foo = (float) conf.getFloat(radiusPath, 1.0F);
    } else if(radiusProp instanceof List) {
      for(final Object el : (List<?>) radiusProp) {
        if(el instanceof HashMap) {
          //etc etc
    
    It relies on knowing that SnakeYaml will be creating List and HashMap objects for the YAML sequences and mappings. Not ideal, but it might be easier than parsing strings.

    I'd love to know a better way of going about it.
     
  3. Offline

    Cheesier

    Can't you do this
    Code:
    final Object radiusProp = conf.getProperty(radiusPath);
    radiusProp.getClass().getName()
    And then just compare as strings.
     
  4. Offline

    toadmess

    Yes, that's possible too. There's perhaps a tad more flexibility with the instanceof operator becuase you can check against superclasses as well.
    In practice though, I'm probably just freaking out about type safety. Must spend more time away from Java to chill out. :)
     
Thread Status:
Not open for further replies.

Share This Page