package com.ripple.core.coretypes;

import org.json.JSONException;
import org.json.JSONObject;

import java.math.BigDecimal;

/**
 * Represents a currency/issuer pair
 */
public class Issue implements Comparable<Issue> {

    public static final Issue XRP = fromString("XRP");
    final Currency currency;
    final AccountID issuer;

    public Issue(Currency currency, AccountID issuer) {
        this.currency = currency;
        this.issuer = issuer;
    }

    public static Issue fromString(String pair) {
        String[] split = pair.split("/");
        return fromStringPair(split);
    }

    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 Currency currency() {
        return currency;
    }

    public AccountID issuer() {
        return issuer;
    }

    @Override
    public String toString() {
        if (isNative()) {
            return "XRP";
        } else {
            return String.format("%s/%s", currency, issuer);
        }
    }

    public JSONObject toJSON() {
        JSONObject o = new JSONObject();
        o.put("currency", currency);
        if (!isNative()) {
            o.put("issuer", issuer);
        }
        return o;
    }

    public Amount amount(BigDecimal value) {
        return new Amount(value, currency, issuer, isNative());
    }

    public boolean isNative() {
        return this == XRP || currency.equals(Currency.XRP);
    }

    public Amount amount(Number value) {
        return new Amount(BigDecimal.valueOf(value.doubleValue()), currency, issuer, isNative());
    }

    @Override
    public int compareTo(Issue o) {
        int ret = issuer.compareTo(o.issuer);
        if (ret != 0) {
            return ret;
        }
        ret = currency.compareTo(o.currency);
        return ret;
    }

    public Amount roundedAmount(BigDecimal amount) {
        return amount(Amount.roundValue(amount, isNative()));
    }
}

@@ -29,16 +29,63 @@
 types;%0A%0A
+import com.ripple.core.coretypes.hash.Hash160;%0A
 import o
@@ -1049,32 +1049,522 @@
        %7D%0A    %7D%0A%0A
+    /**%0A     * See %7B@link com.ripple.core.fields.Field#TakerGetsCurrency%7D%0A     * See %7B@link com.ripple.core.fields.Field#TakerGetsIssuer%7D%0A     *%0A     * TODO: better handling of Taker(Gets%7CPays)(Issuer%7CCurency)%0A     *       maybe special subclasses of AccountID / Currency%0A     *       respectively?%0A     */%0A    public static Issue from160s(Hash160 currency, Hash160 issuer) %7B%0A        return new Issue(new Currency(currency.bytes()),%0A                new AccountID(issuer.toBytes()));%0A    %7D%0A%0A
     public Curre

Add Issue.from160s: * Allow convenient creation of an Issue from TakerGetsIssuer etc Hash160 fields