Results 1 to 12 of 12
- 03-28-2010, 01:54 PM #1
Member
- Join Date
- Oct 2009
- Posts
- 6
- Rep Power
- 0
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
- 03-28-2010, 02:01 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
- 03-28-2010, 02:08 PM #3
Member
- Join Date
- Oct 2009
- Posts
- 6
- Rep Power
- 0
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:
But, like I mentioned above, I don't want to return objects.Java Code:public [B]Object[/B] getValue(String key){ if (stringmap.containsKey(key)) return stringmap.get(key); else return intmap.get(key); }
So I'm guessing I'm in the need of some sort of generics/inheritance/polymorphism-trick I fail to see.
- 03-28-2010, 02:17 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
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:
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.Java Code:public class ReturnType { public Integer intReturn; public String stringReturn; }
kind regards,
JosLast edited by JosAH; 03-28-2010 at 02:20 PM.
- 03-28-2010, 02:26 PM #5
Member
- Join Date
- Oct 2009
- Posts
- 6
- Rep Power
- 0
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
- 03-28-2010, 02:44 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
Not yet ;-) A caller of your String/Integer map has to either process an Integer or a String; define an interface for it:
have your calling class(es) implement this interface and define a getter in your Integer/String map class like this:Java Code:public interface Processor { public void processInteger(Integer theValue); public void processString(String theValue); }
All the nasty casting is done in your map-class. Does that help you out a bit?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 }
kind regards,
Jos
- 03-28-2010, 06:28 PM #7
Member
- Join Date
- Oct 2009
- Posts
- 6
- Rep Power
- 0
That might be a solution. Thank you very much.
- 03-28-2010, 06:30 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
- 03-28-2010, 06:34 PM #9
Member
- Join Date
- Oct 2009
- Posts
- 6
- Rep Power
- 0
Ah, visitor. I'll have to reread that one. I'll look it up right away. Thanks.
- 03-28-2010, 09:26 PM #10
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
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.
- 03-28-2010, 09:30 PM #11
Member
- Join Date
- Oct 2009
- Posts
- 6
- Rep Power
- 0
ofcourse, will do
- 03-29-2010, 12:40 AM #12
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
Similar Threads
-
Finding a number in array close to another number
By SteroidalPsycho in forum New To JavaReplies: 2Last Post: 02-15-2010, 12:37 AM -
Printing the Number of Times a Number in a Range Shows up
By space4rent00 in forum New To JavaReplies: 1Last Post: 02-05-2010, 10:42 PM -
Store the decimal number into an variable
By fataguila in forum New To JavaReplies: 4Last Post: 02-01-2010, 07:22 PM -
Dynamic types for a variable.
By Somelauw in forum New To JavaReplies: 5Last Post: 11-27-2009, 10:38 AM -
[SOLVED] Append an incremental number to variable name
By porchrat in forum New To JavaReplies: 7Last Post: 05-06-2009, 04:32 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks