Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to write a java object with a lot of optional properties

Tags:

java

pojo

I have to write a Java object which is used to hold calculation results. The results contains a large number of fields which may or may not be set depends on the type of algorithm used. For example:

class EquityValuationResult {
    private ValuationAlgorithm algorithm;
    private int yield;
    private double curve;
    private double meanValue;
    private double probability;
    private int standardDeviation;
    private boolean approximateValue;
    ......
    //Getter and Setters

}

For different ValuationAlgorithm, the contents of these properties may be different. For example, if the algorithm is A, yield and probability will contain the calculation value, the rest of those will be null; if the algorithm is B, standardDeviation and curve will contain the result and the rest of those will be null, etc. The rule is very complicated, for example, if approcimateValue is true, some of the value will be overridden etc. Therefore, all these properties have to be in one class as they logically is one result.

An alternative way to do this is to use a Map

class EquityValuationResult {
    private final String YIELD = "yield";
    private final String CURVE = "curve";
    ........

    private ValuationAlgorithm algorithm;
    private final Map<String, Object> result = new HashMap<String, Object>();

    // Getter and Setters
}

But if I do it like this, the getter and setter has to convert the values from Object to corresponding data type. I also have to define those constants and use them as the key of the map, this looks too cumbersome.

Which will be the better way in your opinion? Are there any other better way to do this?

EDIT: Forgot to mention, creating separate class for each calculationType is not an option due to constraints. I have to use one class.

like image 442
Kevin Avatar asked Oct 14 '14 21:10

Kevin


People also ask

Why is Java optional 8?

Optional object is used to represent null with absent value. This class has various utility methods to facilitate code to handle values as 'available' or 'not available' instead of checking null values. It is introduced in Java 8 and is similar to what Optional is in Guava.

How do you set the value of an optional object in Java?

Programmers can also use the ofNullable() method, which will return an empty Optional object if the value is null, or an Optional object containing the given value if it is non-null. Finally, you can create an empty Optional object using the empty() method.


1 Answers

Maybe one option is to create an enum class which represents the variable names:

public enum ResultKey {

    YIELD,
    CURVE,
    MEAN_VALUE,
    ...

    // you can add getValue(Map<ResultKey, Object> map) and
    // setValue(Map<ResultKey, Object> map, Object value) methods

}

Then in your result class have a map:

class EquityValuationResult {

    private ValuationAlgorithm algorithm;
    private Map<ResultKey, Object> result = new HashMap<>();

    // Getter and Setters
}

So it is essentially like your map idea, but with an enum.

like image 107
nmore Avatar answered Nov 14 '22 23:11

nmore