Results 1 to 12 of 12
  1. #1
    Sir G. is offline Member
    Join Date
    Oct 2009
    Posts
    6
    Rep Power
    0

    Default variable number of fields with different types

    I need to create a class which offers a variable amount of fields. These fields might be String or int.

    Example:

    myObject1 might have as fields:
    description - String
    idNumber - int
    authorName - String

    myObject2
    might contain
    description - String
    idNumber - int
    authorName - String
    year - int

    myObject1 and myObject2 should be objects of the same class.
    This needs to be completely transparant. I should be able to get the values using:

    myObject.getValue("description") ... which should return a string
    myObject.getValue("year") ...which should return an integer

    I assume the underlying datastructure should have a hashmap-like structure, so I can map the name of the field to it's value, however, I seem to be unable to figure out how let getValue return an String in the first case, and an Integer in the second.

    note that I do not want to return Object. I want to have a clear distinction between String or integer. I have no clue which fields might occur, or how many string-fields or int-fields it might have.

    any help would be appreciated.
    Thanks

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,352
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Sir G. View Post
    I need to create a class which offers a variable amount of fields. These fields might be String or int.

    Example:

    myObject1 might have as fields:
    description - String
    idNumber - int
    authorName - String

    myObject2
    might contain
    description - String
    idNumber - int
    authorName - String
    year - int

    myObject1 and myObject2 should be objects of the same class.
    This needs to be completely transparant. I should be able to get the values using:

    myObject.getValue("description") ... which should return a string
    myObject.getValue("year") ...which should return an integer

    I assume the underlying datastructure should have a hashmap-like structure, so I can map the name of the field to it's value, however, I seem to be unable to figure out how let getValue return an String in the first case, and an Integer in the second.

    note that I do not want to return Object. I want to have a clear distinction between String or integer. I have no clue which fields might occur, or how many string-fields or int-fields it might have.

    any help would be appreciated.
    Thanks
    Have two maps, one for the String type 'variables' and one for the Integer type 'variables'?

    kind regards,

    Jos

  3. #3
    Sir G. is offline Member
    Join Date
    Oct 2009
    Posts
    6
    Rep Power
    0

    Default

    that would leave me with two inspectors (one for each map), one returning ints, the other strings.

    I would like to make it completely transparant that I have two maps. I would like only one inspector getValue(key) that would return an integer if key is the name of a field that holds an integer and return a string when the key is the name of a string-type field.

    The only way I see of doing that directly would be like this:
    Java Code:
    public [B]Object[/B] getValue(String key){
          if (stringmap.containsKey(key))
                  return stringmap.get(key);
          else
                  return intmap.get(key);
    }
    But, like I mentioned above, I don't want to return objects.
    So I'm guessing I'm in the need of some sort of generics/inheritance/polymorphism-trick I fail to see.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,352
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Sir G. View Post
    I would like to make it completely transparant that I have two maps. I would like only one inspector getValue(key) that would return an integer if key is the name of a field that holds an integer and return a string when the key is the name of a string-type field.
    The only thing in common of a String type and an Integer type is an Object type but you don't want that and a method can only return one type (if not a void method). Using exceptions for one of the types is ugly and not done. Defining a class like this:

    Java Code:
    public class ReturnType {
       public Integer intReturn;
       public String stringReturn;
    }
    is clumsy at best because the receiver of such an object still has to inspect the members of that object in order to determine the actual type of the original return value.

    kind regards,

    Jos
    Last edited by JosAH; 03-28-2010 at 02:20 PM.

  5. #5
    Sir G. is offline Member
    Join Date
    Oct 2009
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    The only thing in common of a String type and an Integer type is an Object type but you don't want that and a method can only return one type (if not a void method). Using exceptions for one of the types is ugly and not done.

    kind regards,

    Jos
    I am fully aware of that. I do not intend to use exceptions in any way. I have been looking for solutions using generics, inheritance and overriding or even the creational design patterns of the Gang of Four. I have always encountered a dead end. I am starting to belief there is no way of pulling this of in java (in a clean way). Can you confirm that?

    If so, I will probably stick to two seperate inspectors. This will render the interface less simple, since all classes using this class will have to develop logic to know which inspector to use..

    Thanks

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,352
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Sir G. View Post
    I am fully aware of that. I do not intend to use exceptions in any way. I have been looking for solutions using generics, inheritance and overriding or even the creational design patterns of the Gang of Four. I have always encountered a dead end. I am starting to belief there is no way of pulling this of in java (in a clean way). Can you confirm that?
    Not yet ;-) A caller of your String/Integer map has to either process an Integer or a String; define an interface for it:

    Java Code:
    public interface Processor {
       public void processInteger(Integer theValue);
       public void processString(String theValue);
    }
    have your calling class(es) implement this interface and define a getter in your Integer/String map class like this:

    Java Code:
    public boolean get(String key, Processor p) {
       Object theValue= map.get(key);
       if (theValue == null) return false; // nothing found
       if (theValue instanceof String) p.processString((String)theValue);
       else p.processInteger((Integer)theValue);
       return true; // signal that something was found
    }
    All the nasty casting is done in your map-class. Does that help you out a bit?

    kind regards,

    Jos

  7. #7
    Sir G. is offline Member
    Join Date
    Oct 2009
    Posts
    6
    Rep Power
    0

    Default

    That might be a solution. Thank you very much.

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,352
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Sir G. View Post
    That might be a solution. Thank you very much.
    You're welcome. The solution I proposed is called "double dispatch" or "visitor" in the GoF book.

    kind regards,

    Jos

  9. #9
    Sir G. is offline Member
    Join Date
    Oct 2009
    Posts
    6
    Rep Power
    0

    Default

    Ah, visitor. I'll have to reread that one. I'll look it up right away. Thanks.

  10. #10
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Also posted at bytes.com.

    @SirG: Could you please alert people if you must do this? (Ie, could you link from each part of the discussion to the others?). See, for instance, JavaRanch's guidelines.

  11. #11
    Sir G. is offline Member
    Join Date
    Oct 2009
    Posts
    6
    Rep Power
    0

    Default

    ofcourse, will do

  12. #12
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

Similar Threads

  1. Finding a number in array close to another number
    By SteroidalPsycho in forum New To Java
    Replies: 2
    Last Post: 02-15-2010, 12:37 AM
  2. Printing the Number of Times a Number in a Range Shows up
    By space4rent00 in forum New To Java
    Replies: 1
    Last Post: 02-05-2010, 10:42 PM
  3. Store the decimal number into an variable
    By fataguila in forum New To Java
    Replies: 4
    Last Post: 02-01-2010, 07:22 PM
  4. Dynamic types for a variable.
    By Somelauw in forum New To Java
    Replies: 5
    Last Post: 11-27-2009, 10:38 AM
  5. Replies: 7
    Last Post: 05-06-2009, 04:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •