KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ca > commons > security > asn1 > Sequence


1
2 package com.ca.commons.security.asn1;
3
4 /**
5  * This class represents ASN.1 constructed types. It is only used in this
6  * package. It represents a sequence of components, where each component
7  * is an ASN1Object. Components can be added to, removed from, and retrieved
8  * from the Sequence object.
9  *
10  * @author who
11  */

12 public class Sequence extends ASN1Object implements java.io.Serializable JavaDoc
13 {
14
15     private ASN1Object [] component;
16     private int count;
17     private static int initSize = 2;
18     
19     /**
20      * Default constructor.
21      */

22     public Sequence()
23     {}
24     
25     /**
26      * Initializes the Sequence object.
27      */

28     public void init(ASN1Type type)
29     {
30         asn1Type = type;
31         byteArray = null;
32         count = 0;
33         component = new ASN1Object[initSize];
34     }
35     
36     /**
37      * Adds a component to the Sequence object at the end.
38      */

39     public void addComponent(ASN1Object comp)
40     {
41         addComponent(comp, count);
42     }
43     
44     /**
45      * Adds a component to the Sequence object at a given index.
46      */

47     public void addComponent(ASN1Object comp, int index)
48     {
49         if (index > count)
50         {
51             throw new IllegalArgumentException JavaDoc("illegal component index");
52         }
53         if (component.length <= count)
54         {
55         
56             /* need to expand the component array */
57             ASN1Object [] oldComponent = component;
58             component = new ASN1Object[component.length+2];
59             System.arraycopy(oldComponent, 0, component, 0,
60                              oldComponent.length);
61         }
62         if (index < count)
63         {
64             System.arraycopy(component, index, component,
65                              index + 1, count - index);
66         }
67         component[index] = comp;
68         count++;
69     }
70     
71     /**
72      * Removes a component at a given index from the Sequence object.
73      */

74     public void removeComponent(ASN1Object comp, int index)
75     {
76         /* not implemented */
77     }
78     
79     /**
80      * Gets a component at a given index from the Sequence object.
81      */

82     public ASN1Object getComponent(int index)
83     {
84         if (index > count)
85         {
86             throw new IllegalArgumentException JavaDoc("illegal component index");
87         }
88         return component[index];
89     }
90     
91     /**
92      * Sets a component at a given index in the Sequence object.
93      */

94     public void setComponent(ASN1Object comp, int index)
95     {
96         if (index > count)
97         {
98             throw new IllegalArgumentException JavaDoc("illegal component index");
99         }
100         component[index] = comp;
101     }
102     
103     /**
104      * Returns the number of components in the Sequence object.
105      */

106     public int size()
107     {
108         return count;
109     }
110     
111     /* *
112      * Returns whether two objects are the same.
113     public equals(Object o) {
114         if (!(o instanceof iss.security.asn1.Sequence)) {
115             return false;
116         }
117         if (!this.asn1Type.equals(o.asn1Type)) {
118             return false;
119         }
120         if (this.count != o.count) {
121             return false;
122         }
123     
124         for (int i = 0; i < count; i++) {
125             if (!this.component[i].equals(o.component[i])) {
126                 return false;
127             }
128         }
129         return true;
130 }
131      */

132     
133     /**
134      * A string representation of the Sequence object.
135      */

136     public String JavaDoc toString()
137     {
138         String JavaDoc result = super.toString();
139         for (int i = 0; i < count; i++)
140         {
141             result += "\n\t[" + i + "]" + component[i].toString();
142         }
143         return result;
144     }
145 }
146
Popular Tags