Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

java type parsing

Status
Not open for further replies.
Apr 16, 2009
1
US
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 type 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.

I want to avoid using reflection as much as possible as it is expensive, ugly, and error-prone.

Please share your experience and thoughts.

thanks,
Sam
 
How many files do you have? How many types do they exist of?

Maybe you can create good fitting classes on the fly
Code:
class LFFI {
  long v1;
  float v2;
  float v3;
  int v4;
}
Sure your types are java-types (int, float, long)?

Another idea might be to use a sql-database, create insert statements, and do the aggregation by the database.

don't visit my homepage:
 
Some ideas:

- You can consider using parseFloat instead of valueOf to get primitive types

- Are you actually using longs, ie, numbers bigger than 2,147,483,647.?


Cheers,
Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top