Exemplo n.º 1
0
  /**
   * Used for building summary statistics
   *
   * @param type
   * @param value
   * @param isPeriod Defines whether the parameter is a time period
   * @param isSquare Defines whether the period is a squared period
   * @return
   */
  @SuppressWarnings({"unchecked", "rawtypes"})
  private String toString(DataType<?> type, double value, boolean isPeriod, boolean isSquare) {

    // Handle corner cases
    if (Double.isNaN(value)) {
      return "Not available";
    } else if (Double.isInfinite(value)) {
      if (value < 0) {
        return "-Infinity";
      } else {
        return "+Infinity";
      }
    }

    // Handle periods
    if (isPeriod) {

      // Init
      long SECONDS = 1000;
      long MINUTES = 60 * SECONDS;
      long HOURS = 60 * MINUTES;
      long DAYS = 24 * HOURS;
      long WEEKS = 7 * DAYS;

      // Square
      if (isSquare) {
        SECONDS *= SECONDS;
        MINUTES *= MINUTES;
        HOURS *= HOURS;
        DAYS *= DAYS;
        WEEKS *= WEEKS;
      }

      // Compute
      final int weeks = (int) (value / WEEKS);
      value = value % WEEKS;
      final int days = (int) (value / DAYS);
      value = value % DAYS;
      final int hours = (int) (value / HOURS);
      value = value % HOURS;
      final int minutes = (int) (value / MINUTES);
      value = value % MINUTES;
      final int seconds = (int) (value / SECONDS);
      value = value % SECONDS;
      final int milliseconds = (int) (value);

      // Convert
      StringBuilder builder = new StringBuilder();
      if (weeks != 0) builder.append(weeks).append(isSquare ? "w^2, " : "w, ");
      if (days != 0) builder.append(days).append(isSquare ? "d^2, " : "d, ");
      if (hours != 0) builder.append(hours).append(isSquare ? "h^2, " : "h, ");
      if (minutes != 0) builder.append(minutes).append(isSquare ? "m^2, " : "m, ");
      if (seconds != 0) builder.append(seconds).append(isSquare ? "s^2, " : "s, ");
      builder.append(milliseconds).append(isSquare ? "ms^2" : "ms");

      // Return
      return builder.toString();
    }

    // Handle data types
    if (type instanceof DataTypeWithRatioScale) {
      DataTypeWithRatioScale rType = (DataTypeWithRatioScale) type;
      return rType.format(rType.fromDouble(value));
    } else {
      return String.valueOf(value);
    }
  }