1package org.bouncycastle.asn1.x500;
2
3import org.bouncycastle.asn1.ASN1Encodable;
4import org.bouncycastle.asn1.ASN1EncodableVector;
5import org.bouncycastle.asn1.ASN1Object;
6import org.bouncycastle.asn1.ASN1ObjectIdentifier;
7import org.bouncycastle.asn1.ASN1Primitive;
8import org.bouncycastle.asn1.ASN1Set;
9import org.bouncycastle.asn1.DERSequence;
10import org.bouncycastle.asn1.DERSet;
11
12public class RDN
13    extends ASN1Object
14{
15    private ASN1Set values;
16
17    private RDN(ASN1Set values)
18    {
19        this.values = values;
20    }
21
22    public static RDN getInstance(Object obj)
23    {
24        if (obj instanceof RDN)
25        {
26            return (RDN)obj;
27        }
28        else if (obj != null)
29        {
30            return new RDN(ASN1Set.getInstance(obj));
31        }
32
33        return null;
34    }
35
36    /**
37     * Create a single valued RDN.
38     *
39     * @param oid RDN type.
40     * @param value RDN value.
41     */
42    public RDN(ASN1ObjectIdentifier oid, ASN1Encodable value)
43    {
44        ASN1EncodableVector v = new ASN1EncodableVector();
45
46        v.add(oid);
47        v.add(value);
48
49        this.values = new DERSet(new DERSequence(v));
50    }
51
52    public RDN(AttributeTypeAndValue attrTAndV)
53    {
54        this.values = new DERSet(attrTAndV);
55    }
56
57    /**
58     * Create a multi-valued RDN.
59     *
60     * @param aAndVs attribute type/value pairs making up the RDN
61     */
62    public RDN(AttributeTypeAndValue[] aAndVs)
63    {
64        this.values = new DERSet(aAndVs);
65    }
66
67    public boolean isMultiValued()
68    {
69        return this.values.size() > 1;
70    }
71
72    /**
73     * Return the number of AttributeTypeAndValue objects in this RDN,
74     *
75     * @return size of RDN, greater than 1 if multi-valued.
76     */
77    public int size()
78    {
79        return this.values.size();
80    }
81
82    public AttributeTypeAndValue getFirst()
83    {
84        if (this.values.size() == 0)
85        {
86            return null;
87        }
88
89        return AttributeTypeAndValue.getInstance(this.values.getObjectAt(0));
90    }
91
92    public AttributeTypeAndValue[] getTypesAndValues()
93    {
94        AttributeTypeAndValue[] tmp = new AttributeTypeAndValue[values.size()];
95
96        for (int i = 0; i != tmp.length; i++)
97        {
98            tmp[i] = AttributeTypeAndValue.getInstance(values.getObjectAt(i));
99        }
100
101        return tmp;
102    }
103
104    /**
105     * <pre>
106     * RelativeDistinguishedName ::=
107     *                     SET OF AttributeTypeAndValue
108
109     * AttributeTypeAndValue ::= SEQUENCE {
110     *        type     AttributeType,
111     *        value    AttributeValue }
112     * </pre>
113     * @return this object as an ASN1Primitive type
114     */
115    public ASN1Primitive toASN1Primitive()
116    {
117        return values;
118    }
119}
120