mirror of
https://github.com/Qortal/altcoinj.git
synced 2025-02-13 10:45:51 +00:00
Replace monetary codes map with an array as a simpler data type.
This commit is contained in:
parent
cc0a00fbdd
commit
72f1f748ee
@ -25,10 +25,9 @@ import static com.google.common.math.LongMath.divide;
|
|||||||
import java.math.RoundingMode;
|
import java.math.RoundingMode;
|
||||||
import java.text.DecimalFormatSymbols;
|
import java.text.DecimalFormatSymbols;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import org.bitcoinj.core.Coin;
|
import org.bitcoinj.core.Coin;
|
||||||
import org.bitcoinj.core.Monetary;
|
import org.bitcoinj.core.Monetary;
|
||||||
@ -61,6 +60,8 @@ public final class MonetaryFormat {
|
|||||||
/** Currency code for base 1/1000000 Bitcoin. */
|
/** Currency code for base 1/1000000 Bitcoin. */
|
||||||
public static final String CODE_UBTC = "µBTC";
|
public static final String CODE_UBTC = "µBTC";
|
||||||
|
|
||||||
|
public static final int MAX_DECIMALS = 8;
|
||||||
|
|
||||||
private final char negativeSign;
|
private final char negativeSign;
|
||||||
private final char positiveSign;
|
private final char positiveSign;
|
||||||
private final char zeroDigit;
|
private final char zeroDigit;
|
||||||
@ -69,7 +70,7 @@ public final class MonetaryFormat {
|
|||||||
private final List<Integer> decimalGroups;
|
private final List<Integer> decimalGroups;
|
||||||
private final int shift;
|
private final int shift;
|
||||||
private final RoundingMode roundingMode;
|
private final RoundingMode roundingMode;
|
||||||
private final Map<Integer, String> codes;
|
private final String[] codes;
|
||||||
private final char codeSeparator;
|
private final char codeSeparator;
|
||||||
private final boolean codePrefixed;
|
private final boolean codePrefixed;
|
||||||
|
|
||||||
@ -233,10 +234,11 @@ public final class MonetaryFormat {
|
|||||||
*/
|
*/
|
||||||
public MonetaryFormat code(int codeShift, String code) {
|
public MonetaryFormat code(int codeShift, String code) {
|
||||||
checkArgument(codeShift >= 0);
|
checkArgument(codeShift >= 0);
|
||||||
Map<Integer, String> codes = new HashMap<Integer, String>();
|
final String[] codes = null == this.codes
|
||||||
if (this.codes != null)
|
? new String[MAX_DECIMALS]
|
||||||
codes.putAll(this.codes);
|
: Arrays.copyOf(this.codes, this.codes.length);
|
||||||
codes.put(codeShift, code);
|
|
||||||
|
codes[codeShift] = code;
|
||||||
return new MonetaryFormat(negativeSign, positiveSign, zeroDigit, decimalMark, minDecimals, decimalGroups,
|
return new MonetaryFormat(negativeSign, positiveSign, zeroDigit, decimalMark, minDecimals, decimalGroups,
|
||||||
shift, roundingMode, codes, codeSeparator, codePrefixed);
|
shift, roundingMode, codes, codeSeparator, codePrefixed);
|
||||||
}
|
}
|
||||||
@ -298,16 +300,16 @@ public final class MonetaryFormat {
|
|||||||
this.decimalGroups = null;
|
this.decimalGroups = null;
|
||||||
this.shift = 0;
|
this.shift = 0;
|
||||||
this.roundingMode = RoundingMode.HALF_UP;
|
this.roundingMode = RoundingMode.HALF_UP;
|
||||||
this.codes = new HashMap<Integer, String>();
|
this.codes = new String[MAX_DECIMALS];
|
||||||
this.codes.put(0, CODE_BTC);
|
this.codes[0] = CODE_BTC;
|
||||||
this.codes.put(3, CODE_MBTC);
|
this.codes[3] = CODE_MBTC;
|
||||||
this.codes.put(6, CODE_UBTC);
|
this.codes[6] = CODE_UBTC;
|
||||||
this.codeSeparator = ' ';
|
this.codeSeparator = ' ';
|
||||||
this.codePrefixed = true;
|
this.codePrefixed = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private MonetaryFormat(char negativeSign, char positiveSign, char zeroDigit, char decimalMark, int minDecimals,
|
private MonetaryFormat(char negativeSign, char positiveSign, char zeroDigit, char decimalMark, int minDecimals,
|
||||||
List<Integer> decimalGroups, int shift, RoundingMode roundingMode, Map<Integer, String> codes,
|
List<Integer> decimalGroups, int shift, RoundingMode roundingMode, String[] codes,
|
||||||
char codeSeparator, boolean codePrefixed) {
|
char codeSeparator, boolean codePrefixed) {
|
||||||
this.negativeSign = negativeSign;
|
this.negativeSign = negativeSign;
|
||||||
this.positiveSign = positiveSign;
|
this.positiveSign = positiveSign;
|
||||||
@ -443,9 +445,8 @@ public final class MonetaryFormat {
|
|||||||
public String code() {
|
public String code() {
|
||||||
if (codes == null)
|
if (codes == null)
|
||||||
return null;
|
return null;
|
||||||
String code = codes.get(shift);
|
if (codes[shift] == null)
|
||||||
if (code == null)
|
|
||||||
throw new NumberFormatException("missing code for shift: " + shift);
|
throw new NumberFormatException("missing code for shift: " + shift);
|
||||||
return code;
|
return codes[shift];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -220,6 +220,16 @@ public class MonetaryFormatTest {
|
|||||||
assertEquals("dBTC 0", MonetaryFormat.UBTC.code(1, "dBTC").shift(1).format(Coin.ZERO).toString());
|
assertEquals("dBTC 0", MonetaryFormat.UBTC.code(1, "dBTC").shift(1).format(Coin.ZERO).toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test clearing all codes, and then setting codes after clearing.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void noCode() throws Exception {
|
||||||
|
assertEquals("0", MonetaryFormat.UBTC.noCode().shift(0).format(Coin.ZERO).toString());
|
||||||
|
// Ensure that inserting a code after codes are wiped, works
|
||||||
|
assertEquals("dBTC 0", MonetaryFormat.UBTC.noCode().code(1, "dBTC").shift(1).format(Coin.ZERO).toString());
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void codeOrientation() throws Exception {
|
public void codeOrientation() throws Exception {
|
||||||
assertEquals("BTC 0.00", MonetaryFormat.BTC.prefixCode().format(Coin.ZERO).toString());
|
assertEquals("BTC 0.00", MonetaryFormat.BTC.prefixCode().format(Coin.ZERO).toString());
|
||||||
|
Loading…
x
Reference in New Issue
Block a user