001package org.apache.commons.ssl.org.bouncycastle.asn1.isismtt.x509;
002
003import java.math.BigInteger;
004import java.util.Enumeration;
005
006import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1EncodableVector;
007import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Integer;
008import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Object;
009import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Primitive;
010import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Sequence;
011import org.apache.commons.ssl.org.bouncycastle.asn1.DERPrintableString;
012import org.apache.commons.ssl.org.bouncycastle.asn1.DERSequence;
013
014/**
015 * Monetary limit for transactions. The QcEuMonetaryLimit QC statement MUST be
016 * used in new certificates in place of the extension/attribute MonetaryLimit
017 * since January 1, 2004. For the sake of backward compatibility with
018 * certificates already in use, components SHOULD support MonetaryLimit (as well
019 * as QcEuLimitValue).
020 * <p>
021 * Indicates a monetary limit within which the certificate holder is authorized
022 * to act. (This value DOES NOT express a limit on the liability of the
023 * certification authority).
024 * <pre>
025 *    MonetaryLimitSyntax ::= SEQUENCE
026 *    {
027 *      currency PrintableString (SIZE(3)),
028 *      amount INTEGER,
029 *      exponent INTEGER
030 *    }
031 * </pre>
032 * <p>
033 * currency must be the ISO code.
034 * <p>
035 * value = amount�10*exponent
036 */
037public class MonetaryLimit
038    extends ASN1Object
039{
040    DERPrintableString currency;
041    ASN1Integer amount;
042    ASN1Integer exponent;
043
044    public static MonetaryLimit getInstance(Object obj)
045    {
046        if (obj == null || obj instanceof MonetaryLimit)
047        {
048            return (MonetaryLimit)obj;
049        }
050
051        if (obj instanceof ASN1Sequence)
052        {
053            return new MonetaryLimit(ASN1Sequence.getInstance(obj));
054        }
055
056        throw new IllegalArgumentException("unknown object in getInstance");
057    }
058
059    private MonetaryLimit(ASN1Sequence seq)
060    {
061        if (seq.size() != 3)
062        {
063            throw new IllegalArgumentException("Bad sequence size: "
064                + seq.size());
065        }
066        Enumeration e = seq.getObjects();
067        currency = DERPrintableString.getInstance(e.nextElement());
068        amount = ASN1Integer.getInstance(e.nextElement());
069        exponent = ASN1Integer.getInstance(e.nextElement());
070    }
071
072    /**
073     * Constructor from a given details.
074     * <p>
075     * value = amount�10^exponent
076     *
077     * @param currency The currency. Must be the ISO code.
078     * @param amount   The amount
079     * @param exponent The exponent
080     */
081    public MonetaryLimit(String currency, int amount, int exponent)
082    {
083        this.currency = new DERPrintableString(currency, true);
084        this.amount = new ASN1Integer(amount);
085        this.exponent = new ASN1Integer(exponent);
086    }
087
088    public String getCurrency()
089    {
090        return currency.getString();
091    }
092
093    public BigInteger getAmount()
094    {
095        return amount.getValue();
096    }
097
098    public BigInteger getExponent()
099    {
100        return exponent.getValue();
101    }
102
103    /**
104     * Produce an object suitable for an ASN1OutputStream.
105     * <p>
106     * Returns:
107     * <pre>
108     *    MonetaryLimitSyntax ::= SEQUENCE
109     *    {
110     *      currency PrintableString (SIZE(3)),
111     *      amount INTEGER,
112     *      exponent INTEGER
113     *    }
114     * </pre>
115     *
116     * @return a DERObject
117     */
118    public ASN1Primitive toASN1Primitive()
119    {
120        ASN1EncodableVector seq = new ASN1EncodableVector();
121        seq.add(currency);
122        seq.add(amount);
123        seq.add(exponent);
124
125        return new DERSequence(seq);
126    }
127
128}