// Copyright 2012 Cloudera Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.cloudera.impala.common;

import static com.cloudera.impala.common.ByteUnits.GIGABYTE;
import static com.cloudera.impala.common.ByteUnits.KILOBYTE;
import static com.cloudera.impala.common.ByteUnits.MEGABYTE;
import static com.cloudera.impala.common.ByteUnits.PETABYTE;
import static com.cloudera.impala.common.ByteUnits.TERABYTE;

/**
 * Utility functions for pretty printing.
 */
public class PrintUtils {
  /**
   * Prints the given number of bytes in PB, TB, GB, MB, KB with 2 decimal points.
   * For example 5000 will be returned as 4.88KB.
   */
  public static String printBytes(long bytes) {
    double result = bytes;
    if (bytes >= PETABYTE) return String.format("%.2f", result / PETABYTE) + "PB";
    if (bytes >= TERABYTE) return String.format("%.2f", result / TERABYTE) + "TB";
    if (bytes >= GIGABYTE) return String.format("%.2f", result / GIGABYTE) + "GB";
    if (bytes >= MEGABYTE) return String.format("%.2f", result / MEGABYTE) + "MB";
    if (bytes >= KILOBYTE) return String.format("%.2f", result / KILOBYTE) + "KB";
    return bytes + "B";
  }

  public static String printCardinality(String prefix, long cardinality) {
    return prefix + "cardinality=" +
        ((cardinality != -1) ? String.valueOf(cardinality) : "unavailable");
  }

  public static String printHosts(String prefix, long numHosts) {
    return prefix + "hosts=" + ((numHosts != -1) ? numHosts : "unavailable");
  }

  public static String printMemCost(String prefix, long perHostMemCost) {
    return prefix + "per-host-mem=" +
        ((perHostMemCost != -1) ? printBytes(perHostMemCost) : "unavailable");
  }

  /**
   * Prints the given square matrix into matrixStr. Separates cells by cellSpacing.
   */
  public static void printMatrix(boolean[][] matrix, int cellSpacing,
      StringBuilder matrixStr) {
    // Print labels.
    for (int i = 0; i < cellSpacing; ++i) {
      matrixStr.append(" ");
    }
    String formatStr = "%Xd".replace("X", String.valueOf(cellSpacing));
    for (int i = 0; i < matrix.length; ++i) {
      matrixStr.append(String.format(formatStr, i));
    }
    matrixStr.append("\n");

    // Print matrix.
    for (int i = 0; i < matrix.length; ++i) {
      matrixStr.append(String.format(formatStr, i));
      for (int j = 0; j < matrix.length; ++j) {
        int cell = (matrix[i][j]) ? 1 : 0;
        matrixStr.append(String.format(formatStr, cell));
      }
      matrixStr.append("\n");
    }
  }
}

@@ -923,16 +923,49 @@
 ABYTE;%0A%0A
+import java.text.DecimalFormat;%0A%0A
 /**%0A * U
@@ -1260,60 +1260,156 @@
     
-if (bytes %3E= PETABYTE) return String.format(%22%25.2f%22, 
+// Avoid String.format() due to IMPALA-1572 which happens on JDK7 but not JDK6.%0A    if (bytes %3E= PETABYTE) return new DecimalFormat(%22.00PB%22).format(
 resu
@@ -1426,15 +1426,8 @@
 YTE)
- + %22PB%22
 ;%0A  
@@ -1458,38 +1458,50 @@
  return 
-String.format(%22%25.2f%22, 
+new DecimalFormat(%22.00TB%22).format(
 result /
@@ -1514,15 +1514,8 @@
 YTE)
- + %22TB%22
 ;%0A  
@@ -1546,38 +1546,50 @@
  return 
-String.format(%22%25.2f%22, 
+new DecimalFormat(%22.00GB%22).format(
 result /
@@ -1602,15 +1602,8 @@
 YTE)
- + %22GB%22
 ;%0A  
@@ -1634,38 +1634,50 @@
  return 
-String.format(%22%25.2f%22, 
+new DecimalFormat(%22.00MB%22).format(
 result /
@@ -1690,15 +1690,8 @@
 YTE)
- + %22MB%22
 ;%0A  
@@ -1726,30 +1726,42 @@
 urn 
-String.format(%22%25.2f%22, 
+new DecimalFormat(%22.00KB%22).format(
 resu
@@ -1778,15 +1778,8 @@
 YTE)
- + %22KB%22
 ;%0A  

Use DecimalFormat instead of String.format() in PrintUtils.