01: package org.bouncycastle.asn1.x509.qualified;
02:
03: import org.bouncycastle.asn1.ASN1Choice;
04: import org.bouncycastle.asn1.ASN1Encodable;
05: import org.bouncycastle.asn1.DEREncodable;
06: import org.bouncycastle.asn1.DERInteger;
07: import org.bouncycastle.asn1.DERObject;
08: import org.bouncycastle.asn1.DERPrintableString;
09:
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: */
20: public class Iso4217CurrencyCode extends ASN1Encodable implements
21: ASN1Choice {
22: final int ALPHABETIC_MAXSIZE = 3;
23: final int NUMERIC_MINSIZE = 1;
24: final int NUMERIC_MAXSIZE = 999;
25:
26: DEREncodable obj;
27: int numeric;
28:
29: public static Iso4217CurrencyCode getInstance(Object obj) {
30: if (obj == null || obj instanceof Iso4217CurrencyCode) {
31: return (Iso4217CurrencyCode) obj;
32: }
33:
34: if (obj instanceof DERInteger) {
35: DERInteger numericobj = DERInteger.getInstance(obj);
36: int numeric = numericobj.getValue().intValue();
37: return new Iso4217CurrencyCode(numeric);
38: } else if (obj instanceof DERPrintableString) {
39: DERPrintableString alphabetic = DERPrintableString
40: .getInstance(obj);
41: return new Iso4217CurrencyCode(alphabetic.getString());
42: }
43: throw new IllegalArgumentException(
44: "unknown object in getInstance");
45: }
46:
47: public Iso4217CurrencyCode(int numeric) {
48: if (numeric > NUMERIC_MAXSIZE || numeric < NUMERIC_MINSIZE) {
49: throw new IllegalArgumentException(
50: "wrong size in numeric code : not in ("
51: + NUMERIC_MINSIZE + ".." + NUMERIC_MAXSIZE
52: + ")");
53: }
54: obj = new DERInteger(numeric);
55: }
56:
57: public Iso4217CurrencyCode(String alphabetic) {
58: if (alphabetic.length() > ALPHABETIC_MAXSIZE) {
59: throw new IllegalArgumentException(
60: "wrong size in alphabetic code : max size is "
61: + ALPHABETIC_MAXSIZE);
62: }
63: obj = new DERPrintableString(alphabetic);
64: }
65:
66: public boolean isAlphabetic() {
67: return obj instanceof DERPrintableString;
68: }
69:
70: public String getAlphabetic() {
71: return ((DERPrintableString) obj).getString();
72: }
73:
74: public int getNumeric() {
75: return ((DERInteger) obj).getValue().intValue();
76: }
77:
78: public DERObject toASN1Object() {
79: return obj.getDERObject();
80: }
81: }
|