001package org.apache.commons.ssl.org.bouncycastle.asn1.tsp; 002 003import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1EncodableVector; 004import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Integer; 005import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Object; 006import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Primitive; 007import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Sequence; 008import org.apache.commons.ssl.org.bouncycastle.asn1.DERSequence; 009import org.apache.commons.ssl.org.bouncycastle.asn1.DERTaggedObject; 010 011 012public class Accuracy 013 extends ASN1Object 014{ 015 ASN1Integer seconds; 016 017 ASN1Integer millis; 018 019 ASN1Integer micros; 020 021 // constantes 022 protected static final int MIN_MILLIS = 1; 023 024 protected static final int MAX_MILLIS = 999; 025 026 protected static final int MIN_MICROS = 1; 027 028 protected static final int MAX_MICROS = 999; 029 030 protected Accuracy() 031 { 032 } 033 034 public Accuracy( 035 ASN1Integer seconds, 036 ASN1Integer millis, 037 ASN1Integer micros) 038 { 039 this.seconds = seconds; 040 041 //Verifications 042 if (millis != null 043 && (millis.getValue().intValue() < MIN_MILLIS || millis 044 .getValue().intValue() > MAX_MILLIS)) 045 { 046 throw new IllegalArgumentException( 047 "Invalid millis field : not in (1..999)"); 048 } 049 else 050 { 051 this.millis = millis; 052 } 053 054 if (micros != null 055 && (micros.getValue().intValue() < MIN_MICROS || micros 056 .getValue().intValue() > MAX_MICROS)) 057 { 058 throw new IllegalArgumentException( 059 "Invalid micros field : not in (1..999)"); 060 } 061 else 062 { 063 this.micros = micros; 064 } 065 066 } 067 068 private Accuracy(ASN1Sequence seq) 069 { 070 seconds = null; 071 millis = null; 072 micros = null; 073 074 for (int i = 0; i < seq.size(); i++) 075 { 076 // seconds 077 if (seq.getObjectAt(i) instanceof ASN1Integer) 078 { 079 seconds = (ASN1Integer) seq.getObjectAt(i); 080 } 081 else if (seq.getObjectAt(i) instanceof DERTaggedObject) 082 { 083 DERTaggedObject extra = (DERTaggedObject) seq.getObjectAt(i); 084 085 switch (extra.getTagNo()) 086 { 087 case 0: 088 millis = ASN1Integer.getInstance(extra, false); 089 if (millis.getValue().intValue() < MIN_MILLIS 090 || millis.getValue().intValue() > MAX_MILLIS) 091 { 092 throw new IllegalArgumentException( 093 "Invalid millis field : not in (1..999)."); 094 } 095 break; 096 case 1: 097 micros = ASN1Integer.getInstance(extra, false); 098 if (micros.getValue().intValue() < MIN_MICROS 099 || 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 instanceof Accuracy) 115 { 116 return (Accuracy) o; 117 } 118 119 if (o != null) 120 { 121 return new Accuracy(ASN1Sequence.getInstance(o)); 122 } 123 124 return null; 125 } 126 127 public ASN1Integer getSeconds() 128 { 129 return seconds; 130 } 131 132 public ASN1Integer getMillis() 133 { 134 return millis; 135 } 136 137 public ASN1Integer getMicros() 138 { 139 return micros; 140 } 141 142 /** 143 * <pre> 144 * Accuracy ::= SEQUENCE { 145 * seconds INTEGER OPTIONAL, 146 * millis [0] INTEGER (1..999) OPTIONAL, 147 * micros [1] INTEGER (1..999) OPTIONAL 148 * } 149 * </pre> 150 */ 151 public ASN1Primitive toASN1Primitive() 152 { 153 154 ASN1EncodableVector v = new ASN1EncodableVector(); 155 156 if (seconds != null) 157 { 158 v.add(seconds); 159 } 160 161 if (millis != null) 162 { 163 v.add(new DERTaggedObject(false, 0, millis)); 164 } 165 166 if (micros != null) 167 { 168 v.add(new DERTaggedObject(false, 1, micros)); 169 } 170 171 return new DERSequence(v); 172 } 173}