From c9d67e40af8005712f80b0584eabdae843aa8c0e Mon Sep 17 00:00:00 2001 From: thatsIch Date: Sun, 29 Mar 2015 15:21:24 +0200 Subject: [PATCH] Using a unified number converter to display short versions of numbers --- .../client/render/AppEngRenderItem.java | 122 +++++---------- .../blocks/RenderBlockCraftingCPUMonitor.java | 20 +-- .../parts/reporting/PartStorageMonitor.java | 78 +++++----- .../appeng/util/ReadableNumberConverter.java | 114 ++++++++++++++ .../util/ReadableNumberConverterTest.java | 147 ++++++++++++++++++ 5 files changed, 346 insertions(+), 135 deletions(-) create mode 100644 src/main/java/appeng/util/ReadableNumberConverter.java create mode 100644 src/test/java/appeng/util/ReadableNumberConverterTest.java diff --git a/src/main/java/appeng/client/render/AppEngRenderItem.java b/src/main/java/appeng/client/render/AppEngRenderItem.java index cfbd37c7..d4900da1 100644 --- a/src/main/java/appeng/client/render/AppEngRenderItem.java +++ b/src/main/java/appeng/client/render/AppEngRenderItem.java @@ -29,42 +29,28 @@ import net.minecraft.item.ItemStack; import appeng.api.storage.data.IAEItemStack; import appeng.core.AEConfig; +import appeng.util.ReadableNumberConverter; + public class AppEngRenderItem extends RenderItem { + private static final ReadableNumberConverter NUMBER_CONVERTER = ReadableNumberConverter.INSTANCE; public IAEItemStack aeStack; - private void renderQuad(Tessellator par1Tessellator, int par2, int par3, int par4, int par5, int par6) - { - par1Tessellator.startDrawingQuads(); - par1Tessellator.setColorOpaque_I( par6 ); - par1Tessellator.addVertex( par2, par3, 0.0D ); - par1Tessellator.addVertex( par2, par3 + par5, 0.0D ); - par1Tessellator.addVertex( par2 + par4, par3 + par5, 0.0D ); - par1Tessellator.addVertex( par2 + par4, par3, 0.0D ); - par1Tessellator.draw(); - } - @Override - public void renderItemOverlayIntoGUI(FontRenderer par1FontRenderer, TextureManager par2RenderEngine, ItemStack par3ItemStack, int par4, int par5) + public void renderItemOverlayIntoGUI( FontRenderer fontRenderer, TextureManager textureManager, ItemStack is, int par4, int par5, String par6Str ) { - this.renderItemOverlayIntoGUI( par1FontRenderer, par2RenderEngine, par3ItemStack, par4, par5, null ); - } - - @Override - public void renderItemOverlayIntoGUI(FontRenderer par1FontRenderer, TextureManager par2RenderEngine, ItemStack is, int par4, int par5, String par6Str) - { - if ( is != null ) + if( is != null ) { float scaleFactor = AEConfig.instance.useTerminalUseLargeFont() ? 0.85f : 0.5f; float inverseScaleFactor = 1.0f / scaleFactor; int offset = AEConfig.instance.useTerminalUseLargeFont() ? 0 : -1; - boolean unicodeFlag = par1FontRenderer.getUnicodeFlag(); - par1FontRenderer.setUnicodeFlag( false ); + boolean unicodeFlag = fontRenderer.getUnicodeFlag(); + fontRenderer.setUnicodeFlag( false ); - if ( is.getItem().showDurabilityBar( is ) ) + if( is.getItem().showDurabilityBar( is ) ) { double health = is.getItem().getDurabilityForDisplay( is ); int j1 = (int) Math.round( 13.0D - health * 13.0D ); @@ -76,7 +62,7 @@ public class AppEngRenderItem extends RenderItem GL11.glDisable( GL11.GL_BLEND ); Tessellator tessellator = Tessellator.instance; int l = 255 - k << 16 | k << 8; - int i1 = (255 - k) / 4 << 16 | 16128; + int i1 = ( 255 - k ) / 4 << 16 | 16128; this.renderQuad( tessellator, par4 + 2, par5 + 13, 13, 2, 0 ); this.renderQuad( tessellator, par4 + 2, par5 + 13, 12, 1, i1 ); this.renderQuad( tessellator, par4 + 2, par5 + 13, j1, 1, l ); @@ -87,84 +73,62 @@ public class AppEngRenderItem extends RenderItem GL11.glColor4f( 1.0F, 1.0F, 1.0F, 1.0F ); } - if ( is.stackSize == 0 ) + if( is.stackSize == 0 ) { - String var6 = AEConfig.instance.useTerminalUseLargeFont() ? "+" : "Craft"; + String craftLabelText = AEConfig.instance.useTerminalUseLargeFont() ? "+" : "Craft"; GL11.glDisable( GL11.GL_LIGHTING ); GL11.glDisable( GL11.GL_DEPTH_TEST ); GL11.glPushMatrix(); GL11.glScaled( scaleFactor, scaleFactor, scaleFactor ); - int X = (int) (((float) par4 + offset + 16.0f - par1FontRenderer.getStringWidth( var6 ) * scaleFactor) * inverseScaleFactor); - int Y = (int) (((float) par5 + offset + 16.0f - 7.0f * scaleFactor) * inverseScaleFactor); - par1FontRenderer.drawStringWithShadow( var6, X, Y, 16777215 ); + int X = (int) ( ( (float) par4 + offset + 16.0f - fontRenderer.getStringWidth( craftLabelText ) * scaleFactor ) * inverseScaleFactor ); + int Y = (int) ( ( (float) par5 + offset + 16.0f - 7.0f * scaleFactor ) * inverseScaleFactor ); + fontRenderer.drawStringWithShadow( craftLabelText, X, Y, 16777215 ); GL11.glPopMatrix(); GL11.glEnable( GL11.GL_LIGHTING ); GL11.glEnable( GL11.GL_DEPTH_TEST ); } long amount = this.aeStack != null ? this.aeStack.getStackSize() : is.stackSize; - amount = Math.max( amount, 999999999999L ); - - if ( amount != 0 ) + if( amount != 0 ) { - String var6 = String.valueOf( Math.abs( amount ) ); - - if ( AEConfig.instance.useTerminalUseLargeFont() ) - { - if ( amount > 999999999 ) - { - var6 = String.valueOf( ( int ) Math.floor( amount / 1000000000.0 ) ) + 'B'; - } - else if ( amount > 99999999 ) - { - var6 = "." + (int) Math.floor( amount / 100000000.0 ) + 'B'; - } - else if ( amount > 999999 ) - { - var6 = String.valueOf( ( int ) Math.floor( amount / 1000000.0 ) ) + 'M'; - } - else if ( amount > 99999 ) - { - var6 = "." + (int) Math.floor( amount / 100000.0 ) + 'M'; - } - else if ( amount > 999 ) - { - var6 = String.valueOf( ( int ) Math.floor( amount / 1000.0 ) ) + 'K'; - } - } - else - { - if ( amount > 999999999 ) - { - var6 = String.valueOf( ( int ) Math.floor( amount / 1000000000.0 ) ) + 'B'; - } - else if ( amount > 999999999 ) - { - var6 = String.valueOf( ( int ) Math.floor( amount / 1000000000.0 ) ) + 'B'; - } - else if ( amount > 999999 ) - { - var6 = String.valueOf( ( int ) Math.floor( amount / 1000000.0 ) ) + 'M'; - } - else if ( amount > 9999 ) - { - var6 = String.valueOf( ( int ) Math.floor( amount / 1000.0 ) ) + 'K'; - } - } + final String stackSize = this.getToBeRenderedStackSize( amount ); GL11.glDisable( GL11.GL_LIGHTING ); GL11.glDisable( GL11.GL_DEPTH_TEST ); GL11.glPushMatrix(); GL11.glScaled( scaleFactor, scaleFactor, scaleFactor ); - int X = (int) (((float) par4 + offset + 16.0f - par1FontRenderer.getStringWidth( var6 ) * scaleFactor) * inverseScaleFactor); - int Y = (int) (((float) par5 + offset + 16.0f - 7.0f * scaleFactor) * inverseScaleFactor); - par1FontRenderer.drawStringWithShadow( var6, X, Y, 16777215 ); + int X = (int) ( ( (float) par4 + offset + 16.0f - fontRenderer.getStringWidth( stackSize ) * scaleFactor ) * inverseScaleFactor ); + int Y = (int) ( ( (float) par5 + offset + 16.0f - 7.0f * scaleFactor ) * inverseScaleFactor ); + fontRenderer.drawStringWithShadow( stackSize, X, Y, 16777215 ); GL11.glPopMatrix(); GL11.glEnable( GL11.GL_LIGHTING ); GL11.glEnable( GL11.GL_DEPTH_TEST ); } - par1FontRenderer.setUnicodeFlag( unicodeFlag ); + fontRenderer.setUnicodeFlag( unicodeFlag ); } } + + private String getToBeRenderedStackSize( long originalSize ) + { + if( AEConfig.instance.useTerminalUseLargeFont() ) + { + return NUMBER_CONVERTER.toShortHumanReadableForm( originalSize ); + } + else + { + return NUMBER_CONVERTER.toHumanReadableForm( originalSize ); + } + } + + private void renderQuad( Tessellator par1Tessellator, int par2, int par3, int par4, int par5, int par6 ) + { + par1Tessellator.startDrawingQuads(); + par1Tessellator.setColorOpaque_I( par6 ); + par1Tessellator.addVertex( par2, par3, 0.0D ); + par1Tessellator.addVertex( par2, par3 + par5, 0.0D ); + par1Tessellator.addVertex( par2 + par4, par3 + par5, 0.0D ); + par1Tessellator.addVertex( par2 + par4, par3, 0.0D ); + par1Tessellator.draw(); + } } diff --git a/src/main/java/appeng/client/render/blocks/RenderBlockCraftingCPUMonitor.java b/src/main/java/appeng/client/render/blocks/RenderBlockCraftingCPUMonitor.java index 8f63322a..301b516d 100644 --- a/src/main/java/appeng/client/render/blocks/RenderBlockCraftingCPUMonitor.java +++ b/src/main/java/appeng/client/render/blocks/RenderBlockCraftingCPUMonitor.java @@ -38,9 +38,12 @@ import appeng.core.AELog; import appeng.tile.AEBaseTile; import appeng.tile.crafting.TileCraftingMonitorTile; import appeng.util.Platform; +import appeng.util.ReadableNumberConverter; + public class RenderBlockCraftingCPUMonitor extends RenderBlockCraftingCPU { + private static final ReadableNumberConverter NUMBER_CONVERTER = ReadableNumberConverter.INSTANCE; public RenderBlockCraftingCPUMonitor() { super( true, 20 ); @@ -173,22 +176,13 @@ public class RenderBlockCraftingCPUMonitor extends RenderBlockCraftingCPU GL11.glTranslatef( 0.0f, 0.14f, -0.24f ); GL11.glScalef( 1.0f / 62.0f, 1.0f / 62.0f, 1.0f / 62.0f ); - long qty = ais.getStackSize(); - if ( qty > 999999999999L ) - qty = 999999999999L; - - String msg = Long.toString( qty ); - if ( qty > 1000000000 ) - msg = Long.toString( qty / 1000000000 ) + 'B'; - else if ( qty > 1000000 ) - msg = Long.toString( qty / 1000000 ) + 'M'; - else if ( qty > 9999 ) - msg = Long.toString( qty / 1000 ) + 'K'; + final long stackSize = ais.getStackSize(); + final String renderedStackSize = NUMBER_CONVERTER.toHumanReadableForm( stackSize ); FontRenderer fr = Minecraft.getMinecraft().fontRenderer; - int width = fr.getStringWidth( msg ); + int width = fr.getStringWidth( renderedStackSize ); GL11.glTranslatef( -0.5f * width, 0.0f, -1.0f ); - fr.drawString( msg, 0, 0, 0 ); + fr.drawString( renderedStackSize, 0, 0, 0 ); GL11.glPopAttrib(); } diff --git a/src/main/java/appeng/parts/reporting/PartStorageMonitor.java b/src/main/java/appeng/parts/reporting/PartStorageMonitor.java index 8a17b45b..28cf98a9 100644 --- a/src/main/java/appeng/parts/reporting/PartStorageMonitor.java +++ b/src/main/java/appeng/parts/reporting/PartStorageMonitor.java @@ -60,12 +60,13 @@ import appeng.core.localization.PlayerMessages; import appeng.helpers.Reflected; import appeng.me.GridAccessException; import appeng.util.Platform; +import appeng.util.ReadableNumberConverter; import appeng.util.item.AEItemStack; public class PartStorageMonitor extends PartMonitor implements IPartStorageMonitor, IStackWatcherHost { - + private static final ReadableNumberConverter NUMBER_CONVERTER = ReadableNumberConverter.INSTANCE; IAEItemStack configuredItem; boolean isLocked; IStackWatcher myWatcher; @@ -93,7 +94,7 @@ public class PartStorageMonitor extends PartMonitor implements IPartStorageMonit data.setBoolean( "isLocked", this.isLocked ); NBTTagCompound myItem = new NBTTagCompound(); - if ( this.configuredItem != null ) + if( this.configuredItem != null ) this.configuredItem.writeToNBT( myItem ); data.setTag( "configuredItem", myItem ); @@ -113,24 +114,24 @@ public class PartStorageMonitor extends PartMonitor implements IPartStorageMonit @Override public boolean onPartActivate( EntityPlayer player, Vec3 pos ) { - if ( Platform.isClient() ) + if( Platform.isClient() ) return true; - if ( !this.proxy.isActive() ) + if( !this.proxy.isActive() ) return false; - if ( !Platform.hasPermissions( this.getLocation(), player ) ) + if( !Platform.hasPermissions( this.getLocation(), player ) ) return false; TileEntity te = this.tile; ItemStack eq = player.getCurrentEquippedItem(); - if ( Platform.isWrench( player, eq, te.xCoord, te.yCoord, te.zCoord ) ) + if( Platform.isWrench( player, eq, te.xCoord, te.yCoord, te.zCoord ) ) { this.isLocked = !this.isLocked; player.addChatMessage( ( this.isLocked ? PlayerMessages.isNowLocked : PlayerMessages.isNowUnlocked ).get() ); this.getHost().markForUpdate(); } - else if ( !this.isLocked ) + else if( !this.isLocked ) { this.configuredItem = AEItemStack.create( eq ); this.configureWatchers(); @@ -150,7 +151,7 @@ public class PartStorageMonitor extends PartMonitor implements IPartStorageMonit data.writeByte( this.spin ); data.writeBoolean( this.isLocked ); data.writeBoolean( this.configuredItem != null ); - if ( this.configuredItem != null ) + if( this.configuredItem != null ) this.configuredItem.writeToPacket( data ); } @@ -162,7 +163,7 @@ public class PartStorageMonitor extends PartMonitor implements IPartStorageMonit this.spin = data.readByte(); this.isLocked = data.readBoolean(); boolean val = data.readBoolean(); - if ( val ) + if( val ) this.configuredItem = AEItemStack.loadItemStackFromPacket( data ); else this.configuredItem = null; @@ -175,20 +176,20 @@ public class PartStorageMonitor extends PartMonitor implements IPartStorageMonit // update the system... public void configureWatchers() { - if ( this.myWatcher != null ) + if( this.myWatcher != null ) this.myWatcher.clear(); try { - if ( this.configuredItem != null ) + if( this.configuredItem != null ) { - if ( this.myWatcher != null ) + if( this.myWatcher != null ) this.myWatcher.add( this.configuredItem ); this.updateReportingValue( this.proxy.getStorage().getItemInventory() ); } } - catch ( GridAccessException e ) + catch( GridAccessException e ) { // >.> } @@ -201,10 +202,10 @@ public class PartStorageMonitor extends PartMonitor implements IPartStorageMonit private void updateReportingValue( IMEMonitor itemInventory ) { - if ( this.configuredItem != null ) + if( this.configuredItem != null ) { IAEItemStack result = itemInventory.getStorageList().findPrecise( this.configuredItem ); - if ( result == null ) + if( result == null ) this.configuredItem.setStackSize( 0 ); else this.configuredItem.setStackSize( result.getStackSize() ); @@ -216,7 +217,7 @@ public class PartStorageMonitor extends PartMonitor implements IPartStorageMonit protected void finalize() throws Throwable { super.finalize(); - if ( this.dspList != null ) + if( this.dspList != null ) GLAllocation.deleteDisplayLists( this.dspList ); } @@ -224,23 +225,23 @@ public class PartStorageMonitor extends PartMonitor implements IPartStorageMonit @SideOnly( Side.CLIENT ) public void renderDynamic( double x, double y, double z, IPartRenderHelper rh, RenderBlocks renderer ) { - if ( this.dspList == null ) + if( this.dspList == null ) this.dspList = GLAllocation.generateDisplayLists( 1 ); Tessellator tess = Tessellator.instance; - if ( Platform.isDrawing( tess ) ) + if( Platform.isDrawing( tess ) ) return; - if ( ( this.clientFlags & ( this.POWERED_FLAG | this.CHANNEL_FLAG ) ) != ( this.POWERED_FLAG | this.CHANNEL_FLAG ) ) + if( ( this.clientFlags & ( this.POWERED_FLAG | this.CHANNEL_FLAG ) ) != ( this.POWERED_FLAG | this.CHANNEL_FLAG ) ) return; IAEItemStack ais = (IAEItemStack) this.getDisplayed(); - if ( ais != null ) + if( ais != null ) { GL11.glPushMatrix(); GL11.glTranslated( x + 0.5, y + 0.5, z + 0.5 ); - if ( this.updateList ) + if( this.updateList ) { this.updateList = false; GL11.glNewList( this.dspList, GL11.GL_COMPILE_AND_EXECUTE ); @@ -272,38 +273,38 @@ public class PartStorageMonitor extends PartMonitor implements IPartStorageMonit ForgeDirection d = this.side; GL11.glTranslated( d.offsetX * 0.77, d.offsetY * 0.77, d.offsetZ * 0.77 ); - if ( d == ForgeDirection.UP ) + if( d == ForgeDirection.UP ) { GL11.glScalef( 1.0f, -1.0f, 1.0f ); GL11.glRotatef( 90.0f, 1.0f, 0.0f, 0.0f ); GL11.glRotatef( this.spin * 90.0F, 0, 0, 1 ); } - if ( d == ForgeDirection.DOWN ) + if( d == ForgeDirection.DOWN ) { GL11.glScalef( 1.0f, -1.0f, 1.0f ); GL11.glRotatef( -90.0f, 1.0f, 0.0f, 0.0f ); GL11.glRotatef( this.spin * -90.0F, 0, 0, 1 ); } - if ( d == ForgeDirection.EAST ) + if( d == ForgeDirection.EAST ) { GL11.glScalef( -1.0f, -1.0f, -1.0f ); GL11.glRotatef( -90.0f, 0.0f, 1.0f, 0.0f ); } - if ( d == ForgeDirection.WEST ) + if( d == ForgeDirection.WEST ) { GL11.glScalef( -1.0f, -1.0f, -1.0f ); GL11.glRotatef( 90.0f, 0.0f, 1.0f, 0.0f ); } - if ( d == ForgeDirection.NORTH ) + if( d == ForgeDirection.NORTH ) { GL11.glScalef( -1.0f, -1.0f, -1.0f ); } - if ( d == ForgeDirection.SOUTH ) + if( d == ForgeDirection.SOUTH ) { GL11.glScalef( -1.0f, -1.0f, -1.0f ); GL11.glRotatef( 180.0f, 0.0f, 1.0f, 0.0f ); @@ -329,7 +330,7 @@ public class PartStorageMonitor extends PartMonitor implements IPartStorageMonit ClientHelper.proxy.doRenderItem( sis, this.tile.getWorldObj() ); } - catch ( Exception e ) + catch( Exception e ) { AELog.error( e ); } @@ -339,22 +340,13 @@ public class PartStorageMonitor extends PartMonitor implements IPartStorageMonit GL11.glTranslatef( 0.0f, 0.14f, -0.24f ); GL11.glScalef( 1.0f / 62.0f, 1.0f / 62.0f, 1.0f / 62.0f ); - long qty = ais.getStackSize(); - if ( qty > 999999999999L ) - qty = 999999999999L; - - String msg = Long.toString( qty ); - if ( qty > 1000000000 ) - msg = Long.toString( qty / 1000000000 ) + 'B'; - else if ( qty > 1000000 ) - msg = Long.toString( qty / 1000000 ) + 'M'; - else if ( qty > 9999 ) - msg = Long.toString( qty / 1000 ) + 'K'; + final long stackSize = ais.getStackSize(); + final String renderedStackSize = NUMBER_CONVERTER.toHumanReadableForm( stackSize ); FontRenderer fr = Minecraft.getMinecraft().fontRenderer; - int width = fr.getStringWidth( msg ); + int width = fr.getStringWidth( renderedStackSize ); GL11.glTranslatef( -0.5f * width, 0.0f, -1.0f ); - fr.drawString( msg, 0, 0, 0 ); + fr.drawString( renderedStackSize, 0, 0, 0 ); GL11.glPopAttrib(); } @@ -375,9 +367,9 @@ public class PartStorageMonitor extends PartMonitor implements IPartStorageMonit @Override public void onStackChange( IItemList o, IAEStack fullStack, IAEStack diffStack, BaseActionSource src, StorageChannel chan ) { - if ( this.configuredItem != null ) + if( this.configuredItem != null ) { - if ( fullStack == null ) + if( fullStack == null ) this.configuredItem.setStackSize( 0 ); else this.configuredItem.setStackSize( fullStack.getStackSize() ); diff --git a/src/main/java/appeng/util/ReadableNumberConverter.java b/src/main/java/appeng/util/ReadableNumberConverter.java new file mode 100644 index 00000000..05df5e4a --- /dev/null +++ b/src/main/java/appeng/util/ReadableNumberConverter.java @@ -0,0 +1,114 @@ +package appeng.util; + + +/** + * Converter class to convert a large number into a SI system. + * + * @author thatsIch + * @version rv2 + * @since rv2 + */ +public enum ReadableNumberConverter +{ + INSTANCE; + + /** + * Defines the base for a division, non-si standard could be 1024 for kilobytes + */ + private static final int DIVISION_BASE = 1000; + + /** + * for lg(1000) = 3, just saves some calculation + */ + private static final double LOG_DIVISION_BASE = Math.log( DIVISION_BASE ); + + /** + * String representation of the sorted postfixes + */ + private static final char[] ENCODED_POSTFIXES = "KMGTPE".toCharArray(); + + /** + * if a result would be higher than this threshold, + * it is pushed into the next bigger group, + * so the display string is shorter + */ + private static final int SHORT_THRESHOLD = 100; + + /** + * Converts a number into a human readable form. It will not round the number, but floor it. + * + * Example: 15555L -> 15.5K + * + * @param number to be converted number + * + * @return String in SI format cut down as far as possible + */ + public String toHumanReadableForm( long number ) + { + final String sign = this.getSign( number ); + final long absNumber = Math.abs( number ); + + if( absNumber < DIVISION_BASE ) + return Long.toString( absNumber ); + + final int exp = (int) ( Math.log( absNumber ) / LOG_DIVISION_BASE ); + final char postFix = ENCODED_POSTFIXES[exp - 1]; + final int result = (int) ( absNumber / Math.pow( DIVISION_BASE, exp ) ); + + 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 earlier. This will limit the String size to 3 chars. + * + * Example: 900L -> 0.9K + * + * @param number to be converted number + * + * @return String in SI format cut down as far as possible + */ + public String toShortHumanReadableForm( long number ) + { + final String sign = this.getSign( number ); + final long absNumber = Math.abs( number ); + + if( absNumber < DIVISION_BASE ) + return Long.toString( absNumber ); + + final int exp = (int) ( Math.log( absNumber ) / LOG_DIVISION_BASE ); + final int result = (int) ( absNumber / Math.pow( DIVISION_BASE, exp ) ); + if( result >= SHORT_THRESHOLD ) + { + final int shortResult = result / SHORT_THRESHOLD; + final char postFix = ENCODED_POSTFIXES[exp]; + + return String.format( "%s.%d%s", sign, shortResult, postFix ); + } + else + { + final char postFix = ENCODED_POSTFIXES[exp - 1]; + + return String.format( "%s%d%s", sign, result, postFix ); + } + } + + /** + * Gets character representation of the sign of a number + * + * @param number maybe signed number + * + * @return '-' if the number is signed, else an empty character + */ + private String getSign( long number ) + { + if ( number < 0 ) + { + return "-"; + } + else + { + return ""; + } + } +} diff --git a/src/test/java/appeng/util/ReadableNumberConverterTest.java b/src/test/java/appeng/util/ReadableNumberConverterTest.java new file mode 100644 index 00000000..0d06527b --- /dev/null +++ b/src/test/java/appeng/util/ReadableNumberConverterTest.java @@ -0,0 +1,147 @@ +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 final class ReadableNumberConverterTest +{ + private static final long NUMBER_NORMAL = 55L; + private static final long NUMBER_KILO = 155555L; + 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_GIGA = -155555555555L; + + private static final String RESULT_NORMAL = "55"; + private static final String RESULT_KILO = "155K"; + private static final String RESULT_MEGA = "155M"; + private static final String RESULT_GIGA = "155G"; + private static final String RESULT_TERA = "155T"; + private static final String RESULT_PETA = "155P"; + private static final String RESULT_EXA = "1E"; + private static final String RESULT_NEGATIVE_GIGA = "-155G"; + + private static final String RESULT_SHORT_NORMAL = "55"; + private static final String RESULT_SHORT_KILO = ".1M"; + private static final String RESULT_SHORT_MEGA = ".1G"; + private static final String RESULT_SHORT_GIGA = ".1T"; + private static final String RESULT_SHORT_TERA = ".1P"; + private static final String RESULT_SHORT_PETA = ".1E"; + private static final String RESULT_SHORT_EXA = "1E"; + private static final String RESULT_SHORT_NEGATIVE_GIGA = "-.1T"; + + private final ReadableNumberConverter converter; + + public ReadableNumberConverterTest() + { + this.converter = ReadableNumberConverter.INSTANCE; + } + + @Test + public void testConvertNormal() + { + assertEquals( RESULT_NORMAL, this.converter.toHumanReadableForm( NUMBER_NORMAL ) ); + } + + @Test + public void testConvertKilo() + { + assertEquals( RESULT_KILO, this.converter.toHumanReadableForm( NUMBER_KILO ) ); + } + + @Test + public void testConvertMega() + { + assertEquals( RESULT_MEGA, this.converter.toHumanReadableForm( NUMBER_MEGA ) ); + } + + @Test + public void testConvertGiga() + { + assertEquals( RESULT_GIGA, this.converter.toHumanReadableForm( NUMBER_GIGA ) ); + } + + @Test + public void testConvertTera() + { + assertEquals( RESULT_TERA, this.converter.toHumanReadableForm( NUMBER_TERA ) ); + } + + @Test + public void testConvertPeta() + { + assertEquals( RESULT_PETA, this.converter.toHumanReadableForm( NUMBER_PETA ) ); + } + + @Test + public void testConvertExa() + { + assertEquals( RESULT_EXA, this.converter.toHumanReadableForm( NUMBER_EXA ) ); + } + + @Test + public void testConvertNegativeGiga() + { + assertEquals( RESULT_NEGATIVE_GIGA, this.converter.toHumanReadableForm( NUMBER_NEGATIVE_GIGA ) ); + } + + @Test + public void testConvertShortNormal() + { + assertEquals( RESULT_SHORT_NORMAL, this.converter.toShortHumanReadableForm( NUMBER_NORMAL ) ); + } + + @Test + public void testConvertShortKilo() + { + assertEquals( RESULT_SHORT_KILO, this.converter.toShortHumanReadableForm( NUMBER_KILO ) ); + } + + @Test + public void testConvertShortMega() + { + assertEquals( RESULT_SHORT_MEGA, this.converter.toShortHumanReadableForm( NUMBER_MEGA ) ); + } + + @Test + public void testConvertShortGiga() + { + assertEquals( RESULT_SHORT_GIGA, this.converter.toShortHumanReadableForm( NUMBER_GIGA ) ); + } + + @Test + public void testConvertShortTera() + { + assertEquals( RESULT_SHORT_TERA, this.converter.toShortHumanReadableForm( NUMBER_TERA ) ); + } + + @Test + public void testConvertShortPeta() + { + assertEquals( RESULT_SHORT_PETA, this.converter.toShortHumanReadableForm( NUMBER_PETA ) ); + } + + @Test + public void testConvertShortExa() + { + assertEquals( RESULT_SHORT_EXA, this.converter.toShortHumanReadableForm( NUMBER_EXA ) ); + } + + @Test + public void testConvertShortNegativeGiga() + { + assertEquals( RESULT_SHORT_NEGATIVE_GIGA, this.converter.toShortHumanReadableForm( NUMBER_NEGATIVE_GIGA ) ); + } +}