• Post Reply Bookmark Topic Watch Topic
  • New Topic

Dynamically invoking functions/classes based on metadata (datatype)  RSS feed

 
Reddy Gurram
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I have 3 fields that should be converted into different (common) formats as below:
--------------------------------------
INPUT OUTPUT
--------------------------------------
Field1311220012001-12-31
Field2$1234.88123488
Field3Field#03Field03
--------------------------------------

---------------------------------------------------------
------------------------ a ------------------------------
---------------------------------------------------------
In the procedural programming world (currently implemented), I�ll define custom input_data_types for these fields as
* DateDDMMYYYY,
* Dollars,
* AlphaNums,
for Field1, Field2 and Field3 respectively.

During run time, the program, based on the data types, applies the following functions to convert from input format to output format (canonical).
(1) convert_date_from_DDMMYYYY_to_YYYY-MM-DD (date)
(2) convert_dollars_to_cents (dollars)
(3) convert_trim_non_alpha_num (string)

The resulted fields are now in in common/canonical format.
---------------------------------------------------------
------------------------ b ------------------------------
---------------------------------------------------------
Now, if I would like to implement the same logic in OO/Java, what is the best way to go about?

Use a common class for similar conversion functions. How do we go about picking the �right/required� conversion function dynamically based on available datatype (metadata)?

Any pointers are appreciated.
 
William A White
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where is your data coming from? Is it read from a database, flat file, user input, web stream, seomething else?
 
Alexandru Popescu
Ranch Hand
Posts: 995
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If i imagine correctly this seems like a homework and most probably it is related to inheritance. Take a look at different toString() formats you get from different object types.

--
:alex |.::the_mindstorm::.
 
Reddy Gurram
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's definitely not a homework...
Planning to move current procedural-based-system into OO-based-system.
***You can make an assumption that the data and metadata is readily available***
Parsing the data is not an issue here though.
Here is an example in hierarchical format (ignore formatting).
Basically,
(a) input has data submitted by user in predefined format.
(b) have metadata defined for all the fields.
----------------------------------
---------- INPUT DATA ------------
----------------------------------
+---Data_Root
� +---WWW01
� +---20041231
� +---15,000.00$
� +---#Server with RAID#

+---MetaData_Root
� +---String
� +---Date_YYYYMMDD
� +---Currency_Dollars_CommaSeperated
� +---String

-------------------------------------------------------
---------- OUTPUT DATA (in Canonical format)-----------
-------------------------------------------------------
+---Data_Root
� +---WWW
� +---2004-12-31
� +---15000.00
� +---#Server with RAID#

Output data should be as above (ignore formatting).
What is the best way to implement these in Java. I do have a
vague idea of using polymorphism/inheritance. But, I'm more or
less looking for production grade solution rahter than an academic solution.

Advance thanks for the help.

Cheers,
Gurram
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe a strategy kinda thing ...

Polymorphism comes into play as we make a bunch of different converters that all implement an interface with the convert(input) method. There might be a DateConverter, a MoneyConverter, a StringConverter and so on. ConverterFactory might be as simple as a HashMap that looks up the metadata type name and returns the proper converter. It might be a bit trickier if you'd like to load metadata-converter mappings from configuration or reload the map without recycling the program.

BTW: I showed returning Object because I'm not sure if your output form is all strings or might be other objects like Java Date. If they're all Strings it could return String.

Does that look appropriate?

Oh, almost forgot ... welcome to the ranch! And check out the naming rules before the law comes around. Your user name has to be a believable first & last name, but not necessarily your real name.
[ March 09, 2005: Message edited by: Stan James ]
 
Marilyn de Queiroz
Sheriff
Posts: 9082
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Gurram

Welcome to JavaRanch! Please adjust your display name to meet the JavaRanch Naming Policy.
You can change it here.

Thanks!
 
Reddy Gurram
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Stan James!!

That sounds like the way to go.
Probably, I'll head in the direction you suggested and try to get the proof-of-concept/prototype soon working.

Thanks a million again James.


Cheers,
Reddy Gurram
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!