1 package org.apache.lucene.benchmark.byTask.utils;
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 import java.text.NumberFormat;
23 * Formatting utilities (for reports).
27 private static NumberFormat numFormat [] = {
28 NumberFormat.getInstance(),
29 NumberFormat.getInstance(),
30 NumberFormat.getInstance(),
32 private static final String padd = " ";
35 numFormat[0].setMaximumFractionDigits(0);
36 numFormat[0].setMinimumFractionDigits(0);
37 numFormat[1].setMaximumFractionDigits(1);
38 numFormat[1].setMinimumFractionDigits(1);
39 numFormat[2].setMaximumFractionDigits(2);
40 numFormat[2].setMinimumFractionDigits(2);
44 * Padd a number from left.
45 * @param numFracDigits number of digits in fraction part - must be 0 or 1 or 2.
46 * @param f number to be formatted.
47 * @param col column name (used for deciding on length).
48 * @return formatted string.
50 public static String format(int numFracDigits, float f, String col) {
51 String res = padd + numFormat[numFracDigits].format(f);
52 return res.substring(res.length() - col.length());
55 public static String format(int numFracDigits, double f, String col) {
56 String res = padd + numFormat[numFracDigits].format(f);
57 return res.substring(res.length() - col.length());
61 * Pad a number from right.
62 * @param numFracDigits number of digits in fraction part - must be 0 or 1 or 2.
63 * @param f number to be formatted.
64 * @param col column name (used for deciding on length).
65 * @return formatted string.
67 public static String formatPaddRight(int numFracDigits, float f, String col) {
68 String res = numFormat[numFracDigits].format(f) + padd;
69 return res.substring(0, col.length());
72 public static String formatPaddRight(int numFracDigits, double f, String col) {
73 String res = numFormat[numFracDigits].format(f) + padd;
74 return res.substring(0, col.length());
78 * Pad a number from left.
79 * @param n number to be formatted.
80 * @param col column name (used for deciding on length).
81 * @return formatted string.
83 public static String format(int n, String col) {
84 String res = padd + n;
85 return res.substring(res.length() - col.length());
89 * Pad a string from right.
90 * @param s string to be formatted.
91 * @param col column name (used for deciding on length).
92 * @return formatted string.
94 public static String format(String s, String col) {
95 String s1 = (s + padd);
96 return s1.substring(0, Math.min(col.length(), s1.length()));
100 * Pad a string from left.
101 * @param s string to be formatted.
102 * @param col column name (used for deciding on length).
103 * @return formatted string.
105 public static String formatPaddLeft(String s, String col) {
106 String res = padd + s;
107 return res.substring(res.length() - col.length());