コード例 #1
0
  /**
   * Get a table from PV
   *
   * <p>Ideally, the PV holds a {@link VTable}, and the returned data is then the table's data.
   *
   * <p>If the PV is a scalar, a table with a single cell is returned.
   *
   * <p>If the PV is an array, a table with one column is returned.
   *
   * @param value Value of a PV
   * @return List of rows, where each row contains either String or Number cells
   */
  @SuppressWarnings("rawtypes")
  public static List<List<Object>> getTable(final VType value) {
    final List<List<Object>> data = new ArrayList<>();
    if (value instanceof VTable) {
      final VTable table = (VTable) value;
      final int rows = table.getRowCount();
      final int cols = table.getColumnCount();
      // Extract 2D string matrix for data
      for (int r = 0; r < rows; ++r) {
        final List<Object> row = new ArrayList<>(cols);
        for (int c = 0; c < cols; ++c) {
          final Object col_data = table.getColumnData(c);
          if (col_data instanceof List) row.add(Objects.toString(((List) col_data).get(r)));
          else if (col_data instanceof ListDouble) row.add(((ListDouble) col_data).getDouble(r));
          else if (col_data instanceof ListNumber) row.add(((ListNumber) col_data).getLong(r));
          else row.add(Objects.toString(col_data));
        }
        data.add(row);
      }
    } else if (value instanceof VNumberArray) {
      final ListNumber numbers = ((VNumberArray) value).getData();
      final int num = numbers.size();
      for (int i = 0; i < num; ++i) data.add(Arrays.asList(numbers.getDouble(i)));
    } else if (value instanceof VNumber) data.add(Arrays.asList(((VNumber) value).getValue()));
    else data.add(Arrays.asList(Objects.toString(value)));

    return data;
  }
コード例 #2
0
  /**
   * Try to get a 'double' type array from a value.
   *
   * @param value Value of a PV
   * @return Current value as double[]. Will return single-element array for scalar value, including
   *     <code>{ Double.NaN }</code> in case the value type does not decode into a number.
   */
  public static double[] getDoubleArray(final VType value) {
    if (value instanceof VNumberArray) {
      final ListNumber list = ((VNumberArray) value).getData();
      final Object wrapped = CollectionNumbers.wrappedArray(list);
      if (wrapped instanceof double[]) return (double[]) wrapped;

      final double[] result = new double[list.size()];
      for (int i = 0; i < result.length; i++) result[i] = list.getDouble(i);
      return result;
    }
    return new double[] {getDouble(value)};
  }