KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > chaperon > common > IntegerList


1 /*
2  * Copyright (C) Chaperon. All rights reserved.
3  * -------------------------------------------------------------------------
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE file.
7  */

8
9 package net.sourceforge.chaperon.common;
10
11 /**
12  * This class represents a list of integers values.
13  *
14  * @author <a HREF="mailto:stephan@apache.org">Stephan Michels</a>
15  * @version CVS $Id: IntegerList.java,v 1.4 2003/12/09 19:55:52 benedikta Exp $
16  */

17 public class IntegerList implements IntegerCollection
18 {
19   private int capacityIncrement = 100;
20   private int elementCount = 0;
21   private int[] list = new int[10];
22   private int dummy;
23
24   /**
25    * Constructs a empty list of integer values.
26    */

27   public IntegerList() {}
28
29   /**
30    * Add a value to the list.
31    *
32    * @param value Integer value, which should be added.
33    *
34    * @return Index of this value.
35    */

36   public int add(int value)
37   {
38     ensureCapacity(elementCount+1);
39     list[elementCount++] = value;
40     return elementCount-1;
41   }
42
43   /**
44    * Add a collection of values to this list.
45    *
46    * @param collection Collection of values.
47    */

48   public void add(IntegerCollection collection)
49   {
50     for (int i = 0; i<collection.getCount(); i++)
51       add(collection.get(i));
52   }
53
54   /**
55    * Add the values of an array to this list.
56    *
57    * @param array Array of integer values.
58    */

59   public void add(int[] array)
60   {
61     for (int i = 0; i<array.length; i++)
62       add(array[i]);
63   }
64
65   /**
66    * Removes a value giving by an index.
67    *
68    * @param index Index of the integer value.
69    */

70   public void remove(int index)
71   {
72     if (index>=elementCount)
73       throw new ArrayIndexOutOfBoundsException JavaDoc(index);
74
75     elementCount--;
76     if (index<elementCount)
77       System.arraycopy(list, index+1, list, index, elementCount-index);
78
79     list[elementCount] = 0;
80   }
81
82   /**
83    * Replace a value given by an index.
84    *
85    * @param index Index of the value, which should be replaced.
86    * @param value The new value.
87    */

88   public void set(int index, int value)
89   {
90     if (index>=elementCount)
91       throw new ArrayIndexOutOfBoundsException JavaDoc(index);
92
93     list[index] = value;
94   }
95
96   /**
97    * Return a value from this list given by an index.
98    *
99    * @param index Index of the value.
100    *
101    * @return Integer value.
102    */

103   public int get(int index)
104   {
105     if (index>=elementCount)
106       throw new ArrayIndexOutOfBoundsException JavaDoc(index);
107
108     return list[index];
109   }
110
111   /**
112    * Returns the count of values in this list
113    *
114    * @return Count of integer values.
115    */

116   public int getCount()
117   {
118     return elementCount;
119   }
120
121   /**
122    * Return the index of a value, otherwise -1.
123    *
124    * @param value Value, which should found in this list.
125    *
126    * @return Index of this value.
127    */

128   public int indexOf(int value)
129   {
130     for (int i = 0; i<elementCount; i++)
131       if (list[i]==value)
132         return i;
133
134     return -1;
135   }
136
137   /**
138    * If the list contains a value.
139    *
140    * @param value Value, which should be found in this list.
141    *
142    * @return True, if this list contains the value.
143    */

144   public boolean contains(int value)
145   {
146     for (int i = 0; i<elementCount; i++)
147       if (list[i]==value)
148         return true;
149
150     return false;
151   }
152
153   /**
154    * Removes a value from this list.
155    *
156    * @param value Value to remove.
157    */

158   public void removeValue(int value)
159   {
160     int index;
161
162     while ((index = indexOf(value))!=-1)
163       remove(index);
164   }
165
166   /**
167    * If this list contains no values.
168    *
169    * @return True, if this list is empty.
170    */

171   public boolean isEmpty()
172   {
173     return (elementCount<=0);
174   }
175
176   /**
177    * Pops a value of the end of this list.
178    *
179    * @return Value from the end of this list.
180    */

181   public int pop()
182   {
183     if (0>=elementCount)
184       throw new java.util.EmptyStackException JavaDoc();
185
186     return list[--elementCount];
187   }
188
189   /**
190    * Pushs a value on top to this list
191    *
192    * @param value Value, which should be added.
193    */

194   public void push(int value)
195   {
196     add(value);
197   }
198
199   /**
200    * Push values from an array.
201    *
202    * @param values Array of integer values.
203    */

204   public void push(int[] values)
205   {
206     add(values);
207   }
208
209   /**
210    * Peek a value of the end of this list.
211    *
212    * @return Integer value.
213    */

214   public int peek()
215   {
216     if (0>=elementCount)
217       throw new java.util.EmptyStackException JavaDoc();
218
219     return list[elementCount-1];
220   }
221
222   /**
223    * Removes all values from this list.
224    */

225   public void clear()
226   {
227     elementCount = 0;
228   }
229
230   /**
231    * Swaps two values from this list.
232    *
233    * @param index1 Index from the first value.
234    * @param index2 Index from the second value.
235    */

236   public void swap(int index1, int index2)
237   {
238     dummy = list[index1];
239     list[index1] = list[index2];
240     list[index2] = dummy;
241   }
242
243   /**
244    * Return a string representation of the collection.
245    *
246    * @return String representation of the collection.
247    */

248   public String JavaDoc toString()
249   {
250     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
251
252     buffer.append("[");
253     for (int i = 0; i<elementCount; i++)
254     {
255       buffer.append(String.valueOf(list[i]));
256       if (i<(elementCount-1))
257         buffer.append(",");
258     }
259
260     buffer.append("]");
261     return buffer.toString();
262   }
263
264   /**
265    * Ensure the capacity for adding values
266    *
267    * @param minCapacity Minimum capacity.
268    */

269   private void ensureCapacity(int minCapacity)
270   {
271     if (list.length>=minCapacity)
272       return;
273
274     int newCapacity = list.length+capacityIncrement;
275
276     if (capacityIncrement<=0)
277       newCapacity = list.length*2;
278
279     int[] newArray = new int[Math.max(newCapacity, minCapacity)];
280
281     System.arraycopy(list, 0, newArray, 0, list.length);
282     list = newArray;
283   }
284 }
285
Popular Tags