001: /*
002: * Copyright (C) Chaperon. All rights reserved.
003: * -------------------------------------------------------------------------
004: * This software is published under the terms of the Apache Software License
005: * version 1.1, a copy of which has been included with this distribution in
006: * the LICENSE file.
007: */
008:
009: package net.sourceforge.chaperon.common;
010:
011: /**
012: * This class represents a set of integer values, which means there doesn't exist multiple
013: * instances of values.
014: *
015: * @author <a href="mailto:stephan@apache.org">Stephan Michels</a>
016: * @version CVS $Id: IntegerSet.java,v 1.3 2003/12/09 19:55:52 benedikta Exp $
017: */
018: public class IntegerSet implements IntegerCollection {
019: private int capacityIncrement = 100;
020: private int elementCount = 0;
021: private int[] list = new int[10];
022: private int dummy;
023:
024: /**
025: * Creates an empty set of integer values.
026: */
027: public IntegerSet() {
028: }
029:
030: /**
031: * Add a value to this set.
032: *
033: * @param value Integer value.
034: *
035: * @return Index of this value.
036: */
037: public int add(int value) {
038: int index = indexOf(value);
039:
040: if (index == -1) {
041: ensureCapacity(elementCount + 1);
042: list[elementCount++] = value;
043: return elementCount - 1;
044: }
045:
046: return index;
047: }
048:
049: /**
050: * Add the values of an other integer collection to this set.
051: *
052: * @param collection Collection of values.
053: */
054: public void add(IntegerCollection collection) {
055: for (int i = 0; i < collection.getCount(); i++)
056: add(collection.get(i));
057: }
058:
059: /**
060: * Add the values of an array to this set.
061: *
062: * @param array Array of integer values.
063: */
064: public void add(int[] array) {
065: for (int i = 0; i < array.length; i++)
066: add(array[i]);
067: }
068:
069: /**
070: * Removes a value giving by an index.
071: *
072: * @param index Index of the integer value.
073: */
074: public void remove(int index) {
075: if (index >= elementCount)
076: throw new ArrayIndexOutOfBoundsException(index);
077:
078: elementCount--;
079: if (index < elementCount)
080: System.arraycopy(list, index + 1, list, index, elementCount
081: - index);
082:
083: list[elementCount] = 0;
084: }
085:
086: /**
087: * Replace a value given by an index.
088: *
089: * @param index Index of the value, which should be replaced.
090: * @param value The new value.
091: */
092: public void set(int index, int value) {
093: if (index >= elementCount)
094: throw new ArrayIndexOutOfBoundsException(index);
095:
096: if (contains(value) && (!(get(index) == value)))
097: throw new IllegalArgumentException(
098: "Set already contains the value");
099:
100: list[index] = value;
101: }
102:
103: /**
104: * Return a value from this set given by an index.
105: *
106: * @param index Index of the value.
107: *
108: * @return Integer value.
109: */
110: public int get(int index) {
111: if (index >= elementCount)
112: throw new ArrayIndexOutOfBoundsException(index);
113:
114: return list[index];
115: }
116:
117: /**
118: * Returns the count of value in this set.
119: *
120: * @return Count of integer values.
121: */
122: public int getCount() {
123: return elementCount;
124: }
125:
126: /**
127: * Return the index of a value, otherwise -1
128: *
129: * @param value Value, which should be found in this set.
130: *
131: * @return Index of this value.
132: */
133: public int indexOf(int value) {
134: for (int i = 0; i < elementCount; i++) {
135: if (list[i] == value)
136: return i;
137: }
138:
139: return -1;
140: }
141:
142: /**
143: * If the list contains a value.
144: *
145: * @param value Value, which should be found in this set
146: *
147: * @return True, if this set contains the value.
148: */
149: public boolean contains(int value) {
150: for (int i = 0; i < elementCount; i++) {
151: if (list[i] == value)
152: return true;
153: }
154:
155: return false;
156: }
157:
158: /**
159: * Removes a value from this set.
160: *
161: * @param value Value to remove.
162: */
163: public void removeValue(int value) {
164: int index;
165:
166: while ((index = indexOf(value)) != -1)
167: remove(index);
168: }
169:
170: /**
171: * If this set contains no values.
172: *
173: * @return True, if this set is empty.
174: */
175: public boolean isEmpty() {
176: return (elementCount <= 0);
177: }
178:
179: /**
180: * Pops a value of the end of this set.
181: *
182: * @return Value from the end of this set.
183: */
184: public int pop() {
185: if (0 >= elementCount)
186: throw new java.util.EmptyStackException();
187:
188: return list[--elementCount];
189: }
190:
191: /**
192: * Pushs a value to this list.
193: *
194: * @param value Value, which should be added.
195: */
196: public void push(int value) {
197: add(value);
198: }
199:
200: /**
201: * Push values from an array.
202: *
203: * @param values Array of integer values.
204: */
205: public void push(int[] values) {
206: add(values);
207: }
208:
209: /**
210: * Peek a value of the end of this set.
211: *
212: * @return Integer value.
213: */
214: public int peek() {
215: if (0 >= elementCount)
216: throw new java.util.EmptyStackException();
217:
218: return list[elementCount - 1];
219: }
220:
221: /**
222: * Removes all values from this set
223: */
224: public void clear() {
225: elementCount = 0;
226: }
227:
228: /**
229: * Swaps two values from this set.
230: *
231: * @param index1 Index from the first value.
232: * @param index2 Index from the second value.
233: */
234: public void swap(int index1, int index2) {
235: dummy = list[index1];
236: list[index1] = list[index2];
237: list[index2] = dummy;
238: }
239:
240: /**
241: * Return a string representation of the collection.
242: *
243: * @return String representation of the collection.
244: */
245: public String toString() {
246: StringBuffer buffer = new StringBuffer();
247:
248: buffer.append("[");
249: for (int i = 0; i < elementCount; i++) {
250: buffer.append(String.valueOf(list[i]));
251: if (i < (elementCount - 1))
252: buffer.append(",");
253: }
254:
255: buffer.append("]");
256: return buffer.toString();
257: }
258:
259: /**
260: * Ensure the capacity for adding values.
261: *
262: * @param minCapacity Minimum capacity.
263: */
264: private void ensureCapacity(int minCapacity) {
265: if (list.length >= minCapacity)
266: return;
267:
268: int newCapacity = list.length + capacityIncrement;
269:
270: if (capacityIncrement <= 0)
271: newCapacity = list.length * 2;
272:
273: int[] newArray = new int[Math.max(newCapacity, minCapacity)];
274:
275: System.arraycopy(list, 0, newArray, 0, list.length);
276: list = newArray;
277: }
278: }
|