1package org.bouncycastle.asn1.x509.qualified;
2
3import org.bouncycastle.asn1.ASN1Choice;
4import org.bouncycastle.asn1.ASN1Encodable;
5import org.bouncycastle.asn1.DEREncodable;
6import org.bouncycastle.asn1.DERInteger;
7import org.bouncycastle.asn1.DERObject;
8import org.bouncycastle.asn1.DERPrintableString;
9
10/**
11 * The Iso4217CurrencyCode object.
12 * <pre>
13 * Iso4217CurrencyCode  ::=  CHOICE {
14 *       alphabetic              PrintableString (SIZE 3), --Recommended
15 *       numeric              INTEGER (1..999) }
16 * -- Alphabetic or numeric currency code as defined in ISO 4217
17 * -- It is recommended that the Alphabetic form is used
18 * </pre>
19 */
20public class Iso4217CurrencyCode
21    extends ASN1Encodable
22    implements ASN1Choice
23{
24    final int ALPHABETIC_MAXSIZE = 3;
25    final int NUMERIC_MINSIZE = 1;
26    final int NUMERIC_MAXSIZE = 999;
27
28    DEREncodable obj;
29    int          numeric;
30
31    public static Iso4217CurrencyCode getInstance(
32        Object obj)
33    {
34        if (obj == null || obj instanceof Iso4217CurrencyCode)
35        {
36            return (Iso4217CurrencyCode)obj;
37        }
38
39        if (obj instanceof DERInteger)
40        {
41            DERInteger numericobj = DERInteger.getInstance(obj);
42            int numeric = numericobj.getValue().intValue();
43            return new Iso4217CurrencyCode(numeric);
44        }
45        else
46        if (obj instanceof DERPrintableString)
47        {
48            DERPrintableString alphabetic = DERPrintableString.getInstance(obj);
49            return new Iso4217CurrencyCode(alphabetic.getString());
50        }
51        throw new IllegalArgumentException("unknown object in getInstance");
52    }
53
54    public Iso4217CurrencyCode(
55        int numeric)
56    {
57        if (numeric > NUMERIC_MAXSIZE || numeric < NUMERIC_MINSIZE)
58        {
59            throw new IllegalArgumentException("wrong size in numeric code : not in (" +NUMERIC_MINSIZE +".."+ NUMERIC_MAXSIZE +")");
60        }
61        obj = new DERInteger(numeric);
62    }
63
64    public Iso4217CurrencyCode(
65        String alphabetic)
66    {
67        if (alphabetic.length() > ALPHABETIC_MAXSIZE)
68        {
69            throw new IllegalArgumentException("wrong size in alphabetic code : max size is " + ALPHABETIC_MAXSIZE);
70        }
71        obj = new DERPrintableString(alphabetic);
72    }
73
74    public boolean isAlphabetic()
75    {
76        return obj instanceof DERPrintableString;
77    }
78
79    public String getAlphabetic()
80    {
81        return ((DERPrintableString)obj).getString();
82    }
83
84    public int getNumeric()
85    {
86        return ((DERInteger)obj).getValue().intValue();
87    }
88
89    public DERObject toASN1Object()
90    {
91        return obj.getDERObject();
92    }
93}
94