ridencww / goldengine

Java implementation of Devin Cook's GOLD Parser engine
Other
35 stars 14 forks source link

Abstract Variable ? #10

Closed stefv closed 10 years ago

stefv commented 11 years ago

Hi Ralph,

Sorry if it's not the good place for that, but I'm a new user with github.

Just one question: why the Variable class is not an abstract class or an interface ? I'm asking that because I have the feeling the implementation of Variable is limited for me.

For example, Variable is using asDouble(), asInt() and asNumber() when it's possible (i think) to use one method for the integers and one for the doubles or to leave the choice to the final user of your GoldEngine to use int, Integer, long, Long, BigDecimal, BigInteger or any other types.

One goal is also for me to have the choice to use one implementation or another for this Variable. For example, for the fast execution, to use only basic types or for a very good precision, to use one with BigDecimal and BigInteger just switching the variable type.

Regards, Stef

ridencww commented 11 years ago

Stef,

You raise a good point about the declaration and implementation of Variable. Making it an interface or abstract class and providing separate concrete implementations would be good object-oriented practice and would provide a great deal of flexibility.

My rationale for the single Variable class is based on a number of design objectives and actually goes back to the first incarnation of the class in Turbo Pascal many, many years ago. First, I wanted a light-weight mechanism to coerce one type of Variable to another. For example, "DATE() + 30 days", which takes a Timestamp, Numeric, and String to generate the final result of a Timestamp. It just seemed convenient to take a Variable instance and be able to express it as another data type. This is particularly useful when generating output in the form of a String when writing to a file, display, or printer.

My second objective was to keep rounding errors to the minimum (the TP implementation used BCD arithmetic). Once any calculations are made, the user can directly retrieve the numeric result in most of the native types. In practice, this is an effective approach. I've used Variable in expression evaluators processing millions of rows and the evaluator wasn't the processing bottleneck. The four data types supported by Variable (String, Numeric, Boolean, and Timestamp) have covered my needs thus far.

This is not to say that a change might not be in order and moving to an interface or abstract class would be difficult or complex. This engine is a Java port of Devin's reference engine and I wanted it to be close to a one-to-one match. There are a number of opportunities to leverage Java and I am considering what benefits could be obtained with a different implementation.

I want to think about your comments a bit more and assess the impact of moving to an interface or abstract class.

Thanks for the input.

Ralph

ridencww commented 10 years ago

Moved to Suggestions in the Wiki and closing as an issue. Thank you for the suggestion.