Search in sources :

Example 1 with Display

use of org.yamcs.studio.data.vtype.Display in project yamcs-studio by yamcs.

the class VTypeHelper method formatScalarNumber.

/**
 * @param formatEnum
 * @param pmValue
 *            The PVManager V Value. It must be a scalar with number value.
 * @param numValue
 *            the number value. not necessary if pmValue is given.
 * @param precision
 * @return
 */
private static String formatScalarNumber(FormatEnum formatEnum, Object pmValue, Number numValue, int precision) {
    if (pmValue != null) {
        numValue = (Number) ((Scalar) pmValue).getValue();
    }
    NumberFormat numberFormat;
    var displayPrecision = calculatePrecision(pmValue, precision);
    double highDispLimit = 0.0, lowDispLimit = 0.0;
    switch(formatEnum) {
        case DECIMAL:
        case DEFAULT:
        default:
            if (precision == UNSET_PRECISION) {
                if (pmValue instanceof Display && ((Display) pmValue).getFormat() != null) {
                    if (numValue instanceof Float) {
                        // Use boxed float, to avoid the upcast to double
                        return ((Display) pmValue).getFormat().format(numValue);
                    } else if (numValue instanceof Byte || numValue instanceof Short || numValue instanceof Integer || numValue instanceof Long) {
                        return ((Display) pmValue).getFormat().format(numValue);
                    } else {
                        return ((Display) pmValue).getFormat().format(numValue.doubleValue());
                    }
                } else {
                    return formatScalarNumber(FormatEnum.COMPACT, numValue, displayPrecision);
                }
            } else {
                if (numValue instanceof Byte || numValue instanceof Short || numValue instanceof Integer || numValue instanceof Long) {
                    numberFormat = getDecimalFormat(precision);
                    return numberFormat.format(numValue);
                } else if (numValue instanceof Float) {
                    // consistent with the rest of CSS.
                    if (Float.isNaN(numValue.floatValue())) {
                        return Float.toString(Float.NaN);
                    }
                    // Also check for positive and negative infinity.
                    if (Float.isInfinite(numValue.floatValue())) {
                        return Float.toString(numValue.floatValue());
                    }
                    numberFormat = getDecimalFormat(precision);
                    // Use boxed Float, to avoid the upcast to double
                    return numberFormat.format(numValue);
                } else {
                    // consistent with the rest of CSS.
                    if (Double.isNaN(numValue.doubleValue())) {
                        return Double.toString(Double.NaN);
                    }
                    // Also check for positive and negative infinity.
                    if (Double.isInfinite(numValue.doubleValue())) {
                        return Double.toString(numValue.doubleValue());
                    }
                    numberFormat = getDecimalFormat(precision);
                    return numberFormat.format(numValue.doubleValue());
                }
            }
        case COMPACT:
            var dValue = numValue.doubleValue();
            if (((dValue > 0.0001) && (dValue < 10000)) || ((dValue < -0.0001) && (dValue > -10000)) || dValue == 0.0) {
                return formatScalarNumber(FormatEnum.DECIMAL, numValue, displayPrecision);
            } else {
                return formatScalarNumber(FormatEnum.EXP, numValue, displayPrecision);
            }
        case ENG:
            var value = numValue.doubleValue();
            if (value == 0) {
                return formatScalarNumber(FormatEnum.EXP, numValue, displayPrecision);
            }
            var log10 = Math.log10(Math.abs(value));
            var power = 3 * (int) Math.floor(log10 / 3);
            return String.format("%." + displayPrecision + "fE%d", value / Math.pow(10, power), power);
        case SEXA:
            if (pmValue instanceof Display) {
                highDispLimit = ((Display) pmValue).getUpperDisplayLimit();
                lowDispLimit = ((Display) pmValue).getLowerDisplayLimit();
            }
            return doubleToSexagesimal(numValue.doubleValue(), displayPrecision, highDispLimit, lowDispLimit);
        case SEXA_HMS:
            if (pmValue instanceof Display) {
                highDispLimit = ((Display) pmValue).getUpperDisplayLimit();
                lowDispLimit = ((Display) pmValue).getLowerDisplayLimit();
            }
            return doubleToSexagesimal(numValue.doubleValue() * 12.0 / Math.PI, displayPrecision, highDispLimit, lowDispLimit);
        case SEXA_DMS:
            if (pmValue instanceof Display) {
                highDispLimit = ((Display) pmValue).getUpperDisplayLimit();
                lowDispLimit = ((Display) pmValue).getLowerDisplayLimit();
            }
            return doubleToSexagesimal(numValue.doubleValue() * 180.0 / Math.PI, displayPrecision, highDispLimit, lowDispLimit);
        case EXP:
            // Exponential notation identified as 'negative' precision in cached
            numberFormat = getExponentialFormat(displayPrecision);
            return numberFormat.format(numValue.doubleValue());
        case HEX:
            return HEX_PREFIX + Integer.toHexString(numValue.intValue()).toUpperCase();
        case HEX64:
            return HEX_PREFIX + Long.toHexString(numValue.longValue());
        case STRING:
            return new String(new char[] { (char) numValue.intValue() });
    }
}
Also used : ListFloat(org.yamcs.studio.data.vtype.ListFloat) VFloat(org.yamcs.studio.data.vtype.VFloat) ListByte(org.yamcs.studio.data.vtype.ListByte) VByte(org.yamcs.studio.data.vtype.VByte) ListLong(org.yamcs.studio.data.vtype.ListLong) VString(org.yamcs.studio.data.vtype.VString) VShort(org.yamcs.studio.data.vtype.VShort) ListShort(org.yamcs.studio.data.vtype.ListShort) Scalar(org.yamcs.studio.data.vtype.Scalar) MultiScalar(org.yamcs.studio.data.vtype.MultiScalar) NumberFormat(java.text.NumberFormat) Display(org.yamcs.studio.data.vtype.Display)

Aggregations

NumberFormat (java.text.NumberFormat)1 Display (org.yamcs.studio.data.vtype.Display)1 ListByte (org.yamcs.studio.data.vtype.ListByte)1 ListFloat (org.yamcs.studio.data.vtype.ListFloat)1 ListLong (org.yamcs.studio.data.vtype.ListLong)1 ListShort (org.yamcs.studio.data.vtype.ListShort)1 MultiScalar (org.yamcs.studio.data.vtype.MultiScalar)1 Scalar (org.yamcs.studio.data.vtype.Scalar)1 VByte (org.yamcs.studio.data.vtype.VByte)1 VFloat (org.yamcs.studio.data.vtype.VFloat)1 VShort (org.yamcs.studio.data.vtype.VShort)1 VString (org.yamcs.studio.data.vtype.VString)1