001: package org.apache.lucene.benchmark.byTask.utils;
002:
003: /**
004: * Licensed to the Apache Software Foundation (ASF) under one or more
005: * contributor license agreements. See the NOTICE file distributed with
006: * this work for additional information regarding copyright ownership.
007: * The ASF licenses this file to You under the Apache License, Version 2.0
008: * (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: */
019:
020: import java.text.NumberFormat;
021:
022: /**
023: * Formatting utilities (for reports).
024: */
025: public class Format {
026:
027: private static NumberFormat numFormat[] = {
028: NumberFormat.getInstance(), NumberFormat.getInstance(),
029: NumberFormat.getInstance(), };
030: private static final String padd = " ";
031:
032: static {
033: numFormat[0].setMaximumFractionDigits(0);
034: numFormat[0].setMinimumFractionDigits(0);
035: numFormat[1].setMaximumFractionDigits(1);
036: numFormat[1].setMinimumFractionDigits(1);
037: numFormat[2].setMaximumFractionDigits(2);
038: numFormat[2].setMinimumFractionDigits(2);
039: }
040:
041: /**
042: * Padd a number from left.
043: * @param numFracDigits number of digits in fraction part - must be 0 or 1 or 2.
044: * @param f number to be formatted.
045: * @param col column name (used for deciding on length).
046: * @return formatted string.
047: */
048: public static String format(int numFracDigits, float f, String col) {
049: String res = padd + numFormat[numFracDigits].format(f);
050: return res.substring(res.length() - col.length());
051: }
052:
053: public static String format(int numFracDigits, double f, String col) {
054: String res = padd + numFormat[numFracDigits].format(f);
055: return res.substring(res.length() - col.length());
056: }
057:
058: /**
059: * Padd a number from right.
060: * @param numFracDigits number of digits in fraction part - must be 0 or 1 or 2.
061: * @param f number to be formatted.
062: * @param col column name (used for deciding on length).
063: * @return formatted string.
064: */
065: public static String formatPaddRight(int numFracDigits, float f,
066: String col) {
067: String res = numFormat[numFracDigits].format(f) + padd;
068: return res.substring(0, col.length());
069: }
070:
071: public static String formatPaddRight(int numFracDigits, double f,
072: String col) {
073: String res = numFormat[numFracDigits].format(f) + padd;
074: return res.substring(0, col.length());
075: }
076:
077: /**
078: * Padd a number from left.
079: * @param n number to be formatted.
080: * @param col column name (used for deciding on length).
081: * @return formatted string.
082: */
083: public static String format(int n, String col) {
084: String res = padd + n;
085: return res.substring(res.length() - col.length());
086: }
087:
088: /**
089: * Padd a string from right.
090: * @param s string to be formatted.
091: * @param col column name (used for deciding on length).
092: * @return formatted string.
093: */
094: public static String format(String s, String col) {
095: String s1 = (s + padd);
096: return s1.substring(0, Math.min(col.length(), s1.length()));
097: }
098:
099: /**
100: * Padd 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.
104: */
105: public static String formatPaddLeft(String s, String col) {
106: String res = padd + s;
107: return res.substring(res.length() - col.length());
108: }
109:
110: /**
111: * Extract simple class name
112: * @param cls class whose simple name is required
113: * @return simple class name
114: */
115: public static String simpleName(Class cls) {
116: String c = cls.getName();
117: String p = cls.getPackage().getName();
118: int k = c.lastIndexOf(p + ".");
119: if (k < 0) {
120: return c;
121: }
122: return c.substring(k + 1 + p.length());
123: }
124:
125: }
|