Home | History | Annotate | Download | only in tsp
      1 package org.bouncycastle.asn1.tsp;
      2 
      3 import org.bouncycastle.asn1.ASN1Encodable;
      4 import org.bouncycastle.asn1.ASN1EncodableVector;
      5 import org.bouncycastle.asn1.DERInteger;
      6 import org.bouncycastle.asn1.DERTaggedObject;
      7 import org.bouncycastle.asn1.DERSequence;
      8 import org.bouncycastle.asn1.ASN1Sequence;
      9 import org.bouncycastle.asn1.DERObject;
     10 
     11 
     12 public class Accuracy
     13     extends ASN1Encodable
     14 {
     15     DERInteger seconds;
     16 
     17     DERInteger millis;
     18 
     19     DERInteger micros;
     20 
     21     // constantes
     22     protected static final int MIN_MILLIS = 1;
     23 
     24     protected static final int MAX_MILLIS = 999;
     25 
     26     protected static final int MIN_MICROS = 1;
     27 
     28     protected static final int MAX_MICROS = 999;
     29 
     30     protected Accuracy()
     31     {
     32     }
     33 
     34     public Accuracy(
     35         DERInteger seconds,
     36         DERInteger millis,
     37         DERInteger micros)
     38     {
     39         this.seconds = seconds;
     40 
     41         //Verifications
     42         if (millis != null
     43                 && (millis.getValue().intValue() < MIN_MILLIS || millis
     44                         .getValue().intValue() > MAX_MILLIS))
     45         {
     46             throw new IllegalArgumentException(
     47                     "Invalid millis field : not in (1..999)");
     48         }
     49         else
     50         {
     51             this.millis = millis;
     52         }
     53 
     54         if (micros != null
     55                 && (micros.getValue().intValue() < MIN_MICROS || micros
     56                         .getValue().intValue() > MAX_MICROS))
     57         {
     58             throw new IllegalArgumentException(
     59                     "Invalid micros field : not in (1..999)");
     60         }
     61         else
     62         {
     63             this.micros = micros;
     64         }
     65 
     66     }
     67 
     68     public Accuracy(ASN1Sequence seq)
     69     {
     70         seconds = null;
     71         millis = null;
     72         micros = null;
     73 
     74         for (int i = 0; i < seq.size(); i++)
     75         {
     76             // seconds
     77             if (seq.getObjectAt(i) instanceof DERInteger)
     78             {
     79                 seconds = (DERInteger) seq.getObjectAt(i);
     80             }
     81             else if (seq.getObjectAt(i) instanceof DERTaggedObject)
     82             {
     83                 DERTaggedObject extra = (DERTaggedObject) seq.getObjectAt(i);
     84 
     85                 switch (extra.getTagNo())
     86                 {
     87                 case 0:
     88                     millis = DERInteger.getInstance(extra, false);
     89                     if (millis.getValue().intValue() < MIN_MILLIS
     90                             || millis.getValue().intValue() > MAX_MILLIS)
     91                     {
     92                         throw new IllegalArgumentException(
     93                                 "Invalid millis field : not in (1..999).");
     94                     }
     95                     break;
     96                 case 1:
     97                     micros = DERInteger.getInstance(extra, false);
     98                     if (micros.getValue().intValue() < MIN_MICROS
     99                             || micros.getValue().intValue() > MAX_MICROS)
    100                     {
    101                         throw new IllegalArgumentException(
    102                                 "Invalid micros field : not in (1..999).");
    103                     }
    104                     break;
    105                 default:
    106                     throw new IllegalArgumentException("Invalig tag number");
    107                 }
    108             }
    109         }
    110     }
    111 
    112     public static Accuracy getInstance(Object o)
    113     {
    114         if (o == null || o instanceof Accuracy)
    115         {
    116             return (Accuracy) o;
    117         }
    118         else if (o instanceof ASN1Sequence)
    119         {
    120             return new Accuracy((ASN1Sequence) o);
    121         }
    122 
    123         throw new IllegalArgumentException(
    124                 "Unknown object in 'Accuracy' factory : "
    125                         + o.getClass().getName() + ".");
    126     }
    127 
    128     public DERInteger getSeconds()
    129     {
    130         return seconds;
    131     }
    132 
    133     public DERInteger getMillis()
    134     {
    135         return millis;
    136     }
    137 
    138     public DERInteger getMicros()
    139     {
    140         return micros;
    141     }
    142 
    143     /**
    144      * <pre>
    145      * Accuracy ::= SEQUENCE {
    146      *             seconds        INTEGER              OPTIONAL,
    147      *             millis     [0] INTEGER  (1..999)    OPTIONAL,
    148      *             micros     [1] INTEGER  (1..999)    OPTIONAL
    149      *             }
    150      * </pre>
    151      */
    152     public DERObject toASN1Object()
    153     {
    154 
    155         ASN1EncodableVector v = new ASN1EncodableVector();
    156 
    157         if (seconds != null)
    158         {
    159             v.add(seconds);
    160         }
    161 
    162         if (millis != null)
    163         {
    164             v.add(new DERTaggedObject(false, 0, millis));
    165         }
    166 
    167         if (micros != null)
    168         {
    169             v.add(new DERTaggedObject(false, 1, micros));
    170         }
    171 
    172         return new DERSequence(v);
    173     }
    174 }
    175