summary refs log tree commit diff
path: root/crypto/src/asn1/x509/qualified/MonetaryValue.cs
blob: d703de943fa722340054144f3675005890117939 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
using System;
using System.Collections;

using Org.BouncyCastle.Math;
using Org.BouncyCastle.Utilities;

namespace Org.BouncyCastle.Asn1.X509.Qualified
{
    /**
    * The MonetaryValue object.
    * <pre>
    * MonetaryValue  ::=  SEQUENCE {
    *       currency              Iso4217CurrencyCode,
    *       amount               INTEGER,
    *       exponent             INTEGER }
    * -- value = amount * 10^exponent
    * </pre>
    */
    public class MonetaryValue
        : Asn1Encodable
    {
        internal Iso4217CurrencyCode	currency;
        internal DerInteger				amount;
        internal DerInteger				exponent;

		public static MonetaryValue GetInstance(
            object obj)
        {
            if (obj == null || obj is MonetaryValue)
            {
                return (MonetaryValue) obj;
            }

			if (obj is Asn1Sequence)
            {
                return new MonetaryValue(Asn1Sequence.GetInstance(obj));
            }

			throw new ArgumentException("unknown object in GetInstance: " + Platform.GetTypeName(obj), "obj");
		}

		private MonetaryValue(
            Asn1Sequence seq)
        {
			if (seq.Count != 3)
				throw new ArgumentException("Bad sequence size: " + seq.Count, "seq");

            currency = Iso4217CurrencyCode.GetInstance(seq[0]);
            amount = DerInteger.GetInstance(seq[1]);
            exponent = DerInteger.GetInstance(seq[2]);
        }

		public MonetaryValue(
            Iso4217CurrencyCode	currency,
            int					amount,
            int					exponent)
        {
            this.currency = currency;
            this.amount = new DerInteger(amount);
            this.exponent = new DerInteger(exponent);
        }

		public Iso4217CurrencyCode Currency
		{
			get { return currency; }
		}

		public BigInteger Amount
		{
			get { return amount.Value; }
		}

		public BigInteger Exponent
		{
			get { return exponent.Value; }
		}

		public override Asn1Object ToAsn1Object()
        {
			return new DerSequence(currency, amount, exponent);
        }
    }
}