package com.onaro.sanscreen.client.view.tabular.value; import com.onaro.commons.util.Unit; import com.onaro.sanscreen.util.Constant; /** * This class assumes that the values passed in are in MB. It exists because we want the value converted to GB when displaying an * individual cell but want to get at the MB value for the summarizer so we don't accumulate rounding errors. * */ public class CapacityValue extends NumberValue { private static final long serialVersionUID = 1L; public static final long UNKNOWN = Constant.DEFAULT_CAPACITY_MB.longValue(); private final Unit srcUnits; private final Unit displayUnits; // If true then show 0, otherwise return null so we don't show anything private final boolean showZero; public CapacityValue(double number, Unit srcUnits, Unit displayUnits) { this(number, null, srcUnits, displayUnits, null, FORMAT_DEFAULT); } public CapacityValue(double number, String identifier, Unit srcUnits, Unit displayUnits) { this(number, identifier, srcUnits, displayUnits, null, FORMAT_DEFAULT); } public CapacityValue(double number, Unit srcUnits, Unit displayUnits, boolean showZero) { this(number, null, srcUnits, displayUnits, showZero); } public CapacityValue(double number, String identifier, Unit srcUnits, Unit displayUnits, boolean showZero) { this(number, identifier, srcUnits, displayUnits, FORMAT_DEFAULT, showZero); } public CapacityValue(double number, String identifier, Unit srcUnits, Unit displayUnits, String format, boolean showZero) { this(number, identifier, srcUnits, displayUnits, showZero, null, format, null); } public CapacityValue(double number, String identifier, Unit srcUnits, Unit displayUnits, String unitsText, String format) { this(number, identifier, srcUnits, displayUnits, true, unitsText, format, null); } public CapacityValue(double number, String identifier, Unit srcUnits, Unit displayUnits, boolean showZero, String unitsText, String format, String valueDecoration) { super(number, unitsText, identifier, format, valueDecoration); this.srcUnits = srcUnits; this.displayUnits = displayUnits; this.showZero = showZero; } /** * * @return true if the capacity value units are different than the display units. */ public boolean isDisplayConversionRequired() { return(srcUnits != displayUnits); } /** * * @return the source units. */ public Unit getSourceUnits() { return(srcUnits); } /** * * @return the display units */ public Unit getDisplayUnits() { return(displayUnits); } /** * * @return true if we are going to convert 0 values to "0". False indicates we will return null when asked to format. */ public boolean showZero() { return(showZero); } @Override public double getNumber() { double quantity = super.getNumber(); if(isDisplayConversionRequired()) { return(Unit.convert(quantity, srcUnits, displayUnits)); } else { return quantity; } } /** * Returns a Number object for the value (converting the value first if convertToGB is true) or null if the value is 0 * and showZero is false. */ @Override public Number getNumberObject() { Number result; double oldNumber = number; number = getNumber();// Convert if necessary // Server passes us -1 when they can't get capacity. We want to return null if (number == UNKNOWN || (!showZero && number == 0.0)) { result = null; } else { result = super.getNumberObject(); } number = oldNumber;// Restore old value return (result); } /** * * @return the raw value (not converted if displayUnits are different than srcUnits). */ public double getRawNumber() { return(super.getNumber()); } }