package org.stellar.sdk;
/**
* Base Asset class.
* @see Assets
*/
public abstract class Asset {
Asset() {}
public static Asset create(String type, String code, String issuer) {
if (type.equals("native")) {
return new AssetTypeNative();
} else {
return Asset.createNonNativeAsset(code, KeyPair.fromAccountId(issuer));
}
}
/**
* Creates one of AssetTypeCreditAlphaNum4 or AssetTypeCreditAlphaNum12 object based on a code length
* @param code Asset code
* @param issuer Asset issuer
*/
public static Asset createNonNativeAsset(String code, KeyPair issuer) {
if (code.length() >= 1 && code.length() <= 4) {
return new AssetTypeCreditAlphaNum4(code, issuer);
} else if (code.length() >= 5 && code.length() <= 12) {
return new AssetTypeCreditAlphaNum12(code, issuer);
} else {
throw new AssetCodeLengthInvalidException();
}
}
/**
* Generates Asset object from a given XDR object
* @param xdr XDR object
*/
public static Asset fromXdr(org.stellar.sdk.xdr.Asset xdr) {
switch (xdr.getDiscriminant()) {
case ASSET_TYPE_NATIVE:
return new AssetTypeNative();
case ASSET_TYPE_CREDIT_ALPHANUM4:
String assetCode4 = Util.paddedByteArrayToString(xdr.getAlphaNum4().getAssetCode());
KeyPair issuer4 = KeyPair.fromXdrPublicKey(
xdr.getAlphaNum4().getIssuer().getAccountID());
return new AssetTypeCreditAlphaNum4(assetCode4, issuer4);
case ASSET_TYPE_CREDIT_ALPHANUM12:
String assetCode12 = Util.paddedByteArrayToString(xdr.getAlphaNum12().getAssetCode());
KeyPair issuer12 = KeyPair.fromXdrPublicKey(xdr.getAlphaNum12().getIssuer().getAccountID());
return new AssetTypeCreditAlphaNum12(assetCode12, issuer12);
default:
throw new IllegalArgumentException("Unknown asset type " + xdr.getDiscriminant());
}
}
/**
* Returns asset type. Possible types:
*
nativecredit_alphanum4credit_alphanum12