KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > util > IntArray


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.util;
30
31 /**
32  * The IntArray is a variable array containing integers. The API follows
33  * the JDK 1.2 collection classes.
34  */

35 public class IntArray {
36   private int []_data;
37   private int _size;
38
39   /**
40    * Create an empty int array.
41    */

42   public IntArray()
43   {
44     _data = new int[16];
45     _size = 0;
46   }
47
48   /**
49    * clear the array, i.e. set the size to 0.
50    */

51   public void clear()
52   {
53     _size = 0;
54   }
55   
56   /**
57    * Returns the current size of the array
58    */

59   public int size()
60   {
61     return _size;
62   }
63
64   /**
65    * Returns the data array.
66    */

67   public int []getArray()
68   {
69     return _data;
70   }
71   
72   /**
73    * Adds an integer to the array.
74    */

75   public void add(int i)
76   {
77     if (_data.length <= _size)
78       expand(_size + 1);
79
80     _data[_size++] = i;
81   }
82   
83   /**
84    * Appends the integers in array to the end of this array.
85    */

86   public void add(IntArray array)
87   {
88     if (_data.length <= _size)
89       expand(_size + array._size);
90
91     for (int i = 0; i < array._size; i++)
92       _data[_size++] = array._data[i];
93   }
94   /**
95    * Inserts an integer into the array.
96    */

97   public void add(int i, int value)
98   {
99     expand(_size + 1);
100
101     System.arraycopy(_data, i, _data, i + 1, _size - i);
102     _data[i] = value;
103     _size++;
104   }
105   
106   /**
107    * Pops the value off the end of the array.
108    */

109   public int pop()
110   {
111     return _data[--_size];
112   }
113   
114   /**
115    * Sets the length of the array, filling with zero if necessary.
116    */

117   public void setLength(int size)
118   {
119     expand(size);
120
121     for (int i = _size; i < size; i++)
122       _data[i] = 0;
123
124     _size = size;
125   }
126   
127   private void expand(int max)
128   {
129     while (_data.length < max) {
130       int []next = new int[_data.length * 2];
131
132       for (int i = 0; i < _data.length; i++)
133     next[i] = _data[i];
134
135       _data = next;
136     }
137   }
138   /**
139    * Gets the integer value at the given index.
140    *
141    * @param i index into the array.
142    * @return value at the index
143    */

144   public int get(int i)
145   {
146     return _data[i];
147   }
148   /**
149    * Returns the last integer in the array.
150    */

151   public int last()
152   {
153     return _data[_size - 1];
154   }
155   /**
156    * Sets the value at the given index.
157    */

158   public void set(int i, int value)
159   {
160     if (_size <= i)
161       throw new IndexOutOfBoundsException JavaDoc(i + " >= " + _size);
162
163     _data[i] = value;
164   }
165   /**
166    * Returns true if the array contains and integer equal to test.
167    */

168   public boolean contains(int test)
169   {
170     int []data = _data;
171     
172     for (int i = _size - 1; i >= 0; i--) {
173       if (data[i] == test)
174     return true;
175     }
176
177     return false;
178   }
179   /**
180    * True if all the integers in subset are contained in the array.
181    */

182   public boolean isSubset(IntArray subset)
183   {
184     int []subData = subset._data;
185     
186     for (int i = subset._size - 1; i >= 0; i--) {
187       if (! contains(subData[i]))
188     return false;
189     }
190
191     return true;
192   }
193   /**
194    * Adds the members of newArray to the list if they are not already
195    * members of the array.
196    */

197   public void union(IntArray newArray)
198   {
199     for (int i = 0; i < newArray._size; i++) {
200       if (! contains(newArray._data[i]))
201     add(newArray._data[i]);
202     }
203   }
204
205   /**
206    * Return a new int array with the contents.
207    */

208   public int []toArray()
209   {
210     int []value = new int[_size];
211     
212     System.arraycopy(_data, 0, value, 0, _size);
213     
214     return value;
215   }
216
217   public String JavaDoc toString()
218   {
219     CharBuffer cb = CharBuffer.allocate();
220
221     cb.append("[");
222     for (int i = 0; i < _size; i++) {
223       if (i != 0)
224     cb.append(", ");
225       cb.append(_data[i]);
226     }
227     cb.append("]");
228
229     return cb.close();
230   }
231 }
232
Popular Tags