001package org.apache.commons.ssl.org.bouncycastle.asn1.x500;
002
003import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Encodable;
004import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1EncodableVector;
005import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Object;
006import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1ObjectIdentifier;
007import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Primitive;
008import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Set;
009import org.apache.commons.ssl.org.bouncycastle.asn1.DERSequence;
010import org.apache.commons.ssl.org.bouncycastle.asn1.DERSet;
011
012public class RDN
013    extends ASN1Object
014{
015    private ASN1Set values;
016
017    private RDN(ASN1Set values)
018    {
019        this.values = values;
020    }
021
022    public static RDN getInstance(Object obj)
023    {
024        if (obj instanceof RDN)
025        {
026            return (RDN)obj;
027        }
028        else if (obj != null)
029        {
030            return new RDN(ASN1Set.getInstance(obj));
031        }
032
033        return null;
034    }
035
036    /**
037     * Create a single valued RDN.
038     *
039     * @param oid RDN type.
040     * @param value RDN value.
041     */
042    public RDN(ASN1ObjectIdentifier oid, ASN1Encodable value)
043    {
044        ASN1EncodableVector v = new ASN1EncodableVector();
045
046        v.add(oid);
047        v.add(value);
048
049        this.values = new DERSet(new DERSequence(v));
050    }
051
052    public RDN(AttributeTypeAndValue attrTAndV)
053    {
054        this.values = new DERSet(attrTAndV);
055    }
056
057    /**
058     * Create a multi-valued RDN.
059     *
060     * @param aAndVs attribute type/value pairs making up the RDN
061     */
062    public RDN(AttributeTypeAndValue[] aAndVs)
063    {
064        this.values = new DERSet(aAndVs);
065    }
066
067    public boolean isMultiValued()
068    {
069        return this.values.size() > 1;
070    }
071
072    /**
073     * Return the number of AttributeTypeAndValue objects in this RDN,
074     *
075     * @return size of RDN, greater than 1 if multi-valued.
076     */
077    public int size()
078    {
079        return this.values.size();
080    }
081
082    public AttributeTypeAndValue getFirst()
083    {
084        if (this.values.size() == 0)
085        {
086            return null;
087        }
088
089        return AttributeTypeAndValue.getInstance(this.values.getObjectAt(0));
090    }
091
092    public AttributeTypeAndValue[] getTypesAndValues()
093    {
094        AttributeTypeAndValue[] tmp = new AttributeTypeAndValue[values.size()];
095
096        for (int i = 0; i != tmp.length; i++)
097        {
098            tmp[i] = AttributeTypeAndValue.getInstance(values.getObjectAt(i));
099        }
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}