/*******************************************************************************
* This file is part of OpenNMS(R).
*
* Copyright (C) 2007-2011 The OpenNMS Group, Inc.
* OpenNMS(R) is Copyright (C) 1999-2011 The OpenNMS Group, Inc.
*
* OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.
*
* OpenNMS(R) 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, either version 3 of the License,
* or (at your option) any later version.
*
* OpenNMS(R) 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 OpenNMS(R). If not, see:
* http://www.gnu.org/licenses/
*
* For more information contact:
* OpenNMS(R) Licensing <license@opennms.org>
* http://www.opennms.org/
* http://www.opennms.com/
*******************************************************************************/
package org.opennms.core.utils;
import java.text.DecimalFormat;
/**
* Utilities for manipulating values in SI units. This is the
* International System of Units that you learned in science class.
*
* @author djgregor
* @link http://www.physics.nist.gov/cuu/Units/
*/
public abstract class SIUtils {
/**
* Always print at least one digit after the decimal point,
* and at most three digits after the decimal point.
*/
private static final DecimalFormat s_oneDigitAfterDecimal = new DecimalFormat("0.0##");
/**
* Print no digits after the decimal point (heh, nor a decimal point).
*/
private static final DecimalFormat s_noDigitsAfterDecimal = new DecimalFormat("0");
/**
* Method used to convert an integer bits-per-second value to a more
* readable vale using commonly recognized abbreviation for network
* interface speeds. Feel free to expand it as necessary to accomodate
* different values.
*
* @param ifSpeed
* The bits-per-second value to be converted into a string
* description
* @return A string representation of the speed ("100 Mbps" for
* example)
*/
public static String getHumanReadableIfSpeed(long ifSpeed) {
DecimalFormat formatter;
double displaySpeed;
String units;
if (ifSpeed >= 1000000000L) {
if ((ifSpeed % 1000000000L) == 0) {
formatter = s_noDigitsAfterDecimal;
} else {
formatter = s_oneDigitAfterDecimal;
}
displaySpeed = ((double) ifSpeed) / 1000000000;
units = "Gbps";
} else if (ifSpeed >= 1000000L) {
if ((ifSpeed % 1000000L) == 0) {
formatter = s_noDigitsAfterDecimal;
} else {
formatter = s_oneDigitAfterDecimal;
}
displaySpeed = ((double) ifSpeed) / 1000000;
units = "Mbps";
} else if (ifSpeed >= 1000L) {
if ((ifSpeed % 1000L) == 0) {
formatter = s_noDigitsAfterDecimal;
} else {
formatter = s_oneDigitAfterDecimal;
}
displaySpeed = ((double) ifSpeed) / 1000;
units = "kbps";
} else {
formatter = s_noDigitsAfterDecimal;
displaySpeed = (double) ifSpeed;
units = "bps";
}
return formatter.format(displaySpeed) + " " + units;
}
}