public Amount(BigDecimal value, String currency) { isNative = false; this.currency = Currency.fromString(currency); this.setAndCheckValue(value); }
public boolean isNative() { return this == XRP || currency.equals(Currency.XRP); }
public boolean isIOU() { return !isNative(); }
@Override public String toString() { switch (type) { case ISO: String code = getCurrencyCodeFromTLCBytes(bytes()); if (code.equals("XRP")) { // HEX of the bytes return super.toString(); } else if (code.equals("\0\0\0")) { return "XRP"; } else { // the 3 letter isoCode return code; } case HASH: case DEMURRAGE: case UNKNOWN: default: return super.toString(); } }
@Override public boolean equals(Object obj) { if (obj instanceof Currency) { Currency other = (Currency) obj; byte[] bytes = this.bytes(); byte[] otherBytes = other.bytes(); if (type == Type.ISO && other.type == Type.ISO) { return (bytes[12] == otherBytes[12] && bytes[13] == otherBytes[13] && bytes[14] == otherBytes[14]); } } return super.equals(obj); // Full comparison }
@Override public Currency newInstance(byte[] b) { return new Currency(b); }
@Override public int compareTo(Issue o) { int ret = issuer.compareTo(o.issuer); if (ret != 0) { return ret; } ret = currency.compareTo(o.currency); return ret; }
@Override public void toBytesSink(BytesSink buffer) { int n = 0; for (Path path : this) { if (n++ != 0) { buffer.add(PATH_SEPARATOR_BYTE); } for (Hop hop : path) { int type = hop.getType(); buffer.add((byte) type); if (hop.hasAccount()) { buffer.add(hop.account.bytes()); } if (hop.hasCurrency()) { buffer.add(hop.currency.bytes()); } if (hop.hasIssuer()) { buffer.add(hop.issuer.bytes()); } } } buffer.add(PATHSET_END_BYTE); }
/** * See {@link com.ripple.core.fields.Field#TakerGetsCurrency} * See {@link com.ripple.core.fields.Field#TakerGetsIssuer} * * TODO: better handling of Taker(Gets|Pays)(Issuer|Curency) * maybe special subclasses of AccountID / Currency * respectively? */ public static Issue from160s(Hash160 currency, Hash160 issuer) { return new Issue(new Currency(currency.bytes()), new AccountID(issuer.toBytes())); }
public Issue issue(String code) { return new Issue(Currency.fromString(code), this); }
public boolean isNative() { return this == Currency.XRP || equals(Currency.XRP); }
private void normalizeDestinationAmount() { if (destinationCurrency != null && destinationAmountValue != null) { BigDecimal normalized = Amount.roundValue(destinationAmountValue, destinationCurrency.isNative()); if (destinationAmountValue.stripTrailingZeros().compareTo(normalized.stripTrailingZeros()) != 0) { String text = normalized.stripTrailingZeros().toPlainString(); destinationAmountInput.setText(text); destinationAmountInput.setSelection(text.length()); } } }
@Override public void toBytesSink(BytesSink to) { UInt64 man = mantissa(); if (isNative()) { if (!isNegative()) { man = man.or(BINARY_FLAG_IS_NON_NEGATIVE_NATIVE); } to.add(man.toByteArray()); } else { int exponent = exponent(); UInt64 packed; if (isZero()) { packed = BINARY_FLAG_IS_IOU; } else if (isNegative()) { packed = man.or(new UInt64(512 + 0 + 97 + exponent).shiftLeft(64 - 10)); } else { packed = man.or(new UInt64(512 + 256 + 97 + exponent).shiftLeft(64 - 10)); } to.add(packed.toByteArray()); to.add(currency.bytes()); to.add(issuer.bytes()); } }
static public Hop fromJSONObject(JSONObject json) { Hop hop = new Hop(); if (json.has("account")) { hop.account = AccountID.fromAddress(json.getString("account")); } if (json.has("issuer")) { hop.issuer = AccountID.fromAddress(json.getString("issuer")); } if (json.has("currency")) { hop.currency = Currency.fromString(json.getString("currency")); } if (json.has("type")) { hop.type = json.getInt("type"); } return hop; }
public PaymentFlow destinationAmountCurrency(final Currency currency) { if (destAmountCurrency == null || !currency.equals(destAmountCurrency)) { destAmountCurrency = currency; makePathFindRequestIfCan(); } return this; }
public static AccountLine fromJSON(AccountID orientedTo, JSONObject line) { AccountLine l = new AccountLine(); AccountID peer = AccountID.fromAddress(line.getString("account")); BigDecimal balance = new BigDecimal(line.getString("balance")); BigDecimal limit = new BigDecimal(line.getString("limit")); BigDecimal limit_peer = new BigDecimal(line.getString("limit_peer")); l.currency = Currency.fromString(line.getString("currency")); l.balance = new Amount(balance, l.currency, peer); l.limit = new Amount(limit, l.currency, peer); l.limit_peer = new Amount(limit_peer, l.currency, orientedTo); l.freeze = line.optBoolean("freeze", false); l.freeze_peer = line.optBoolean("freeze_peer", false); l.authorized = line.optBoolean("authorized", false); l.authorized_peer = line.optBoolean("authorized_peer", false); l.no_ripple = line.optBoolean("no_ripple", false); l.no_ripple_peer = line.optBoolean("no_ripple_peer", false); l.quality_in = line.optInt("quality_in", 0); l.quality_out = line.optInt("quality_out", 0); return l; }
public boolean equals(Amount amt) { return equalValue(amt) && currency.equals(amt.currency) && (isNative() || issuer.equals(amt.issuer)); }
private static Issue fromStringPair(String[] split) { if (split.length == 2) { return new Issue(Currency.fromString(split[0]), AccountID.fromString(split[1])); } else if (split[0].equals("XRP")) { return new Issue(Currency.XRP, AccountID.XRP_ISSUER); } else { throw new RuntimeException("Issue string must be XRP or $currency/$issuer"); } }
public boolean isFor(AccountID s1, AccountID s2, Currency currency) { return currency.equals(balance().currency()) && isFor(s1) && isFor(s2); }