1 package org.bouncycastle.asn1;
6 public abstract class ASN1Sequence
9 private Vector seq = new Vector();
12 * return an ASN1Sequence from the given object.
14 * @param obj the object we want converted.
15 * @exception IllegalArgumentException if the object cannot be converted.
17 public static ASN1Sequence getInstance(
20 if (obj == null || obj instanceof ASN1Sequence)
22 return (ASN1Sequence)obj;
25 throw new IllegalArgumentException("unknown object in getInstance");
29 * Return an ASN1 sequence from a tagged object. There is a special
30 * case here, if an object appears to have been explicitly tagged on
31 * reading but we were expecting it to be implictly tagged in the
32 * normal course of events it indicates that we lost the surrounding
33 * sequence - so we need to add it back (this will happen if the tagged
34 * object is a sequence that contains other sequences). If you are
35 * dealing with implicitly tagged sequences you really <b>should</b>
36 * be using this method.
38 * @param obj the tagged object.
39 * @param explicit true if the object is meant to be explicitly tagged,
41 * @exception IllegalArgumentException if the tagged object cannot
44 public static ASN1Sequence getInstance(
50 if (!obj.isExplicit())
52 throw new IllegalArgumentException("object implicit - explicit expected.");
55 return (ASN1Sequence)obj.getObject();
60 // constructed object which appears to be explicitly tagged
61 // when it should be implicit means we have to add the
62 // surrounding sequence.
68 if (obj instanceof BERTaggedObject)
70 seq = new BERConstructedSequence();
74 seq = new DERConstructedSequence();
77 seq.addObject(obj.getObject());
85 if (obj.getObject() instanceof ASN1Sequence)
87 return (ASN1Sequence)obj.getObject();
92 throw new IllegalArgumentException(
93 "unknown object in getInstanceFromTagged");
96 public Enumeration getObjects()
98 return seq.elements();
102 * return the object at the sequence postion indicated by index.
104 * @param the sequence number (starting at zero) of the object
105 * @return the object at the sequence postion indicated by index.
107 public DEREncodable getObjectAt(
110 return (DEREncodable)seq.elementAt(index);
114 * return the number of objects in this sequence.
116 * @return the number of objects in this sequence.
123 public int hashCode()
125 Enumeration e = this.getObjects();
128 while (e.hasMoreElements())
130 hashCode ^= e.nextElement().hashCode();
136 public boolean equals(
139 if (o == null || !(o instanceof ASN1Sequence))
144 ASN1Sequence other = (ASN1Sequence)o;
146 if (this.size() != other.size())
151 Enumeration s1 = this.getObjects();
152 Enumeration s2 = other.getObjects();
154 while (s1.hasMoreElements())
156 if (!s1.nextElement().equals(s2.nextElement()))
165 protected void addObject(
171 abstract void encode(DEROutputStream out)