Search in sources :

Example 1 with ListInt

use of org.diirt.util.array.ListInt in project yamcs-studio by yamcs.

the class SimpleValueFormat method format.

/**
 * Formats a numeric array. This method can be overridden to change
 * the way numeric arrays are formatted.
 *
 * @param array the array to format
 * @param toAppendTo the buffer to append to
 * @param pos the position of the field
 * @return  the string buffer
 */
protected StringBuffer format(VNumberArray array, StringBuffer toAppendTo, FieldPosition pos) {
    NumberFormat f = nf(array);
    toAppendTo.append("[");
    boolean hasMore = false;
    ListNumber data = array.getData();
    if (data.size() > maxElements) {
        hasMore = true;
    }
    for (int i = 0; i < Math.min(data.size(), maxElements); i++) {
        if (i != 0) {
            toAppendTo.append(", ");
        }
        if (data instanceof ListByte || data instanceof ListShort || data instanceof ListInt || data instanceof ListLong) {
            toAppendTo.append(f.format(data.getLong(i)));
        } else {
            toAppendTo.append(f.format(data.getDouble(i)));
        }
    }
    if (hasMore) {
        toAppendTo.append(", ...");
    }
    toAppendTo.append("]");
    return toAppendTo;
}
Also used : ListNumber(org.diirt.util.array.ListNumber) ListByte(org.diirt.util.array.ListByte) ListInt(org.diirt.util.array.ListInt) ListLong(org.diirt.util.array.ListLong) ListShort(org.diirt.util.array.ListShort) NumberFormat(java.text.NumberFormat)

Example 2 with ListInt

use of org.diirt.util.array.ListInt in project yamcs-studio by yamcs.

the class VTableFactory method union.

public static VTable union(VString extraColumnName, final VStringArray extraColumnData, final VTable... tables) {
    // Prune nulls
    final List<String> extraColumnDataPruned = new ArrayList<>();
    final List<VTable> tablesPruned = new ArrayList<>();
    for (int i = 0; i < tables.length; i++) {
        VTable table = tables[i];
        if (table != null) {
            extraColumnDataPruned.add(extraColumnData.getData().get(i));
            tablesPruned.add(table);
        }
    }
    if (tablesPruned.isEmpty()) {
        return null;
    }
    List<String> columnNames = new ArrayList<>();
    List<Class<?>> columnTypes = new ArrayList<>();
    List<Map<String, VColumn>> tableColumns = new ArrayList<>();
    if (extraColumnName != null) {
        columnNames.add(extraColumnName.getValue());
        columnTypes.add(String.class);
    }
    int[] tableOffsets = new int[tablesPruned.size()];
    int currentOffset = 0;
    for (int k = 0; k < tablesPruned.size(); k++) {
        VTable table = tablesPruned.get(k);
        if (table == null) {
            continue;
        }
        tableOffsets[k] = currentOffset;
        currentOffset += table.getRowCount();
        tableColumns.add(VColumn.columnMap(table));
        for (int i = 0; i < table.getColumnCount(); i++) {
            String name = table.getColumnName(i);
            if (!columnNames.contains(name)) {
                columnNames.add(name);
                columnTypes.add(table.getColumnType(i));
            }
        }
    }
    final int rowCount = currentOffset;
    final ListInt offsets = new ArrayInt(tableOffsets);
    List<Object> columnData = new ArrayList<>();
    if (extraColumnName != null) {
        columnData.add(new AbstractList<String>() {

            @Override
            public String get(int index) {
                int nTable = ListNumbers.binarySearchValueOrLower(offsets, index);
                return extraColumnDataPruned.get(nTable);
            }

            @Override
            public int size() {
                return rowCount;
            }
        });
    }
    for (int i = 1; i < columnNames.size(); i++) {
        String columnName = columnNames.get(i);
        List<VColumn> columns = new ArrayList<>();
        for (int j = 0; j < tableColumns.size(); j++) {
            columns.add(tableColumns.get(j).get(columnName));
        }
        columnData.add(VColumn.combineData(columnTypes.get(i), rowCount, offsets, columns));
    }
    return ValueFactory.newVTable(columnTypes, columnNames, columnData);
}
Also used : ListInt(org.diirt.util.array.ListInt) ArrayList(java.util.ArrayList) VString(org.diirt.vtype.VString) VTable(org.diirt.vtype.VTable) ArrayInt(org.diirt.util.array.ArrayInt) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with ListInt

use of org.diirt.util.array.ListInt in project yamcs-studio by yamcs.

the class PVUtil method getStringArray.

/**
 * Get string array from pv.
 *
 * @param pv
 *            The PV.
 * @return String array. For string array, it's the actual strings. For numeric arrays, the numbers are formatted as
 *         strings. For enum array, the labels are returned. For scalar PVs, an array with a single string is
 *         returned.
 */
public static final String[] getStringArray(IPV pv) {
    final VType value = checkPVValue(pv);
    if (value instanceof VStringArray) {
        final List<String> list = ((VStringArray) value).getData();
        return list.toArray(new String[list.size()]);
    } else if (value instanceof VDoubleArray) {
        final ListNumber list = ((VNumberArray) value).getData();
        final String[] text = new String[list.size()];
        for (int i = 0; i < text.length; ++i) text[i] = Double.toString(list.getDouble(i));
        return text;
    } else if (value instanceof VNumberArray) {
        final ListNumber list = ((VNumberArray) value).getData();
        final String[] text = new String[list.size()];
        for (int i = 0; i < text.length; ++i) text[i] = Long.toString(list.getLong(i));
        return text;
    } else if (value instanceof VEnumArray) {
        final List<String> labels = ((VEnumArray) value).getLabels();
        final ListInt list = ((VEnumArray) value).getIndexes();
        final String[] text = new String[list.size()];
        for (int i = 0; i < text.length; ++i) {
            final int index = list.getInt(i);
            if (index >= 0 && index <= labels.size())
                text[i] = labels.get(index);
            else
                text[i] = "<" + index + ">";
        }
        return text;
    }
    return new String[] { getString(pv) };
}
Also used : VDoubleArray(org.diirt.vtype.VDoubleArray) VNumberArray(org.diirt.vtype.VNumberArray) VEnumArray(org.diirt.vtype.VEnumArray) ListNumber(org.diirt.util.array.ListNumber) ListInt(org.diirt.util.array.ListInt) VType(org.diirt.vtype.VType) List(java.util.List) VStringArray(org.diirt.vtype.VStringArray)

Example 4 with ListInt

use of org.diirt.util.array.ListInt in project yamcs-studio by yamcs.

the class ValueFactory method newVNumberArray.

/**
 * Creates a new VNumberArray based on the type of the data.
 *
 * @param data the array data
 * @param alarm the alarm
 * @param time the time
 * @param display the display
 * @return a new value
 */
public static VNumberArray newVNumberArray(final ListNumber data, final Alarm alarm, final Time time, final Display display) {
    ListInt sizes = new ArrayInt(data.size());
    List<ArrayDimensionDisplay> dimensionDisplay = ValueUtil.defaultArrayDisplay(sizes);
    return newVNumberArray(data, sizes, dimensionDisplay, alarm, time, display);
}
Also used : ListInt(org.diirt.util.array.ListInt) ArrayInt(org.diirt.util.array.ArrayInt)

Example 5 with ListInt

use of org.diirt.util.array.ListInt in project yamcs-studio by yamcs.

the class ColumnOfVTableFunction method calculate.

@Override
public Object calculate(final List<Object> args) {
    VTable table = (VTable) args.get(0);
    VString columnName = (VString) args.get(1);
    if (columnName == null || table == null) {
        return null;
    }
    int index = -1;
    for (int i = 0; i < table.getColumnCount(); i++) {
        if (Objects.equals(columnName.getValue(), table.getColumnName(i))) {
            index = i;
        }
    }
    if (index == -1) {
        throw new RuntimeException("Table does not contain column '" + columnName.getValue() + "'");
    }
    Class<?> type = table.getColumnType(index);
    if (String.class.isAssignableFrom(type)) {
        @SuppressWarnings("unchecked") List<String> data = (List<String>) table.getColumnData(index);
        return ValueFactory.newVStringArray(data, ValueFactory.alarmNone(), ValueFactory.timeNow());
    }
    if (Double.TYPE.isAssignableFrom(type)) {
        ListDouble data = (ListDouble) table.getColumnData(index);
        return ValueFactory.newVDoubleArray(data, ValueFactory.alarmNone(), ValueFactory.timeNow(), ValueFactory.displayNone());
    }
    if (Integer.TYPE.isAssignableFrom(type)) {
        ListInt data = (ListInt) table.getColumnData(index);
        return ValueFactory.newVIntArray(data, ValueFactory.alarmNone(), ValueFactory.timeNow(), ValueFactory.displayNone());
    }
    throw new RuntimeException("Unsupported type " + type.getSimpleName());
}
Also used : ListInt(org.diirt.util.array.ListInt) VString(org.diirt.vtype.VString) VTable(org.diirt.vtype.VTable) List(java.util.List) ListDouble(org.diirt.util.array.ListDouble) VString(org.diirt.vtype.VString)

Aggregations

ListInt (org.diirt.util.array.ListInt)8 ListNumber (org.diirt.util.array.ListNumber)5 VEnumArray (org.diirt.vtype.VEnumArray)4 VNumberArray (org.diirt.vtype.VNumberArray)4 VString (org.diirt.vtype.VString)4 List (java.util.List)3 ArrayList (java.util.ArrayList)2 Background (javafx.scene.layout.Background)2 BackgroundFill (javafx.scene.layout.BackgroundFill)2 ArrayInt (org.diirt.util.array.ArrayInt)2 VBoolean (org.diirt.vtype.VBoolean)2 VDoubleArray (org.diirt.vtype.VDoubleArray)2 VEnum (org.diirt.vtype.VEnum)2 VNumber (org.diirt.vtype.VNumber)2 VStringArray (org.diirt.vtype.VStringArray)2 VTable (org.diirt.vtype.VTable)2 NumberFormat (java.text.NumberFormat)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Rotate (javafx.scene.transform.Rotate)1