package org.bouncycastle.asn1.esf;

import java.math.BigInteger;
import java.text.ParseException;
import java.util.Date;

import org.bouncycastle.asn1.ASN1Encodable;
import org.bouncycastle.asn1.ASN1EncodableVector;
import org.bouncycastle.asn1.ASN1Sequence;
import org.bouncycastle.asn1.DERInteger;
import org.bouncycastle.asn1.DERObject;
import org.bouncycastle.asn1.DERSequence;
import org.bouncycastle.asn1.DERUTCTime;
import org.bouncycastle.asn1.x509.X509Name;

/**
 * <pre>
 *  CrlIdentifier ::= SEQUENCE 
 * {
 *   crlissuer    Name,
 *   crlIssuedTime  UTCTime,
 *   crlNumber    INTEGER OPTIONAL
 * }
 * </pre>
 */
public class CrlIdentifier
    extends ASN1Encodable
{
    private X509Name crlIssuer;
    private DERUTCTime crlIssuedTime;
    private DERInteger crlNumber;

    public static CrlIdentifier getInstance(Object obj)
    {
        if (obj instanceof CrlIdentifier)
        {
            return (CrlIdentifier)obj;
        }
        else if (obj != null)
        {
            return new CrlIdentifier(ASN1Sequence.getInstance(obj));
        }

        throw new IllegalArgumentException("null value in getInstance");
    }

    private CrlIdentifier(ASN1Sequence seq)
    {
        if (seq.size() < 2 || seq.size() > 3)
        {
            throw new IllegalArgumentException();
        }
        this.crlIssuer = X509Name.getInstance(seq.getObjectAt(0));
        this.crlIssuedTime = DERUTCTime.getInstance(seq.getObjectAt(1));
        if (seq.size() > 2)
        {
            this.crlNumber = DERInteger.getInstance(seq.getObjectAt(2));
        }
    }

    public CrlIdentifier(X509Name crlIssuer, Date crlIssuedTime)
    {
        this(crlIssuer, crlIssuedTime, null);
    }

    public CrlIdentifier(X509Name crlIssuer, Date crlIssuedTime,
                         BigInteger crlNumber)
    {
        this.crlIssuer = crlIssuer;
        this.crlIssuedTime = new DERUTCTime(crlIssuedTime);
        if (null != crlNumber)
        {
            this.crlNumber = new DERInteger(crlNumber);
        }
    }

    public X509Name getCrlIssuer()
    {
        return this.crlIssuer;
    }

    public Date getCrlIssuedTime()
    {
        try
        {
            return this.crlIssuedTime.getAdjustedDate();
        }
        catch (ParseException e)
        {
            throw new IllegalStateException("invalid date: " + e.getMessage());
        }
    }

    public BigInteger getCrlNumber()
    {
        if (null == this.crlNumber)
        {
            return null;
        }
        return this.crlNumber.getValue();
    }

    public DERObject toASN1Object()
    {
        ASN1EncodableVector v = new ASN1EncodableVector();
        v.add(this.crlIssuer.toASN1Object());
        v.add(this.crlIssuedTime);
        if (null != this.crlNumber)
        {
            v.add(this.crlNumber);
        }
        return new DERSequence(v);
    }

}

@@ -416,16 +416,60 @@
 TCTime;%0A
+import org.bouncycastle.asn1.x500.X500Name;%0A
 import o
@@ -504,16 +504,16 @@
 09Name;%0A
-
 %0A/**%0A * 
@@ -728,25 +728,25 @@
  private X50
-9
+0
 Name crlIssu
@@ -1375,25 +1375,25 @@
 Issuer = X50
-9
+0
 Name.getInst
@@ -1611,32 +1611,93 @@
        %7D%0A    %7D%0A%0A
+    /**%0A     * @deprecated use X500Name constructor.%0A     */%0A
     public CrlId
@@ -1796,32 +1796,93 @@
 , null);%0A    %7D%0A%0A
+    /**%0A     * @deprecated use X500Name constructor.%0A     */%0A
     public CrlId
@@ -1975,32 +1975,506 @@
 rlNumber)%0A    %7B%0A
+        this.crlIssuer = X500Name.getInstance(crlIssuer);%0A        this.crlIssuedTime = new DERUTCTime(crlIssuedTime);%0A        if (null != crlNumber)%0A        %7B%0A            this.crlNumber = new DERInteger(crlNumber);%0A        %7D%0A    %7D%0A%0A    public CrlIdentifier(X500Name crlIssuer, Date crlIssuedTime)%0A    %7B%0A        this(crlIssuer, crlIssuedTime, null);%0A    %7D%0A%0A    public CrlIdentifier(X500Name crlIssuer, Date crlIssuedTime,%0A                         BigInteger crlNumber)%0A    %7B%0A
         this.crl
@@ -2681,17 +2681,17 @@
 blic X50
-9
+0
 Name get

update to X500Name