Ejemplo n.º 1
0
  /**
   * 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;
  }
Ejemplo n.º 2
0
  /**
   * Format just the value of a {@link VType} into a buffer (not timestamp, ..)
   *
   * @param value Value
   * @param buf {@link StringBuilder}
   * @return {@link StringBuilder}
   */
  public StringBuilder format(final VType value, final StringBuilder buf) {
    // After the time this is implemented, VEnum may change into a class
    // that also implements VNumber and/or VString.
    // Handle it first to assert that VEnum is always identified as such
    // and not handled as Number.
    if (value instanceof VEnum) {
      final VEnum enumerated = (VEnum) value;
      try {
        buf.append(enumerated.getValue());
        buf.append(" (").append(enumerated.getIndex()).append(")");
      } catch (ArrayIndexOutOfBoundsException ex) { // Error getting label for invalid index?
        buf.append("<enum ").append(enumerated.getIndex()).append(">");
      }
    } else if (value instanceof VNumber) {
      final VNumber number = (VNumber) value;
      final Display display = (Display) number;
      format(number.getValue().doubleValue(), display, buf);
    } else if (value instanceof VNumberArray) {
      final VNumberArray array = (VNumberArray) value;
      final Display display = (Display) array;
      final ListNumber list = array.getData();
      final int N = list.size();
      if (N <= MAX_ARRAY_ELEMENTS) {
        if (N > 0) format(list.getDouble(0), display, buf);
        for (int i = 1; i < N; ++i) {
          buf.append(", ");
          format(list.getDouble(i), display, buf);
        }
      } else {
        format(list.getDouble(0), display, buf);
        for (int i = 1; i < MAX_ARRAY_ELEMENTS / 2; ++i) {
          buf.append(", ");
          format(list.getDouble(i), display, buf);
        }
        buf.append(", ... (total ").append(N).append(" elements) ...");
        for (int i = N - MAX_ARRAY_ELEMENTS / 2; i < N; ++i) {
          buf.append(", ");
          format(list.getDouble(i), display, buf);
        }
      }
    } else if (value instanceof VStatistics) {
      final VStatistics stats = (VStatistics) value;
      final Display display = (Display) stats;
      format(stats.getAverage(), display, buf);
      buf.append(" [").append(stats.getMin()).append(" ... ").append(stats.getMax());
      buf.append(", ").append(stats.getNSamples()).append(" samples");
      final Double dev = stats.getStdDev();
      if (dev > 0) buf.append(", dev ").append(dev);
      buf.append("]");
    } else if (value instanceof VString) buf.append(((VString) value).getValue());
    else if (value == null) buf.append("null");
    else // TODO: VEnumArray, other types?
    buf.append(value.toString());

    return buf;
  }