Consequences of changing a primitive to a Boxed Primitive in a Java Application

StackOverflow https://stackoverflow.com/questions/4959477

  •  12-11-2019
  •  | 
  •  

Frage

I am working on an application in which all performance values for a given item are primitives. However, there is now the possibility that one of these values - Revenue - is not available when adding new data to the system. In these cases we are fine with simply storing/displaying the value for Revenue as 0.00

However, we now also want to be able to add this missing data at a later date (but not overwrite data if present). Rather than adding flags to indicate whether Revenue was actually available when 0.00 was added to the database, it seems obvious to me to change Revenue from the primitive type double to a boxed primitive Double, and allow NULLS for the Revenue column in the db.

There are various places in the application where getRevenue() is called however, and mathematic operations are performed upon the value. Obviously now having the potential for getRevenue() to return null could cause major problems. As I mentioned though, we are perfectly happy to present Revenue as having a value of 0.00 in the cases where there is no value present.

So, it seems like an obvious solution would be to update the getRevenue() method to return the value, or 0.00 in the case of the value being null. And to add a new method getRevenueDB() which returns the true value of Revenue, including null. This method would only be called when accessing the value to add to the db.

How does this seems as a solution? It shouold work, and is quick. But does this seem a horribly dirty solution, for which there is a better option available?

All comments are much appreciated!

Keine korrekte Lösung

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top