KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > lists > U16Vector


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

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