Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optional class-members

Tags:

java

oop

I import data from a XML file to use it internally. Now there is an uint value, which is (according to the XSD) not required. Now here is the question: How do map this behaviour in my class (it is unclear, if the Value is present or not, but I need to know at runtime)

Basically I see 3 solutions:

Solution 1: Use values of which we know that they are invalid to flag the Value as 'not-set':

public class Solution1 {
    private int optionalVal;

    public boolean isSetOptionalVal() {
        return (optionalVal>=0);
    }

    public void setOptionalVal(int val) {
        optionalVal = val;
    }

    public void unSetOptionalVal() {
        optionalVal = -1;
    }

    public int optionalVal() {
        if(isSetOptionalVal()) {
            return optionalVal;
        } else {
            return -1;
        }
    }
}

Solution 2: Use the boxed class and set it to null if the value is 'not-set':

public class Solution2 {
    private Integer optionalVal;

    public boolean isSetOptionalVal() {
        return (optionalVal!=null);
    }

    public void setOptionalVal(int val) {
        optionalVal = val;
    }

    public void unSetOptionalVal() {
        optionalVal = null;
    }

    public int optionalVal() {
        if(isSetOptionalVal()) {
            return optionalVal;
        } else {
            return -1;
        }
    }
}

Solution 3: Use an additional variable that describes the value as 'not-set':

public class Solution3 {
    private int optionalVal;
    private boolean optionalValSet;

    public boolean isSetOptionalVal() {
        return (optionalValSet);
    }

    public void setOptionalVal(int val) {
        optionalVal = val;
        optionalValSet = true;
    }

    public void unSetOptionalVal() {
        optionalValSet = false;
    }

    public int optionalVal() {
        if(isSetOptionalVal()) {
            return optionalVal;
        } else {
            return -1;
        }
    }
}

These are my proposals to solve the issue, but I don't really like any of those.

Solution 1 seems very hacky, maybe there is somewhere a point where I can't determine the invalid value.

Solution 2 is actually the solution I am using, but I only need the additional information for some of the memeber variables, so I have either to use some variables as boxed types and some as primitive (which seems inconsistent) or I have always to use the boxed types (which I don't really like).

Solution 3 seems to be the cleanest, but here I am worried, that at some place the bool isn't set correctly, which would be a hard to find error (I already have a lot of code, and found the problem, that some elements are not set in the XML just recently)

So...what would you prefer as a solution to solve the "Optional Value"-problem - is there maybe an even better solution? How is this problem generally handled?

like image 555
Philipp Wendt Avatar asked Dec 06 '25 08:12

Philipp Wendt


1 Answers

I'd choose option 2, using the Integer class, and leave the conversion between int and Integer to autoboxing. The advantage of this approach is that it keeps everything concerned with your optional value in a single variable.

The first option is a magic value, and if the unused value becomes a used value later, it becomes a nightmare to maintain.

The third option means having to keep track of both the int and the boolean that keeps track of the question whether it is used. If you're going to do this, consider making it a class in and of itself... but then you might as well use Integer.

like image 164
S.L. Barth Avatar answered Dec 08 '25 22:12

S.L. Barth