0

I have a class Calculation which contains a set of CalculationResult.

The CalculationResult has a property String result. It is a String, but the value can represent a calculated date, boolean, double, ...

So I was thinking of making it so that I can get the result in the right datatype:

public Date getAsDate();
public BigDecimal getAsBigDecimal();
...

But I'm not sure how I can best implement this.

Any pointers are appreciated!

1
  • Are you converting these values to a String? If so why not just keep them around in their original types? Commented Oct 20, 2011 at 8:23

1 Answer 1

4

Usually you would know what result you expect from your calculation. So you can make a public class CalculationResult<T>. Then use, for example:

CalculationResult<BigDecimal> result = calculator.getResult(..);

Otherwise, if you really have your result as string, always, then the getAsX methods can be implemented via methods like Integer.parseInt(str), new BigDecimal(str) and new Date(Long.parseLong(str)) (or using a DateFormat). But note that keeping non-text as string is wrong.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.