001package org.apache.commons.ssl.org.bouncycastle.asn1.x509.qualified;
002
003import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Choice;
004import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Encodable;
005import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Integer;
006import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Object;
007import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Primitive;
008import org.apache.commons.ssl.org.bouncycastle.asn1.DERPrintableString;
009
010/**
011 * The Iso4217CurrencyCode object.
012 * <pre>
013 * Iso4217CurrencyCode  ::=  CHOICE {
014 *       alphabetic              PrintableString (SIZE 3), --Recommended
015 *       numeric              INTEGER (1..999) }
016 * -- Alphabetic or numeric currency code as defined in ISO 4217
017 * -- It is recommended that the Alphabetic form is used
018 * </pre>
019 */
020public class Iso4217CurrencyCode 
021    extends ASN1Object
022    implements ASN1Choice
023{
024    final int ALPHABETIC_MAXSIZE = 3;
025    final int NUMERIC_MINSIZE = 1;
026    final int NUMERIC_MAXSIZE = 999;
027    
028    ASN1Encodable obj;
029    int          numeric;
030    
031    public static Iso4217CurrencyCode getInstance(
032        Object obj)
033    {
034        if (obj == null || obj instanceof Iso4217CurrencyCode)
035        {
036            return (Iso4217CurrencyCode)obj;
037        }
038
039        if (obj instanceof ASN1Integer)
040        {
041            ASN1Integer numericobj = ASN1Integer.getInstance(obj);
042            int numeric = numericobj.getValue().intValue();  
043            return new Iso4217CurrencyCode(numeric);            
044        }
045        else
046        if (obj instanceof DERPrintableString)
047        {
048            DERPrintableString alphabetic = DERPrintableString.getInstance(obj);
049            return new Iso4217CurrencyCode(alphabetic.getString());
050        }
051        throw new IllegalArgumentException("unknown object in getInstance");
052    }
053            
054    public Iso4217CurrencyCode(
055        int numeric)
056    {
057        if (numeric > NUMERIC_MAXSIZE || numeric < NUMERIC_MINSIZE)
058        {
059            throw new IllegalArgumentException("wrong size in numeric code : not in (" +NUMERIC_MINSIZE +".."+ NUMERIC_MAXSIZE +")");
060        }
061        obj = new ASN1Integer(numeric);
062    }
063    
064    public Iso4217CurrencyCode(
065        String alphabetic)
066    {
067        if (alphabetic.length() > ALPHABETIC_MAXSIZE)
068        {
069            throw new IllegalArgumentException("wrong size in alphabetic code : max size is " + ALPHABETIC_MAXSIZE);
070        }
071        obj = new DERPrintableString(alphabetic);
072    }            
073
074    public boolean isAlphabetic()
075    {
076        return obj instanceof DERPrintableString;
077    }
078    
079    public String getAlphabetic()
080    {
081        return ((DERPrintableString)obj).getString();
082    }
083    
084    public int getNumeric()
085    {
086        return ((ASN1Integer)obj).getValue().intValue();
087    }
088    
089    public ASN1Primitive toASN1Primitive()
090    {    
091        return obj.toASN1Primitive();
092    }
093}