Results 1 to 9 of 9
  1. #1
    sardare is offline Member
    Join Date
    Jun 2008
    Posts
    17
    Rep Power
    0

    Default java data fields parsing

    Hi,

    I have a file database containing data values for several fields. The fields are specified in a separate meta data within the database in a format as:
    fieldName, fieldType, fieldAggregationMethod
    e.g., volume, long, sum
    e.g., closePrice, float, last
    e.g., numTrades, int, sum
    The fieldType is always a basic type.

    I need to read in these fields and allocate my arrays accordingly. Then I need to read the data values for each of these fields from a database, apply aggregation operation and return the values to the user.

    I am thinking of setting up an aggregation method factory for the different aggregation operations.

    But I am not sure how to handle field types. How do I allocate arrays based on the its specified type, return the right type from my functions, set type within the field, casting operation, etc. Is there a base class for the basic types in Java.
    e.g, if I do something like:

    if (type.equals("float"))
    Object field1 = Float.valueOf(value);

    Now how do I call my aggregation operation on field1?
    something like

    public Float sum(Float f1, Float f2) {
    return f1 + f2;
    }

    I am thinking I need a wrapper around the existing java wrapper type classes and have those handle all these operations. Something like:

    public interface Value {
    public Object value();
    public Object aggregate(Operation op);
    }

    public class DoubleValue implements Value {
    Double d;
    public Double(String s) {
    // get d from s
    }
    public Object value() {
    return d;
    }
    public Object aggregate(Operation op) {
    return op.apply(d);
    }
    }

    then have a factory construct the right Value object for me.

    Is there an existing framework or example I could use for this? This looks like a common problem and I am sure there must be a good design pattern out there.

    Please share your experience and thoughts.


    thanks,
    Sam
    Last edited by sardare; 04-16-2009 at 07:41 PM.

  2. #2
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    java.lang.reflect is probably going to be your friend here. Reflection is a very powerful technology so can be tricky to get right and easy to blow everything up. This will let you programmatically deal with types and arrays though, and isn't to hard to learn the basics.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  3. #3
    sardare is offline Member
    Join Date
    Jun 2008
    Posts
    17
    Rep Power
    0

    Default

    Quote Originally Posted by OrangeDog View Post
    java.lang.reflect is probably going to be your friend here. Reflection is a very powerful technology so can be tricky to get right and easy to blow everything up. This will let you programmatically deal with types and arrays though, and isn't to hard to learn the basics.
    I want to avoid using reflection as much as possible as it is expensive, ugly, and error-prone. I prefer an OOP solution. Just wondering if there is an existing design pattern or framework for this problem.

  4. #4
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Get the database to do the work and just get the results? SQL does aggregation and everything.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  5. #5
    sardare is offline Member
    Join Date
    Jun 2008
    Posts
    17
    Rep Power
    0

    Default

    Quote Originally Posted by OrangeDog View Post
    Get the database to do the work and just get the results? SQL does aggregation and everything.
    it is not a sql database... everything is stored in flat files..

  6. #6
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Then use an SQL database. This is exactly what they are for.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  7. #7
    sardare is offline Member
    Join Date
    Jun 2008
    Posts
    17
    Rep Power
    0

    Default

    Quote Originally Posted by OrangeDog View Post
    Then use an SQL database. This is exactly what they are for.
    i cannot use sql database..

  8. #8
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Quote Originally Posted by sardare View Post
    i cannot use sql database..
    Why not?

    *paddy pad pad*
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  9. #9
    alchemist is offline Member
    Join Date
    Sep 2009
    Posts
    6
    Rep Power
    0

    Default

    This maybe what you are after, automatic object population via reflection:

    http://bytearray.brixtonjunkies.com/2009/09/24/auto-object-population-via-reflection/

Similar Threads

  1. parsing/storing large text data
    By hkansal in forum New To Java
    Replies: 4
    Last Post: 10-19-2008, 07:34 PM
  2. need very basic help with xml parsing in java
    By 2potatocakes in forum New To Java
    Replies: 7
    Last Post: 09-17-2008, 01:22 PM
  3. Parsing URL in Java
    By Java Tip in forum java.net
    Replies: 0
    Last Post: 04-07-2008, 09:14 PM
  4. Jsp / Java cannot get fields
    By Dracos in forum New To Java
    Replies: 0
    Last Post: 02-04-2008, 10:39 PM
  5. Help with text fields in Java
    By romina in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 06:29 AM

Posting Permissions

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