001: /*
002: * Primitive Collections for Java.
003: * Copyright (C) 2002, 2003 S�ren Bak
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019: package com.uwyn.rife.pcj.list;
020:
021: import com.uwyn.rife.pcj.AbstractIntCollection;
022: import com.uwyn.rife.pcj.IntCollection;
023: import com.uwyn.rife.pcj.IntIterator;
024: import com.uwyn.rife.pcj.hash.DefaultIntHashFunction;
025: import com.uwyn.rife.pcj.util.Exceptions;
026:
027: /**
028: * This class represents an abstract base for implementing
029: * lists of int values. All operations that can be implemented
030: * using iterators and the <tt>get()</tt> and <tt>set()</tt> methods
031: * are implemented as such. In most cases, this is
032: * hardly an efficient solution, and at least some of those
033: * methods should be overridden by sub-classes.
034: *
035: * @author Søren Bak
036: * @version 1.2 21-08-2003 19:14
037: * @since 1.0
038: */
039: public abstract class AbstractIntList extends AbstractIntCollection
040: implements IntList {
041:
042: /** Default constructor to be invoked by sub-classes. */
043: protected AbstractIntList() {
044: }
045:
046: public boolean add(int v) {
047: add(size(), v);
048: return true;
049: }
050:
051: /**
052: * Throws <tt>UnsupportedOperationException</tt>.
053: *
054: * @throws UnsupportedOperationException
055: * unconditionally.
056: */
057: public void add(int index, int v) {
058: Exceptions.unsupported("add");
059: }
060:
061: public boolean addAll(int index, IntCollection c) {
062: if (index < 0 || index > size())
063: Exceptions.indexOutOfBounds(index, 0, size());
064: IntIterator i = c.iterator();
065: boolean result = i.hasNext();
066: while (i.hasNext()) {
067: add(index, i.next());
068: index++;
069: }
070: return result;
071: }
072:
073: public int indexOf(int c) {
074: return indexOf(0, c);
075: }
076:
077: /**
078: * @since 1.2
079: */
080: public int indexOf(int index, int c) {
081: IntListIterator i = listIterator(index);
082: while (i.hasNext())
083: if (i.next() == c)
084: return i.previousIndex();
085: return -1;
086: }
087:
088: public IntIterator iterator() {
089: return listIterator();
090: }
091:
092: public int lastIndexOf(int c) {
093: IntListIterator i = listIterator(size());
094: while (i.hasPrevious())
095: if (i.previous() == c)
096: return i.nextIndex();
097: return -1;
098: }
099:
100: public int lastIndexOf(int index, int c) {
101: IntListIterator i = listIterator(index);
102: while (i.hasPrevious())
103: if (i.previous() == c)
104: return i.nextIndex();
105: return -1;
106: }
107:
108: public IntListIterator listIterator() {
109: return listIterator(0);
110: }
111:
112: public IntListIterator listIterator(final int index) {
113: if (index < 0 || index > size())
114: Exceptions.indexOutOfBounds(index, 0, size());
115:
116: return new IntListIterator() {
117: private int ptr = index;
118: private int lptr = -1;
119:
120: // -------------------------------------------------------
121: // Implementation of Iterator
122: // -------------------------------------------------------
123:
124: public boolean hasNext() {
125: return ptr < size();
126: }
127:
128: public int next() {
129: if (ptr == size())
130: Exceptions.endOfIterator();
131: lptr = ptr++;
132: return get(lptr);
133: }
134:
135: public void remove() {
136: if (lptr == -1)
137: Exceptions.noElementToRemove();
138: AbstractIntList.this .removeElementAt(lptr);
139: if (lptr < ptr)
140: ptr--;
141: lptr = -1;
142: }
143:
144: // -------------------------------------------------------
145: // Implementation of ListIterator
146: // -------------------------------------------------------
147:
148: public void add(int v) {
149: AbstractIntList.this .add(ptr++, v);
150: lptr = -1;
151: }
152:
153: public boolean hasPrevious() {
154: return ptr > 0;
155: }
156:
157: public int nextIndex() {
158: return ptr;
159: }
160:
161: public int previous() {
162: if (ptr == 0)
163: Exceptions.startOfIterator();
164: ptr--;
165: lptr = ptr;
166: return get(ptr);
167: }
168:
169: public int previousIndex() {
170: return ptr - 1;
171: }
172:
173: public void set(int v) {
174: if (lptr == -1)
175: Exceptions.noElementToSet();
176: AbstractIntList.this .set(lptr, v);
177: }
178:
179: };
180: }
181:
182: /**
183: * Throws <tt>UnsupportedOperationException</tt>.
184: *
185: * @throws UnsupportedOperationException
186: * unconditionally.
187: */
188: public int removeElementAt(int index) {
189: Exceptions.unsupported("removeElementAt");
190: throw new RuntimeException();
191: }
192:
193: public boolean equals(Object obj) {
194: if (this == obj)
195: return true;
196: if (!(obj instanceof IntList))
197: return false;
198: IntListIterator i1 = listIterator();
199: IntListIterator i2 = ((IntList) obj).listIterator();
200: while (i1.hasNext() && i2.hasNext())
201: if (i1.next() != i2.next())
202: return false;
203: return !(i1.hasNext() || i2.hasNext());
204: }
205:
206: public int hashCode() {
207: int h = 1;
208: IntIterator i = iterator();
209: while (i.hasNext())
210: h = 31 * h + DefaultIntHashFunction.INSTANCE.hash(i.next());
211: return h;
212: }
213:
214: }
|