NumbersUtil cleanup - removed methods that were not used - parseLong, parseInt and parseFloat replaced with the commons-lang NumberUtils call - Test for the remaining methods
Signed-off-by: Laszlo Hornyak <laszlo.horn...@gmail.com> Signed-off-by: Hugo Trippaers <htrippa...@schubergphilis.com> Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/b44bc9db Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/b44bc9db Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/b44bc9db Branch: refs/heads/ui-restyle Commit: b44bc9db029b7f5417f162406d4f870b9dbb407c Parents: 4590813 Author: Laszlo Hornyak <laszlo.horn...@gmail.com> Authored: Fri Sep 27 23:43:49 2013 +0200 Committer: Hugo Trippaers <htrippa...@schubergphilis.com> Committed: Wed Oct 2 10:55:00 2013 +0200 ---------------------------------------------------------------------- utils/src/com/cloud/utils/NumbersUtil.java | 155 ++----------------- utils/test/com/cloud/utils/NumbersUtilTest.java | 24 ++- 2 files changed, 29 insertions(+), 150 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cloudstack/blob/b44bc9db/utils/src/com/cloud/utils/NumbersUtil.java ---------------------------------------------------------------------- diff --git a/utils/src/com/cloud/utils/NumbersUtil.java b/utils/src/com/cloud/utils/NumbersUtil.java index 035239a..6c7961e 100755 --- a/utils/src/com/cloud/utils/NumbersUtil.java +++ b/utils/src/com/cloud/utils/NumbersUtil.java @@ -23,135 +23,21 @@ import java.util.Date; import java.util.Formatter; import java.util.Locale; +import org.apache.commons.lang.math.NumberUtils; + import com.cloud.utils.exception.CloudRuntimeException; public class NumbersUtil { public static long parseLong(String s, long defaultValue) { - if (s == null) { - return defaultValue; - } - - try { - return Long.parseLong(s); - } catch (NumberFormatException e) { - return defaultValue; - } + return NumberUtils.toLong(s, defaultValue); } public static int parseInt(String s, int defaultValue) { - if (s == null) { - return defaultValue; - } - try { - return Integer.parseInt(s); - } catch (NumberFormatException e) { - return defaultValue; - } + return NumberUtils.toInt(s, defaultValue); } public static float parseFloat(String s, float defaultValue) { - if (s == null) { - return defaultValue; - } - try { - return Float.parseFloat(s); - } catch (NumberFormatException e) { - return defaultValue; - } - } - - /** - * Converts bytes to short on input. - */ - public static int bytesToShort(byte b[]) { - return (b[1] & 0xff) | ((b[0] << 8) & 0xff00); - } - - public static int bytesToShort(byte b[], int pos) { - return (b[pos + 1] & 0xff) | ((b[pos] << 8) & 0xff00); - } - - /** - * Converts bytes to long on input. - */ - public static int bytesToInt(byte b[]) { - return bytesToInt(b, 0); - } - - public static int bytesToInt(byte b[], int pos) { - int value = b[pos + 3] & 0xff; - value |= (b[pos + 2] << 8) & 0xff00; - value |= (b[pos + 1] << 16) & 0xff0000; - value |= (b[pos] << 24) & 0xff000000; - return value; - } - - /** - * Converts a short to a series of bytes for output. Must be 2 bytes long. - */ - public static byte[] shortToBytes(int n) { - byte b[] = new byte[2]; - b[1] = (byte)(n & 0xff); - b[0] = (byte)((n >> 8) & 0xff); - return b; - } - - public static char encodeByte(int b) { - if (b < 10) { - return (char)(b + '0'); - } else if (b < 36) { - return (char)(b - 10 + 'A'); - } else if (b < 62) { - return (char)(b - 36 + 'a'); - } else if (b == 62) { - return '('; - } else if (b == 63) { - return ')'; - } - return (char)255; - } - - public static int decodeByte(char b) { - if (b >= 'A' && b <= 'Z') { - return b + 10 - 'A'; - } else if (b >= 'a' && b <= 'z') { - return b + 36 - 'a'; - } else if (b >= '0' && b <= '9') { - return b - '0'; - } else if (b == ')') { - return 63; - } else if (b == '(') { - return 62; - } - return -1; - } - - /** - * Converts a long to a series of bytes for output. Must be 4 bytes long. - */ - public static byte[] intToBytes(int n) { - byte b[] = new byte[4]; - b[3] = (byte)(n & 0xff); - b[2] = (byte)((n >> 8) & 0xff); - b[1] = (byte)((n >> 16) & 0xff); - b[0] = (byte)((n >> 24) & 0xff); - return b; - } - - /** - * Sorry for the bad naming but the longToBytes is already taken. Returns an 8 byte long byte array. - **/ - public static byte[] longToBytes(long n) { - byte b[] = new byte[8]; - b[7] = (byte)(n & 0xff); - b[6] = (byte)((n >> 8) & 0xff); - b[5] = (byte)((n >> 16) & 0xff); - b[4] = (byte)((n >> 24) & 0xff); - b[3] = (byte)((n >> 32) & 0xff); - b[2] = (byte)((n >> 40) & 0xff); - b[1] = (byte)((n >> 48) & 0xff); - b[0] = (byte)((n >> 56) & 0xff); - return b; + return NumberUtils.toFloat(s, defaultValue); } /** @@ -162,19 +48,7 @@ public class NumbersUtil { } public static long bytesToLong(byte b[], int pos) { - ByteBuffer buf = ByteBuffer.wrap(b, pos, 8); - return buf.getLong(); - /* - * long value = b[pos + 7] & 0xff; - * value |= (b[pos + 6] << 8) & 0xff00; - * value |= (b[pos + 5] << 16) & 0xff0000; - * value |= (b[pos + 4] << 24) & 0xff000000; - * value |= (b[pos + 3] << 32) & 0xff00000000; - * value |= (b[pos + 2] << 40) & 0xff0000000000; - * value |= (b[pos + 1] << 48) & 0xff000000000000; - * value |= (b[pos + 0] << 56) & 0xff00000000000000; - * return value; - */ + return ByteBuffer.wrap(b, pos, 8).getLong(); } /** @@ -198,28 +72,22 @@ public class NumbersUtil { protected static final long TB = 1024 * GB; public static String toReadableSize(long bytes) { - if (bytes <= KB && bytes >= 0) { + if (bytes < KB && bytes >= 0) { return Long.toString(bytes) + " bytes"; } StringBuilder builder = new StringBuilder(); Formatter format = new Formatter(builder, Locale.getDefault()); if (bytes < MB) { format.format("%.2f KB", (float)bytes / (float)KB); - format.close(); - return builder.toString(); } else if (bytes < GB) { format.format("%.2f MB", (float)bytes / (float)MB); - format.close(); - return builder.toString(); } else if (bytes < TB) { format.format("%.2f GB", (float)bytes / (float)GB); - format.close(); - return builder.toString(); } else { format.format("%.4f TB", (float)bytes / (float)TB); - format.close(); - return builder.toString(); } + format.close(); + return builder.toString(); } /** @@ -261,9 +129,4 @@ public class NumbersUtil { public static int hash(long value) { return (int)(value ^ (value >>> 32)); } - - public static void main(String[] args) { - long interval = parseInterval(args[0], -1); - System.out.println(args[0] + " is " + interval); - } } http://git-wip-us.apache.org/repos/asf/cloudstack/blob/b44bc9db/utils/test/com/cloud/utils/NumbersUtilTest.java ---------------------------------------------------------------------- diff --git a/utils/test/com/cloud/utils/NumbersUtilTest.java b/utils/test/com/cloud/utils/NumbersUtilTest.java index f60f58a..6d77dbe 100644 --- a/utils/test/com/cloud/utils/NumbersUtilTest.java +++ b/utils/test/com/cloud/utils/NumbersUtilTest.java @@ -23,9 +23,25 @@ import org.junit.Test; public class NumbersUtilTest { @Test - public void formattingCheck() { - long size = 1024 * 1024 * 1024; - String formatted = NumbersUtil.toReadableSize(size); - assertEquals("1.00 GB", formatted); + public void toReadableSize() { + assertEquals("1.0000 TB", + NumbersUtil.toReadableSize((1024l * 1024l * 1024l * 1024l))); + assertEquals("1.00 GB", + NumbersUtil.toReadableSize((long) (1024 * 1024 * 1024))); + assertEquals("1.00 MB", + NumbersUtil.toReadableSize((long) (1024 * 1024))); + assertEquals("1.00 KB", NumbersUtil.toReadableSize((long) (1024))); + assertEquals("1023 bytes", NumbersUtil.toReadableSize((long) (1023))); } + + @Test + public void bytesToLong() { + assertEquals(0, + NumbersUtil.bytesToLong(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 })); + assertEquals(1, + NumbersUtil.bytesToLong(new byte[] { 0, 0, 0, 0, 0, 0, 0, 1 })); + assertEquals(257, + NumbersUtil.bytesToLong(new byte[] { 0, 0, 0, 0, 0, 0, 1, 1 })); + } + }