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