KickJava   Java API By Example, From Geeks To Geeks.

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


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 set of integer values, which means there doesn't exist multiple
13  * instances of values.
14  *
15  * @author <a HREF="mailto:stephan@apache.org">Stephan Michels</a>
16  * @version CVS $Id: IntegerSet.java,v 1.3 2003/12/09 19:55:52 benedikta Exp $
17  */

18 public class IntegerSet implements IntegerCollection
19 {
20   private int capacityIncrement = 100;
21   private int elementCount = 0;
22   private int[] list = new int[10];
23   private int dummy;
24
25   /**
26    * Creates an empty set of integer values.
27    */

28   public IntegerSet() {}
29
30   /**
31    * Add a value to this set.
32    *
33    * @param value Integer value.
34    *
35    * @return Index of this value.
36    */

37   public int add(int value)
38   {
39     int index = indexOf(value);
40
41     if (index==-1)
42     {
43       ensureCapacity(elementCount+1);
44       list[elementCount++] = value;
45       return elementCount-1;
46     }
47
48     return index;
49   }
50
51   /**
52    * Add the values of an other integer collection to this set.
53    *
54    * @param collection Collection of values.
55    */

56   public void add(IntegerCollection collection)
57   {
58     for (int i = 0; i<collection.getCount(); i++)
59       add(collection.get(i));
60   }
61
62   /**
63    * Add the values of an array to this set.
64    *
65    * @param array Array of integer values.
66    */

67   public void add(int[] array)
68   {
69     for (int i = 0; i<array.length; i++)
70       add(array[i]);
71   }
72
73   /**
74    * Removes a value giving by an index.
75    *
76    * @param index Index of the integer value.
77    */

78   public void remove(int index)
79   {
80     if (index>=elementCount)
81       throw new ArrayIndexOutOfBoundsException JavaDoc(index);
82
83     elementCount--;
84     if (index<elementCount)
85       System.arraycopy(list, index+1, list, index, elementCount-index);
86
87     list[elementCount] = 0;
88   }
89
90   /**
91    * Replace a value given by an index.
92    *
93    * @param index Index of the value, which should be replaced.
94    * @param value The new value.
95    */

96   public void set(int index, int value)
97   {
98     if (index>=elementCount)
99       throw new ArrayIndexOutOfBoundsException JavaDoc(index);
100
101     if (contains(value) && (!(get(index)==value)))
102       throw new IllegalArgumentException JavaDoc("Set already contains the value");
103
104     list[index] = value;
105   }
106
107   /**
108    * Return a value from this set given by an index.
109    *
110    * @param index Index of the value.
111    *
112    * @return Integer value.
113    */

114   public int get(int index)
115   {
116     if (index>=elementCount)
117       throw new ArrayIndexOutOfBoundsException JavaDoc(index);
118
119     return list[index];
120   }
121
122   /**
123    * Returns the count of value in this set.
124    *
125    * @return Count of integer values.
126    */

127   public int getCount()
128   {
129     return elementCount;
130   }
131
132   /**
133    * Return the index of a value, otherwise -1
134    *
135    * @param value Value, which should be found in this set.
136    *
137    * @return Index of this value.
138    */

139   public int indexOf(int value)
140   {
141     for (int i = 0; i<elementCount; i++)
142     {
143       if (list[i]==value)
144         return i;
145     }
146
147     return -1;
148   }
149
150   /**
151    * If the list contains a value.
152    *
153    * @param value Value, which should be found in this set
154    *
155    * @return True, if this set contains the value.
156    */

157   public boolean contains(int value)
158   {
159     for (int i = 0; i<elementCount; i++)
160     {
161       if (list[i]==value)
162         return true;
163     }
164
165     return false;
166   }
167
168   /**
169    * Removes a value from this set.
170    *
171    * @param value Value to remove.
172    */

173   public void removeValue(int value)
174   {
175     int index;
176
177     while ((index = indexOf(value))!=-1)
178       remove(index);
179   }
180
181   /**
182    * If this set contains no values.
183    *
184    * @return True, if this set is empty.
185    */

186   public boolean isEmpty()
187   {
188     return (elementCount<=0);
189   }
190
191   /**
192    * Pops a value of the end of this set.
193    *
194    * @return Value from the end of this set.
195    */

196   public int pop()
197   {
198     if (0>=elementCount)
199       throw new java.util.EmptyStackException JavaDoc();
200
201     return list[--elementCount];
202   }
203
204   /**
205    * Pushs a value to this list.
206    *
207    * @param value Value, which should be added.
208    */

209   public void push(int value)
210   {
211     add(value);
212   }
213
214   /**
215    * Push values from an array.
216    *
217    * @param values Array of integer values.
218    */

219   public void push(int[] values)
220   {
221     add(values);
222   }
223
224   /**
225    * Peek a value of the end of this set.
226    *
227    * @return Integer value.
228    */

229   public int peek()
230   {
231     if (0>=elementCount)
232       throw new java.util.EmptyStackException JavaDoc();
233
234     return list[elementCount-1];
235   }
236
237   /**
238    * Removes all values from this set
239    */

240   public void clear()
241   {
242     elementCount = 0;
243   }
244
245   /**
246    * Swaps two values from this set.
247    *
248    * @param index1 Index from the first value.
249    * @param index2 Index from the second value.
250    */

251   public void swap(int index1, int index2)
252   {
253     dummy = list[index1];
254     list[index1] = list[index2];
255     list[index2] = dummy;
256   }
257
258   /**
259    * Return a string representation of the collection.
260    *
261    * @return String representation of the collection.
262    */

263   public String JavaDoc toString()
264   {
265     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
266
267     buffer.append("[");
268     for (int i = 0; i<elementCount; i++)
269     {
270       buffer.append(String.valueOf(list[i]));
271       if (i<(elementCount-1))
272         buffer.append(",");
273     }
274
275     buffer.append("]");
276     return buffer.toString();
277   }
278
279   /**
280    * Ensure the capacity for adding values.
281    *
282    * @param minCapacity Minimum capacity.
283    */

284   private void ensureCapacity(int minCapacity)
285   {
286     if (list.length>=minCapacity)
287       return;
288
289     int newCapacity = list.length+capacityIncrement;
290
291     if (capacityIncrement<=0)
292       newCapacity = list.length*2;
293
294     int[] newArray = new int[Math.max(newCapacity, minCapacity)];
295
296     System.arraycopy(list, 0, newArray, 0, list.length);
297     list = newArray;
298   }
299 }
300
Popular Tags