Formatter.java
| Index Score | ||
|---|---|---|
![]() |
![]() |
org.xnap.util |
![]() |
![]() |
XNap 3 |
View: Reasons, Metrics, Source Code
These are the metrics that contribute to the Enerjy Score for this file, ranked by impact. So the metrics listed at the top influence the score to a greater extent that the metrics listed at the bottom.
/*
* XNap - A P2P framework and client.
*
* See the file AUTHORS for copyright information.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.xnap.util;
// import xnap.net.ITransferContainer;
import java.text.DateFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.StringTokenizer;
/**
* Helper class providing static methods for formatting different number and
* date formats.
*/
public class Formatter
{
//--- Constant(s) ---
public static final int LEFT = 0;
public static final int CENTER = 1;
public static final int RIGHT = 2;
public static final String[] SIZES = { "B", "KB", "MB", "GB", "TB" };
//--- Data field(s) ---
//--- Constructor(s) ---
//--- Method(s) ---
/**
* Formats double as decimal number.
*
* @param number number to be formatted
* @param decimal the number of digits allowed in the fraction portion of
* a number
* @return formatted number as string
*/
public static String formatNumber(double number, int decimal)
{
NumberFormat n = NumberFormat.getNumberInstance();
if (decimal > 0) {
n.setMinimumFractionDigits(decimal);
n.setMaximumFractionDigits(decimal);
}
return n.format(number);
}
/**
* Formats a number without any digits in the fraction portion.
*
* @param number number to be formatted
* @return formatted number as string
*/
public static String formatNumber(double number)
{
return formatNumber(number, 0);
}
/**
* Returns <code>value</code> (byte) formatted as a file size.
* For example value=2048 returns "2 kb".
*
* @param size filesize to be formatted
* @return formatted number as string
*/
public static String formatSize(double size)
{
int i = 0;
for (; i < SIZES.length - 1 && size >= 1024; i++) {
size /= 1024;
}
return formatNumber(size, 1) + " " + SIZES[i];
}
/**
* Formats number of seconds in appropriate time unit
*
* @param i number of seconds
* @return formatted duration as string
*/
public static String formatLength(long i)
{
StringBuffer s = new StringBuffer();
long x = (i / 3600);
if (x > 0) {
s.append(x);
s.append(":");
}
x = (i % 3600) / 60;
if (x < 10) {
s.append("0");
}
s.append(x);
s.append(":");
x = (i % 60);
if (x < 10) {
s.append("0");
}
s.append(x);
return s.toString();
}
/**
* Formats date.
*/
public static String formatDate(Date date)
{
return DateFormat.getDateInstance().format(date);
}
/**
* Formats time from given milliseconds time value.
*
* @param time milliseconds since January 1, 1970, 00:00:00 GMT not to
* exceed the milliseconds representation for the year 8099. A negative
* number indicates the number of milliseconds before January 1, 1970,
* 00:00:00 GMT.
*/
public static String formatTime(long time)
{
return DateFormat.getDateInstance().format(new Date(time));
}
/**
* Formats a row of strings according to given alignments for the console
* output.
*
* @param rows array of strings to be formatted
* @param colAlignment array of alignment policies
*/
public static String formatTable(String[] rows, int[] colAlignment)
{
if (rows.length == 0) {
return "(list is empty)";
}
int[] widths = new int[colAlignment.length];
for (int i = 0; i < rows.length; i++) {
StringTokenizer t = new StringTokenizer(rows[i], "|");
for (int col = 0; t.hasMoreTokens(); col++) {
int l = t.nextToken().length();
if (l > widths[col]) {
widths[col] = l;
}
}
}
StringBuffer sb = new StringBuffer();
for (int i = 0; i < rows.length; i++) {
StringTokenizer t = new StringTokenizer(rows[i], "|");
for (int col = 0; col < colAlignment.length; col++) {
String s = (t.hasMoreTokens()) ? t.nextToken() : "";
int appendCount = widths[col] - s.length();
switch (colAlignment[col]) {
case LEFT:
sb.append(s);
sb.append(fill(" ", appendCount));
break;
case CENTER:
int a = appendCount / 2;
sb.append(fill(" ", a));
sb.append(s);
sb.append(fill(" ", appendCount - a));
break;
case RIGHT:
sb.append(fill(" ", appendCount));
sb.append(s);
break;
}
if (col < colAlignment.length - 1) {
sb.append(" | ");
}
}
sb.append("\n");
}
return sb.toString();
}
/**
* Returns <code>t</code> formatted as a logfile entry in CLF.
*
* Example: joe@server - - [Date] "mymusic.ogg" "finished" 434339 - "XNap"
*/
// public static String formatTransfer(ITransferContainer t)
// {
// return new String(t.getUser() + " - - " + "[" + getCLFDate() + "]"
// + " " + "\"" + t.getFilename() + "\"" + " " + "\""
// + t.getStatusText() + "\"" + " " + t.getFilesize()
// + " - " + "\"" + t.getUser().getClientInfo() + "\""
// + "\n");
// }
/**
* Creates a string from a string being <code>count</code> times appended
* to itself.
*
* @param s string
* @param count number of times it's appended to itself
*/
public static String fill(String s, int count)
{
StringBuffer sb = new StringBuffer(s.length() * count);
for (int i = 0; i < count; i++) {
sb.append(s);
}
return sb.toString();
}
/**
* Returns the current date in the format needed for CLF.
*
* Example: 15/Dec/2002:23:46:16
*/
public static String getCLFDate()
{
String pattern = "dd/MMM/yyyy:HH:mm:ss";
SimpleDateFormat df = new SimpleDateFormat(pattern);
return df.format(new Date());
}
/**
* Returns a string of the current date in short format.
*
* Example: 11.19.80
*/
public static String shortDate()
{
return DateFormat.getTimeInstance(DateFormat.SHORT).format(new Date());
}
}
The table below shows all metrics for Formatter.java.




