org.nuiton.jaxx.runtime.util.StringUtil Maven / Gradle / Ivy
package org.nuiton.jaxx.runtime.util;
/*-
* #%L
* JAXX :: Runtime
* %%
* Copyright (C) 2008 - 2017 Code Lutin, Ultreia.io
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* 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 Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* .
* #L%
*/
import java.text.MessageFormat;
import java.util.Locale;
/**
* Created by tchemit on 12/07/17.
*
* @author Tony Chemit - [email protected]
*/
public class StringUtil {
public static final String[] EMPTY_STRING_ARRAY = new String[0];
static final protected double[] timeFactors = {1000000, 1000, 60, 60, 24};
static final protected String[] timeUnites = {"ns", "ms", "s", "m", "h",
"d"};
/**
* Converts an time delay into a human readable format.
*
* @param value the delay to convert
* @return the memory representation of the given value
* @see #convert(long, double[], String[])
*/
public static String convertTime(long value) {
return convert(value, timeFactors, timeUnites);
}
/**
* Note: this method use the current locale
* (the {@link Locale#getDefault()}) in the method
* {@link MessageFormat#MessageFormat(String)}.
*
* @param value value to convert
* @param factors facotrs used form conversion
* @param unites libelle of unites to use
* @return the converted representation of the given value
*/
public static String convert(long value, double[] factors, String[] unites) {
long sign = value == 0 ? 1 : value / Math.abs(value);
int i = 0;
double tmp = Math.abs(value);
while (i < factors.length && i < unites.length && tmp > factors[i]) {
tmp = tmp / factors[i++];
}
tmp *= sign;
String result;
result = MessageFormat.format("{0,number,0.###}{1}", tmp,
unites[i]);
return result;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy