From b58c92ebb679ed55aa9328e93c594292fe79547e Mon Sep 17 00:00:00 2001 From: thatsIch Date: Tue, 7 Apr 2015 23:46:52 +0200 Subject: [PATCH] Fixes #1202: Now 10000x items is the limit before being converted to next bigger unit Added a new method to display a number in a long fashion. Added respective unit tests for them --- .../client/render/AppEngRenderItem.java | 2 +- .../appeng/util/ReadableNumberConverter.java | 31 +++- .../util/LongReadableNumberConverterTest.java | 138 ++++++++++++++++++ 3 files changed, 168 insertions(+), 3 deletions(-) create mode 100644 src/test/java/appeng/util/LongReadableNumberConverterTest.java diff --git a/src/main/java/appeng/client/render/AppEngRenderItem.java b/src/main/java/appeng/client/render/AppEngRenderItem.java index 66635d97..42383254 100644 --- a/src/main/java/appeng/client/render/AppEngRenderItem.java +++ b/src/main/java/appeng/client/render/AppEngRenderItem.java @@ -129,7 +129,7 @@ public class AppEngRenderItem extends RenderItem } else { - return NUMBER_CONVERTER.toHumanReadableForm( originalSize ); + return NUMBER_CONVERTER.toLongHumanReadableForm( originalSize ); } } } diff --git a/src/main/java/appeng/util/ReadableNumberConverter.java b/src/main/java/appeng/util/ReadableNumberConverter.java index fc3bbabe..6bda3b59 100644 --- a/src/main/java/appeng/util/ReadableNumberConverter.java +++ b/src/main/java/appeng/util/ReadableNumberConverter.java @@ -49,7 +49,7 @@ public enum ReadableNumberConverter final long absNumber = Math.abs( number ); if( absNumber < DIVISION_BASE ) - return Long.toString( absNumber ); + return Long.toString( number ); final int exp = (int) ( Math.log( absNumber ) / LOG_DIVISION_BASE ); final char postFix = ENCODED_POSTFIXES[exp - 1]; @@ -93,7 +93,7 @@ public enum ReadableNumberConverter final long absNumber = Math.abs( number ); if( absNumber < DIVISION_BASE ) - return Long.toString( absNumber ); + return Long.toString( number ); final int exp = (int) ( Math.log( absNumber ) / LOG_DIVISION_BASE ); final int result = (int) ( absNumber / Math.pow( DIVISION_BASE, exp ) ); @@ -111,4 +111,31 @@ public enum ReadableNumberConverter return String.format( "%s%d%s", sign, result, postFix ); } } + + /** + * Converts a number into a human readable form. It will not round the number, but floor it. + * Will try to cut the number down 1 decimal later. + * + * Example: + * 10000L -> 10K + * 9999L -> 9999 + * + * @param number to be converted number + * + * @return String in SI format cut down as far as possible + */ + public String toLongHumanReadableForm( long number ) + { + final String sign = this.getSign( number ); + final long absNumber = Math.abs( number ); + + if( absNumber < 10 * DIVISION_BASE ) + return Long.toString( number ); + + final int exp = (int) ( Math.log( absNumber / 10 ) / LOG_DIVISION_BASE ); + final int result = (int) ( absNumber / Math.pow( DIVISION_BASE, exp ) ); + final char postFix = ENCODED_POSTFIXES[exp - 1]; + + return String.format( "%s%d%s", sign, result, postFix ); + } } diff --git a/src/test/java/appeng/util/LongReadableNumberConverterTest.java b/src/test/java/appeng/util/LongReadableNumberConverterTest.java new file mode 100644 index 00000000..48a2038a --- /dev/null +++ b/src/test/java/appeng/util/LongReadableNumberConverterTest.java @@ -0,0 +1,138 @@ +package appeng.util; + + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + + +/** + * Tests for {@link appeng.util.ReadableNumberConverter} + * + * @author thatsIch + * @version rv2 + * @since rv2 + */ +public class LongReadableNumberConverterTest +{ + private static final long NUMBER_LOW = 999L; + private static final long NUMBER_NORMAL = 9999L; + private static final long NUMBER_BORDER = 10000L; + private static final long NUMBER_KILO = 155555L; + private static final long NUMBER_KILO_BORDER = 9999999L; + private static final long NUMBER_MEGA_STEP = 10000000; + private static final long NUMBER_MEGA = 155555555L; + private static final long NUMBER_GIGA = 155555555555L; + private static final long NUMBER_TERA = 155555555555555L; + private static final long NUMBER_PETA = 155555555555555555L; + private static final long NUMBER_EXA = 1555555555555555555L; + private static final long NUMBER_NEGATIVE_LOW = -999L; + private static final long NUMBER_NEGATIVE_NORMAL = -9999L; + private static final long NUMBER_NEGATIVE_GIGA = -155555555555L; + + private static final String RESULT_LONG_LOW = "999"; + private static final String RESULT_LONG_NORMAL = "9999"; + private static final String RESULT_LONG_BORDER = "10K"; + private static final String RESULT_LONG_KILO = "155K"; + private static final String RESULT_LONG_KILO_BORDER = "9999K"; + private static final String RESULT_LONG_MEGA_STEP = "10M"; + private static final String RESULT_LONG_MEGA = "155M"; + private static final String RESULT_LONG_GIGA = "155G"; + private static final String RESULT_LONG_TERA = "155T"; + private static final String RESULT_LONG_PETA = "155P"; + private static final String RESULT_LONG_EXA = "1555P"; + private static final String RESULT_LONG_NEGATIVE_LOW = "-999"; + private static final String RESULT_LONG_NEGATIVE_NORMAL = "-9999"; + private static final String RESULT_LONG_NEGATIVE_GIGA = "-155G"; + + private final ReadableNumberConverter converter; + + public LongReadableNumberConverterTest() + { + this.converter = ReadableNumberConverter.INSTANCE; + } + + @Test + public void testConvertLongNormal() + { + assertEquals( RESULT_LONG_NORMAL, this.converter.toLongHumanReadableForm( NUMBER_NORMAL ) ); + } + + @Test + public void testConvertLongKiloBorder() + { + assertEquals( RESULT_LONG_KILO_BORDER, this.converter.toLongHumanReadableForm( NUMBER_KILO_BORDER ) ); + } + + @Test + public void testConvertLongMegaStep() + { + assertEquals( RESULT_LONG_MEGA_STEP, this.converter.toLongHumanReadableForm( NUMBER_MEGA_STEP ) ); + } + + @Test + public void testConvertLongLow() + { + assertEquals( RESULT_LONG_LOW, this.converter.toLongHumanReadableForm( NUMBER_LOW ) ); + } + + @Test + public void testConvertLongBorder() + { + assertEquals( RESULT_LONG_BORDER, this.converter.toLongHumanReadableForm( NUMBER_BORDER ) ); + } + + @Test + public void testConvertLongKilo() + { + assertEquals( RESULT_LONG_KILO, this.converter.toLongHumanReadableForm( NUMBER_KILO ) ); + } + + @Test + public void testConvertLongMega() + { + assertEquals( RESULT_LONG_MEGA, this.converter.toLongHumanReadableForm( NUMBER_MEGA ) ); + } + + @Test + public void testConvertLongGiga() + { + assertEquals( RESULT_LONG_GIGA, this.converter.toLongHumanReadableForm( NUMBER_GIGA ) ); + } + + @Test + public void testConvertLongTera() + { + assertEquals( RESULT_LONG_TERA, this.converter.toLongHumanReadableForm( NUMBER_TERA ) ); + } + + @Test + public void testConvertLongPeta() + { + assertEquals( RESULT_LONG_PETA, this.converter.toLongHumanReadableForm( NUMBER_PETA ) ); + } + + @Test + public void testConvertLongExa() + { + assertEquals( RESULT_LONG_EXA, this.converter.toLongHumanReadableForm( NUMBER_EXA ) ); + } + + @Test + public void testConvertLongNegativeLow() + { + assertEquals( RESULT_LONG_NEGATIVE_LOW, this.converter.toLongHumanReadableForm( NUMBER_NEGATIVE_LOW ) ); + } + + @Test + public void testConvertLongNegativeNormal() + { + assertEquals( RESULT_LONG_NEGATIVE_NORMAL, this.converter.toLongHumanReadableForm( NUMBER_NEGATIVE_NORMAL ) ); + } + + @Test + public void testConvertLongNegativeGiga() + { + assertEquals( RESULT_LONG_NEGATIVE_GIGA, this.converter.toLongHumanReadableForm( NUMBER_NEGATIVE_GIGA ) ); + } +}