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