KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > lists > S32Vector


1 // Copyright (c) 2001, 2002 Per M.A. Bothner and Brainfood Inc.
2
// This is free software; for terms and warranty disclaimer see ./COPYING.
3

4 package gnu.lists;
5 import java.io.*;
6
7 /** Simple adjustable-length vector of signed 32-bit integers (ints). */
8
9 public class S32Vector extends SimpleVector
10   implements Externalizable
11   /* #ifdef JAVA2 */
12   , Comparable JavaDoc
13   /* #endif */
14 {
15   int[] data;
16   protected static int[] empty = new int[0];
17
18   public S32Vector ()
19   {
20     data = empty;
21   }
22
23   public S32Vector(int size, int value)
24   {
25     int[] array = new int[size];
26     data = array;
27     this.size = size;
28     while (--size >= 0)
29       array[size] = value;
30   }
31
32   public S32Vector(int size)
33   {
34     this.data = new int[size];
35     this.size = size;
36   }
37
38   public S32Vector (int[] data)
39   {
40     this.data = data;
41     size = data.length;
42   }
43
44   public S32Vector(Sequence seq)
45   {
46     data = new int[seq.size()];
47     addAll(seq);
48   }
49
50   /** Get the allocated length of the data buffer. */
51   public int getBufferLength()
52   {
53     return data.length;
54   }
55
56   public void setBufferLength(int length)
57   {
58     int oldLength = data.length;
59     if (oldLength != length)
60       {
61     int[] tmp = new int[length];
62     System.arraycopy(data, 0, tmp, 0,
63              oldLength < length ? oldLength : length);
64     data = tmp;
65       }
66   }
67
68   protected Object JavaDoc getBuffer() { return data; }
69
70   public final int intAt(int index)
71   {
72     if (index > size)
73       throw new IndexOutOfBoundsException JavaDoc();
74     return data[index];
75   }
76
77   public final int intAtBuffer(int index)
78   {
79     return data[index];
80   }
81
82   public final Object JavaDoc get(int index)
83   {
84     if (index > size)
85       throw new IndexOutOfBoundsException JavaDoc();
86     return Convert.toObject(data[index]);
87   }
88
89   public final Object JavaDoc getBuffer(int index)
90   {
91     return Convert.toObject(data[index]);
92   }
93
94   public Object JavaDoc setBuffer(int index, Object JavaDoc value)
95   {
96     int old = data[index];
97     data[index] = Convert.toInt(value);
98     return Convert.toObject(old);
99   }
100
101   public final void setIntAt(int index, int value)
102   {
103     if (index > size)
104       throw new IndexOutOfBoundsException JavaDoc();
105     data[index] = value;
106   }
107
108   public final void setIntAtBuffer(int index, int value)
109   {
110     data[index] = value;
111   }
112
113   protected void clearBuffer(int start, int count)
114   {
115     while (--count >= 0)
116       data[start++] = 0;
117   }
118
119   public int getElementKind()
120   {
121     return INT_S32_VALUE;
122   }
123
124   public String JavaDoc getTag() { return "s32"; }
125
126   public boolean consumeNext (int ipos, Consumer out)
127   {
128     int index = ipos >>> 1;
129     if (index >= size)
130       return false;
131     out.writeInt(data[index]);
132     return true;
133   }
134
135   public void consumePosRange (int iposStart, int iposEnd, Consumer out)
136   {
137     if (out.ignoring())
138       return;
139     int i = iposStart >>> 1;
140     int end = iposEnd >>> 1;
141     if (end > size)
142       end = size;
143     for (; i < end; i++)
144       out.writeInt(data[i]);
145   }
146
147   public int compareTo(Object JavaDoc obj)
148   {
149     return compareToInt(this, (S32Vector) obj);
150   }
151
152   /**
153    * @serialData Write 'size' (using writeInt),
154    * followed by 'size' elements in order (using writeInt).
155    */

156   public void writeExternal(ObjectOutput out) throws IOException
157   {
158     int size = this.size;
159     out.writeInt(size);
160     for (int i = 0; i < size; i++)
161       out.writeInt(data[i]);
162   }
163
164   public void readExternal(ObjectInput in)
165     throws IOException, ClassNotFoundException JavaDoc
166   {
167     int size = in.readInt();
168     int[] data = new int[size];
169     for (int i = 0; i < size; i++)
170       data[i] = in.readInt();
171     this.data = data;
172     this.size = size;
173   }
174 }
175
Popular Tags