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;
}
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);
}
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) };
}
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);
}
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());
}
Aggregations