001: package org.bouncycastle.asn1.tsp;
002:
003: import org.bouncycastle.asn1.ASN1Encodable;
004: import org.bouncycastle.asn1.ASN1EncodableVector;
005: import org.bouncycastle.asn1.DERInteger;
006: import org.bouncycastle.asn1.DERTaggedObject;
007: import org.bouncycastle.asn1.DERSequence;
008: import org.bouncycastle.asn1.ASN1Sequence;
009: import org.bouncycastle.asn1.DERObject;
010:
011: public class Accuracy extends ASN1Encodable {
012: DERInteger seconds;
013:
014: DERInteger millis;
015:
016: DERInteger micros;
017:
018: // constantes
019: protected static final int MIN_MILLIS = 1;
020:
021: protected static final int MAX_MILLIS = 999;
022:
023: protected static final int MIN_MICROS = 1;
024:
025: protected static final int MAX_MICROS = 999;
026:
027: protected Accuracy() {
028: }
029:
030: public Accuracy(DERInteger seconds, DERInteger millis,
031: DERInteger micros) {
032: this .seconds = seconds;
033:
034: //Verifications
035: if (millis != null
036: && (millis.getValue().intValue() < MIN_MILLIS || millis
037: .getValue().intValue() > MAX_MILLIS)) {
038: throw new IllegalArgumentException(
039: "Invalid millis field : not in (1..999)");
040: } else {
041: this .millis = millis;
042: }
043:
044: if (micros != null
045: && (micros.getValue().intValue() < MIN_MICROS || micros
046: .getValue().intValue() > MAX_MICROS)) {
047: throw new IllegalArgumentException(
048: "Invalid micros field : not in (1..999)");
049: } else {
050: this .micros = micros;
051: }
052:
053: }
054:
055: public Accuracy(ASN1Sequence seq) {
056: seconds = null;
057: millis = null;
058: micros = null;
059:
060: for (int i = 0; i < seq.size(); i++) {
061: // seconds
062: if (seq.getObjectAt(i) instanceof DERInteger) {
063: seconds = (DERInteger) seq.getObjectAt(i);
064: } else if (seq.getObjectAt(i) instanceof DERTaggedObject) {
065: DERTaggedObject extra = (DERTaggedObject) seq
066: .getObjectAt(i);
067:
068: switch (extra.getTagNo()) {
069: case 0:
070: millis = DERInteger.getInstance(extra, false);
071: if (millis.getValue().intValue() < MIN_MILLIS
072: || millis.getValue().intValue() > MAX_MILLIS) {
073: throw new IllegalArgumentException(
074: "Invalid millis field : not in (1..999).");
075: }
076: break;
077: case 1:
078: micros = DERInteger.getInstance(extra, false);
079: if (micros.getValue().intValue() < MIN_MICROS
080: || micros.getValue().intValue() > MAX_MICROS) {
081: throw new IllegalArgumentException(
082: "Invalid micros field : not in (1..999).");
083: }
084: break;
085: default:
086: throw new IllegalArgumentException(
087: "Invalig tag number");
088: }
089: }
090: }
091: }
092:
093: public static Accuracy getInstance(Object o) {
094: if (o == null || o instanceof Accuracy) {
095: return (Accuracy) o;
096: } else if (o instanceof ASN1Sequence) {
097: return new Accuracy((ASN1Sequence) o);
098: }
099:
100: throw new IllegalArgumentException(
101: "Unknown object in 'Accuracy' factory : "
102: + o.getClass().getName() + ".");
103: }
104:
105: public DERInteger getSeconds() {
106: return seconds;
107: }
108:
109: public DERInteger getMillis() {
110: return millis;
111: }
112:
113: public DERInteger getMicros() {
114: return micros;
115: }
116:
117: /**
118: * <pre>
119: * Accuracy ::= SEQUENCE {
120: * seconds INTEGER OPTIONAL,
121: * millis [0] INTEGER (1..999) OPTIONAL,
122: * micros [1] INTEGER (1..999) OPTIONAL
123: * }
124: * </pre>
125: */
126: public DERObject toASN1Object() {
127:
128: ASN1EncodableVector v = new ASN1EncodableVector();
129:
130: if (seconds != null) {
131: v.add(seconds);
132: }
133:
134: if (millis != null) {
135: v.add(new DERTaggedObject(false, 0, millis));
136: }
137:
138: if (micros != null) {
139: v.add(new DERTaggedObject(false, 1, micros));
140: }
141:
142: return new DERSequence(v);
143: }
144: }
|