Merge pull request #824 from jplsek/bank-value-update

Bank value plugin
This commit is contained in:
Adam
2018-03-18 12:25:54 -04:00
committed by GitHub
12 changed files with 591 additions and 18 deletions

View File

@@ -29,6 +29,7 @@ import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import javax.imageio.ImageIO;
import net.runelite.http.api.RuneLiteAPI;
import okhttp3.HttpUrl;
@@ -72,6 +73,42 @@ public class ItemClient
}
}
public ItemPrice[] lookupItemPrice(Integer[] itemIds) throws IOException
{
HttpUrl.Builder urlBuilder = RuneLiteAPI.getApiBase().newBuilder()
.addPathSegment("item")
.addPathSegment("price");
for (int itemId : itemIds)
{
urlBuilder.addQueryParameter("id", String.valueOf(itemId));
}
HttpUrl url = urlBuilder.build();
logger.debug("Built URI: {}", url);
Request request = new Request.Builder()
.url(url)
.build();
try (Response response = RuneLiteAPI.CLIENT.newCall(request).execute())
{
if (!response.isSuccessful())
{
logger.debug("Error looking up items {}: {}", Arrays.toString(itemIds), response.message());
return null;
}
InputStream in = response.body().byteStream();
return RuneLiteAPI.GSON.fromJson(new InputStreamReader(in), ItemPrice[].class);
}
catch (JsonParseException ex)
{
throw new IOException(ex);
}
}
public BufferedImage getIcon(int itemId) throws IOException
{
HttpUrl url = RuneLiteAPI.getApiBase().newBuilder()

View File

@@ -33,6 +33,8 @@ public enum Setting
{
ATTACK_STYLE(43),
BANK_TAB(115),
SPECIAL_ATTACK_PERCENT(300),
SPECIAL_ATTACK_ENABLED(301),

View File

@@ -61,7 +61,7 @@ public class BankItemQuery extends WidgetItemQuery
Widget[] children = bank.getDynamicChildren();
for (int i = 0; i < children.length; i++)
{
if (children[i].getItemId() == ITEM_EMPTY)
if (children[i].getItemId() == ITEM_EMPTY || children[i].isHidden())
{
continue;
}

View File

@@ -132,6 +132,8 @@ public class WidgetID
{
static final int ITEM_CONTAINER = 12;
static final int INVENTORY_ITEM_CONTAINER = 3;
static final int BANK_TITLE_BAR = 4;
static final int BANK_ITEM_COUNT = 5;
}
static class GrandExchange

View File

@@ -87,6 +87,8 @@ public enum WidgetInfo
BANK_ITEM_CONTAINER(WidgetID.BANK_GROUP_ID, WidgetID.Bank.ITEM_CONTAINER),
BANK_INVENTORY_ITEMS_CONTAINER(WidgetID.BANK_INVENTORY_GROUP_ID, WidgetID.Bank.INVENTORY_ITEM_CONTAINER),
BANK_TITLE_BAR(WidgetID.BANK_GROUP_ID, WidgetID.Bank.BANK_TITLE_BAR),
BANK_ITEM_COUNT(WidgetID.BANK_GROUP_ID, WidgetID.Bank.BANK_ITEM_COUNT),
GRAND_EXCHANGE_INVENTORY_ITEMS_CONTAINER(WidgetID.GRAND_EXCHANGE_INVENTORY_GROUP_ID, WidgetID.GrandExchange.INVENTORY_ITEM_CONTAINER),

View File

@@ -29,6 +29,10 @@ import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
@@ -36,6 +40,7 @@ import javax.annotation.Nullable;
import javax.inject.Inject;
import javax.inject.Singleton;
import lombok.Value;
import lombok.extern.slf4j.Slf4j;
import net.runelite.api.Client;
import static net.runelite.api.Constants.CLIENT_DEFAULT_ZOOM;
import net.runelite.api.ItemComposition;
@@ -45,6 +50,7 @@ import net.runelite.http.api.item.ItemPrice;
import net.runelite.http.api.item.SearchResult;
@Singleton
@Slf4j
public class ItemManager
{
@Value
@@ -58,17 +64,19 @@ public class ItemManager
/**
* not yet looked up
*/
static final ItemPrice EMPTY = new ItemPrice();
/**
* has no price
*/
static final ItemPrice NONE = new ItemPrice();
private final Client client;
private final ScheduledExecutorService scheduledExecutorService;
private final ItemClient itemClient = new ItemClient();
private final LoadingCache<String, SearchResult> itemSearches;
private final LoadingCache<Integer, ItemPrice> itemPrices;
private final LoadingCache<Integer, ItemPrice> itemPriceCache;
private final LoadingCache<ImageKey, BufferedImage> itemImages;
private final LoadingCache<Integer, ItemComposition> itemCompositions;
@@ -76,8 +84,10 @@ public class ItemManager
public ItemManager(@Nullable Client client, ScheduledExecutorService executor)
{
this.client = client;
itemPrices = CacheBuilder.newBuilder()
.maximumSize(512L)
this.scheduledExecutorService = executor;
itemPriceCache = CacheBuilder.newBuilder()
.maximumSize(1024L)
.expireAfterAccess(1, TimeUnit.HOURS)
.build(new ItemPriceLoader(executor, itemClient));
@@ -126,16 +136,74 @@ public class ItemManager
*/
public ItemPrice getItemPriceAsync(int itemId)
{
ItemPrice itemPrice = itemPrices.getIfPresent(itemId);
ItemPrice itemPrice = itemPriceCache.getIfPresent(itemId);
if (itemPrice != null && itemPrice != EMPTY)
{
return itemPrice == NONE ? null : itemPrice;
}
itemPrices.refresh(itemId);
itemPriceCache.refresh(itemId);
return null;
}
/**
* Look up bulk item prices asynchronously
*
* @param itemIds array of item Ids
* @return a future called with the looked up prices
*/
public CompletableFuture<ItemPrice[]> getItemPriceBatch(List<Integer> itemIds)
{
final List<Integer> lookup = new ArrayList<>();
final List<ItemPrice> existing = new ArrayList<>();
for (int itemId : itemIds)
{
ItemPrice itemPrice = itemPriceCache.getIfPresent(itemId);
if (itemPrice != null)
{
existing.add(itemPrice);
}
else
{
lookup.add(itemId);
}
}
// All cached?
if (lookup.isEmpty())
{
return CompletableFuture.completedFuture(existing.toArray(new ItemPrice[existing.size()]));
}
final CompletableFuture<ItemPrice[]> future = new CompletableFuture<>();
scheduledExecutorService.execute(() ->
{
try
{
// Do a query for the items not in the cache
ItemPrice[] itemPrices = itemClient.lookupItemPrice(lookup.toArray(new Integer[lookup.size()]));
if (itemPrices != null)
{
for (int itemId : lookup)
{
itemPriceCache.put(itemId, NONE);
}
for (ItemPrice itemPrice : itemPrices)
{
itemPriceCache.put(itemPrice.getItem().getId(), itemPrice);
}
// Append these to the already cached items
Arrays.stream(itemPrices).forEach(existing::add);
}
future.complete(existing.toArray(new ItemPrice[existing.size()]));
}
catch (Exception ex)
{
future.completeExceptionally(ex);
}
});
return future;
}
/**
* Look up an item's price synchronously
*
@@ -145,7 +213,7 @@ public class ItemManager
*/
public ItemPrice getItemPrice(int itemId) throws IOException
{
ItemPrice itemPrice = itemPrices.getIfPresent(itemId);
ItemPrice itemPrice = itemPriceCache.getIfPresent(itemId);
if (itemPrice != null && itemPrice != EMPTY)
{
return itemPrice == NONE ? null : itemPrice;
@@ -154,11 +222,11 @@ public class ItemManager
itemPrice = itemClient.lookupItemPrice(itemId);
if (itemPrice == null)
{
itemPrices.put(itemId, NONE);
itemPriceCache.put(itemId, NONE);
return null;
}
itemPrices.put(itemId, itemPrice);
itemPriceCache.put(itemId, itemPrice);
return itemPrice;
}

View File

@@ -0,0 +1,219 @@
/*
* Copyright (c) 2018, Jeremy Plsek <https://github.com/jplsek>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.runelite.client.plugins.bankvalue;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import javax.inject.Inject;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import net.runelite.api.Client;
import net.runelite.api.ItemComposition;
import static net.runelite.api.ItemID.COINS_995;
import static net.runelite.api.ItemID.PLATINUM_TOKEN;
import net.runelite.api.queries.BankItemQuery;
import net.runelite.api.widgets.Widget;
import net.runelite.api.widgets.WidgetInfo;
import net.runelite.api.widgets.WidgetItem;
import net.runelite.client.game.ItemManager;
import net.runelite.http.api.item.ItemPrice;
@Slf4j
class BankCalculation
{
private static final float HIGH_ALCHEMY_CONSTANT = 0.6f;
private final Client client;
private final BankValueConfig config;
private final ItemManager itemManager;
// Used to avoid extra calculation if the bank has not changed
private int itemsHash;
@Getter
private long gePrice;
@Getter
private long haPrice;
@Getter
private boolean finished;
@Inject
BankCalculation(Client client, ItemManager itemManager, BankValueConfig config)
{
this.client = client;
this.itemManager = itemManager;
this.config = config;
}
/**
* Calculate the bank based on the cache, price can be 0 if bank not active, or cache not set
*/
void calculate()
{
Widget widgetBankTitleBar = client.getWidget(WidgetInfo.BANK_TITLE_BAR);
// Don't update on a search because rs seems to constantly update the title
if (widgetBankTitleBar == null ||
widgetBankTitleBar.isHidden() ||
widgetBankTitleBar.getText().contains("Showing"))
{
return;
}
WidgetItem[] widgetItems = new BankItemQuery().result(client);
if (widgetItems.length == 0 || !isBankDifferent(widgetItems))
{
return;
}
log.debug("Calculating new bank value...");
gePrice = haPrice = 0;
finished = false;
List<ItemComposition> itemCompositions = new ArrayList<>();
Map<Integer, WidgetItem> itemMap = new HashMap<>();
List<Integer> itemIds = new ArrayList<>();
// Generate our lists (and do some quick price additions)
for (WidgetItem widgetItem : widgetItems)
{
if (widgetItem.getId() <= 0 || widgetItem.getQuantity() == 0)
{
continue;
}
if (widgetItem.getId() == COINS_995)
{
gePrice += widgetItem.getQuantity();
haPrice += widgetItem.getQuantity();
continue;
}
if (widgetItem.getId() == PLATINUM_TOKEN)
{
gePrice += widgetItem.getQuantity() * 1000;
haPrice += widgetItem.getQuantity() * 1000;
continue;
}
ItemComposition itemComposition = itemManager.getItemComposition(widgetItem.getId());
itemCompositions.add(itemComposition);
itemMap.put(widgetItem.getId(), widgetItem);
if (config.showGE())
{
itemIds.add(widgetItem.getId());
}
}
// Now do the calculations
if (config.showGE() && !itemIds.isEmpty())
{
CompletableFuture<ItemPrice[]> future = itemManager.getItemPriceBatch(itemIds);
future.whenComplete((ItemPrice[] itemPrices, Throwable ex) ->
{
if (ex != null)
{
log.debug("Error looking up item prices", ex);
return;
}
if (itemPrices == null)
{
log.debug("Error looking up item prices");
return;
}
log.debug("Price lookup is complete. {} prices.", itemPrices.length);
try
{
for (ItemPrice itemPrice : itemPrices)
{
if (itemPrice.getItem() == null)
{
continue; // cached no price
}
gePrice += itemPrice.getPrice() * itemMap.get(itemPrice.getItem().getId()).getQuantity();
}
}
catch (Exception ex2)
{
log.warn("error calculating price", ex2);
}
finally
{
finished = true;
}
});
}
else
{
finished = true;
}
if (config.showHA())
{
for (ItemComposition itemComposition : itemCompositions)
{
int price = itemComposition.getPrice();
if (price > 0)
{
haPrice += Math.round(price * HIGH_ALCHEMY_CONSTANT) *
itemMap.get(itemComposition.getId()).getQuantity();
}
}
}
}
private boolean isBankDifferent(WidgetItem[] widgetItems)
{
Map<Integer, Integer> mapCheck = new HashMap<>();
for (WidgetItem widgetItem : widgetItems)
{
mapCheck.put(widgetItem.getId(), widgetItem.getQuantity());
}
int curHash = mapCheck.hashCode();
if (curHash != itemsHash)
{
itemsHash = curHash;
return true;
}
return false;
}
}

View File

@@ -0,0 +1,106 @@
/*
* Copyright (c) 2018, Jeremy Plsek <https://github.com/jplsek>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.runelite.client.plugins.bankvalue;
import com.google.inject.Inject;
import lombok.extern.slf4j.Slf4j;
import net.runelite.api.Client;
import net.runelite.api.widgets.Widget;
import net.runelite.api.widgets.WidgetInfo;
import net.runelite.client.util.StackFormatter;
@Slf4j
class BankTitle
{
private final Client client;
private final BankValueConfig config;
private String bankTitle;
@Inject
BankTitle(Client client, BankValueConfig config)
{
this.client = client;
this.config = config;
}
void reset()
{
Widget widgetBankTitleBar = client.getWidget(WidgetInfo.BANK_TITLE_BAR);
if (widgetBankTitleBar == null || widgetBankTitleBar.isHidden())
{
return;
}
widgetBankTitleBar.setText(bankTitle);
}
void save()
{
Widget widgetBankTitleBar = client.getWidget(WidgetInfo.BANK_TITLE_BAR);
// Only save if the title hasn't been modified
// Don't update on a search because rs seems to constantly update the title
if (widgetBankTitleBar == null ||
widgetBankTitleBar.isHidden() ||
widgetBankTitleBar.getText().contains("(") ||
widgetBankTitleBar.getText().contains("Showing"))
{
return;
}
bankTitle = widgetBankTitleBar.getText();
}
void update(long gePrice, long haPrice)
{
Widget widgetBankTitleBar = client.getWidget(WidgetInfo.BANK_TITLE_BAR);
// Don't update on a search because rs seems to constantly update the title
if (widgetBankTitleBar == null ||
widgetBankTitleBar.isHidden() ||
widgetBankTitleBar.getText().contains("Showing") ||
widgetBankTitleBar.getText().contains("("))
{
return;
}
String strCurrentTab = "";
if (config.showGE() && gePrice != 0)
{
strCurrentTab += " (EX: " + StackFormatter.quantityToStackSize(gePrice) + ")";
}
if (config.showHA() && haPrice != 0)
{
strCurrentTab += " (HA: " + StackFormatter.quantityToStackSize(haPrice) + ")";
}
log.debug("Setting bank title: {}", bankTitle + strCurrentTab);
widgetBankTitleBar.setText(bankTitle + strCurrentTab);
}
}

View File

@@ -0,0 +1,55 @@
/*
* Copyright (c) 2018, TheLonelyDev <https://github.com/TheLonelyDev>
* Copyright (c) 2018, Jeremy Plsek <https://github.com/jplsek>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.runelite.client.plugins.bankvalue;
import net.runelite.client.config.Config;
import net.runelite.client.config.ConfigGroup;
import net.runelite.client.config.ConfigItem;
@ConfigGroup(
keyName = "bankvalue",
name = "Bank Value",
description = "Shows the value of your bank and/or current tab")
public interface BankValueConfig extends Config
{
@ConfigItem(
keyName = "showGE",
name = "Show Grand Exchange price",
description = "Show grand exchange price total (GE)")
default boolean showGE()
{
return true;
}
@ConfigItem(
keyName = "showHA",
name = "Show high alchemy price",
description = "Show high alchemy price total (HA)")
default boolean showHA()
{
return false;
}
}

View File

@@ -0,0 +1,80 @@
/*
* Copyright (c) 2018, TheLonelyDev <https://github.com/TheLonelyDev>
* Copyright (c) 2018, Jeremy Plsek <https://github.com/jplsek>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.runelite.client.plugins.bankvalue;
import com.google.common.eventbus.Subscribe;
import com.google.inject.Provides;
import javax.inject.Inject;
import net.runelite.api.Client;
import net.runelite.api.events.GameTick;
import net.runelite.api.widgets.Widget;
import net.runelite.api.widgets.WidgetInfo;
import net.runelite.client.config.ConfigManager;
import net.runelite.client.plugins.Plugin;
import net.runelite.client.plugins.PluginDescriptor;
@PluginDescriptor(name = "Bank Value")
public class BankValuePlugin extends Plugin
{
@Inject
private Client client;
@Inject
private BankCalculation bankCalculation;
@Inject
private BankTitle bankTitle;
@Provides
BankValueConfig getConfig(ConfigManager configManager)
{
return configManager.getConfig(BankValueConfig.class);
}
@Override
protected void shutDown()
{
bankTitle.reset();
}
@Subscribe
public void onGameTick(GameTick event)
{
Widget widgetBankTitleBar = client.getWidget(WidgetInfo.BANK_TITLE_BAR);
if (widgetBankTitleBar == null || widgetBankTitleBar.isHidden())
{
return;
}
bankTitle.save();
bankCalculation.calculate();
if (bankCalculation.isFinished())
{
bankTitle.update(bankCalculation.getGePrice(), bankCalculation.getHaPrice());
}
}
}

View File

@@ -58,12 +58,12 @@ public class StackFormatter
* @return A condensed version, with commas, K, M or B
* as needed to 3 significant figures.
*/
public static String quantityToStackSize(int quantity)
public static String quantityToStackSize(long quantity)
{
if (quantity < 0)
{
// Integer.MIN_VALUE = -1 * Integer.MIN_VALUE so we need to correct for it.
return "-" + quantityToStackSize(quantity == Integer.MIN_VALUE ? Integer.MAX_VALUE : -quantity);
// Long.MIN_VALUE = -1 * Long.MIN_VALUE so we need to correct for it.
return "-" + quantityToStackSize(quantity == Long.MIN_VALUE ? Long.MAX_VALUE : -quantity);
}
else if (quantity < 10_000)
{
@@ -71,14 +71,14 @@ public class StackFormatter
}
String suffix = SUFFIXES[0];
int divideBy = 1;
long divideBy = 1;
// determine correct suffix by iterating backward through the list
// of suffixes until the suffix results in a value >= 1
for (int i = (SUFFIXES.length - 1); i >= 0; i--)
{
divideBy = (int) Math.pow(10, i * 3);
if ((float) quantity / divideBy >= 1)
divideBy = (long) Math.pow(10, i * 3);
if ((double) quantity / divideBy >= 1)
{
suffix = SUFFIXES[i];
break;
@@ -86,7 +86,7 @@ public class StackFormatter
}
// get locale formatted string
String formattedString = NUMBER_FORMATTER.format((float) quantity / divideBy);
String formattedString = NUMBER_FORMATTER.format((double) quantity / divideBy);
// strip down any digits past the 4 first
formattedString = (formattedString.length() > 4 ? formattedString.substring(0, 4) : formattedString);

View File

@@ -61,12 +61,14 @@ public class StackFormatterTest
assertEquals("100K", StackFormatter.quantityToStackSize(100_000));
assertEquals("10M", StackFormatter.quantityToStackSize(10_000_000));
assertEquals(NumberFormat.getNumberInstance().format(2.14) + "B", StackFormatter.quantityToStackSize(Integer.MAX_VALUE));
assertEquals("100B", StackFormatter.quantityToStackSize(100_000_000_000L));
assertEquals("0", StackFormatter.quantityToStackSize(-0));
assertEquals("-400", StackFormatter.quantityToStackSize(-400));
assertEquals("-400K", StackFormatter.quantityToStackSize(-400_000));
assertEquals("-40M", StackFormatter.quantityToStackSize(-40_000_000));
assertEquals(NumberFormat.getNumberInstance().format(-2.14) + "B", StackFormatter.quantityToStackSize(Integer.MIN_VALUE));
assertEquals("-400B", StackFormatter.quantityToStackSize(-400_000_000_000L));
}
@Test
@@ -103,4 +105,4 @@ public class StackFormatterTest
{
}
}
}
}