KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > bc > asn1 > ASN1Set


1 package com.lowagie.bc.asn1;
2
3 import java.io.IOException;
4 import java.util.Enumeration;
5 import java.util.Vector;
6
7 abstract public class ASN1Set
8     extends DERObject
9 {
10     protected Vector set = new Vector();
11
12     /**
13      * return an ASN1Set from the given object.
14      *
15      * @param obj the object we want converted.
16      * @exception IllegalArgumentException if the object cannot be converted.
17      */

18     public static ASN1Set getInstance(
19         Object obj)
20     {
21         if (obj == null || obj instanceof ASN1Set)
22         {
23             return (ASN1Set)obj;
24         }
25
26         throw new IllegalArgumentException("unknown object in getInstance");
27     }
28
29     /**
30      * Return an ASN1 set from a tagged object. There is a special
31      * case here, if an object appears to have been explicitly tagged on
32      * reading but we were expecting it to be implictly tagged in the
33      * normal course of events it indicates that we lost the surrounding
34      * set - so we need to add it back (this will happen if the tagged
35      * object is a sequence that contains other sequences). If you are
36      * dealing with implicitly tagged sets you really <b>should</b>
37      * be using this method.
38      *
39      * @param obj the tagged object.
40      * @param explicit true if the object is meant to be explicitly tagged
41      * false otherwise.
42      * @exception IllegalArgumentException if the tagged object cannot
43      * be converted.
44      */

45     public static ASN1Set getInstance(
46         ASN1TaggedObject obj,
47         boolean explicit)
48     {
49         if (explicit)
50         {
51             if (!obj.isExplicit())
52             {
53                 throw new IllegalArgumentException("object implicit - explicit expected.");
54             }
55
56             return (ASN1Set)obj.getObject();
57         }
58         else
59         {
60             //
61
// constructed object which appears to be explicitly tagged
62
// and it's really implicit means we have to add the
63
// surrounding sequence.
64
//
65
if (obj.isExplicit())
66             {
67                 ASN1Set set = new DERSet(obj.getObject());
68
69                 return set;
70             }
71             else
72             {
73                 if (obj.getObject() instanceof ASN1Set)
74                 {
75                     return (ASN1Set)obj.getObject();
76                 }
77
78                 //
79
// in this case the parser returns a sequence, convert it
80
// into a set.
81
//
82
ASN1EncodableVector v = new ASN1EncodableVector();
83
84                 if (obj.getObject() instanceof ASN1Sequence)
85                 {
86                     ASN1Sequence s = (ASN1Sequence)obj.getObject();
87                     Enumeration e = s.getObjects();
88
89                     while (e.hasMoreElements())
90                     {
91                         v.add((DEREncodable)e.nextElement());
92                     }
93
94                     return new DERSet(v);
95                 }
96             }
97         }
98
99         throw new IllegalArgumentException(
100                     "unknown object in getInstanceFromTagged");
101     }
102
103     public ASN1Set()
104     {
105     }
106
107     public Enumeration getObjects()
108     {
109         return set.elements();
110     }
111
112     /**
113      * return the object at the set postion indicated by index.
114      *
115      * @param index the set number (starting at zero) of the object
116      * @return the object at the set postion indicated by index.
117      */

118     public DEREncodable getObjectAt(
119         int index)
120     {
121         return (DEREncodable)set.elementAt(index);
122     }
123
124     /**
125      * return the number of objects in this set.
126      *
127      * @return the number of objects in this set.
128      */

129     public int size()
130     {
131         return set.size();
132     }
133
134     public int hashCode()
135     {
136         Enumeration e = this.getObjects();
137         int hashCode = 0;
138
139         while (e.hasMoreElements())
140         {
141             hashCode ^= e.nextElement().hashCode();
142         }
143
144         return hashCode;
145     }
146
147     public boolean equals(
148         Object o)
149     {
150         if (o == null || !(o instanceof ASN1Set))
151         {
152             return false;
153         }
154
155         ASN1Set other = (ASN1Set)o;
156
157         if (this.size() != other.size())
158         {
159             return false;
160         }
161
162         Enumeration s1 = this.getObjects();
163         Enumeration s2 = other.getObjects();
164
165         while (s1.hasMoreElements())
166         {
167             if (!s1.nextElement().equals(s2.nextElement()))
168             {
169                 return false;
170             }
171         }
172
173         return true;
174     }
175
176     protected void addObject(
177         DEREncodable obj)
178     {
179         set.addElement(obj);
180     }
181
182     abstract void encode(DEROutputStream out)
183             throws IOException;
184 }
185
Popular Tags