18 #ifndef _DECAF_UTIL_ABSTRACTLIST_H_
19 #define _DECAF_UTIL_ABSTRACTLIST_H_
64 template<
typename E >
83 SimpleListIterator(
const SimpleListIterator&);
84 SimpleListIterator
operator=(
const SimpleListIterator&);
93 __FILE__, __LINE__,
"List Iterator constructed with NULL parent" );
96 if (start < 0 || start > parent->
size()) {
98 __FILE__, __LINE__,
"start index passed was negative or greater than size()" );
101 this->numLeft = parent->
size() - start;
102 this->parent = parent;
103 this->expectedModCount = parent->
modCount;
106 virtual ~SimpleListIterator() {}
108 virtual bool hasNext()
const {
109 return this->numLeft > 0;
114 if (this->expectedModCount != this->parent->
modCount) {
116 __FILE__, __LINE__,
"Concurrent Modification of Parent List detected." );
121 int index = this->parent->
size() - this->numLeft;
122 E result = this->parent->
get( index );
123 this->lastPosition = index;
129 __FILE__, __LINE__,
"Next called without a next element to process." );
133 virtual void remove() {
135 if (this->lastPosition == -1) {
137 __FILE__, __LINE__,
"Remove called before next() was called." );
140 if (this->expectedModCount != this->parent->
modCount) {
142 __FILE__, __LINE__,
"Concurrent Modification of Parent List detected." );
147 if (this->lastPosition == this->parent->
size() - this->numLeft) {
151 this->parent->
removeAt( lastPosition );
155 __FILE__, __LINE__,
"Concurrent Modification detected." );
158 this->expectedModCount = this->parent->
modCount;
159 this->lastPosition = -1;
162 virtual void add(
const E& value ) {
164 if (this->expectedModCount != this->parent->
modCount) {
166 __FILE__, __LINE__,
"Concurrent Modification of Parent List detected." );
170 this->parent->
add(this->parent->
size() - this->numLeft, value);
171 this->expectedModCount = this->parent->
modCount;
172 this->lastPosition = -1;
175 __FILE__, __LINE__,
"Add called without a next element to process." );
179 virtual bool hasPrevious()
const {
180 return this->numLeft < this->parent->
size();
183 virtual int nextIndex()
const {
184 return this->parent->
size() - this->numLeft;
187 virtual E previous() {
189 if (this->expectedModCount != this->parent->
modCount) {
191 __FILE__, __LINE__,
"Concurrent Modification detected." );
196 int index = this->parent->
size() - this->numLeft - 1;
197 E result = this->parent->
get(index);
199 this->lastPosition = index;
204 __FILE__, __LINE__,
"No previous element exists." );
208 virtual int previousIndex()
const {
209 return this->parent->
size() - this->numLeft - 1;
212 virtual void set(
const E& value ) {
214 if (this->expectedModCount != this->parent->
modCount) {
216 __FILE__, __LINE__,
"Concurrent Modification detected." );
220 this->parent->
set(this->lastPosition, value);
227 class ConstSimpleListIterator :
public ListIterator<E> {
232 int expectedModCount;
237 ConstSimpleListIterator(
const ConstSimpleListIterator&);
238 ConstSimpleListIterator
operator=(
const ConstSimpleListIterator&);
243 ListIterator<E>(), parent(parent), numLeft(0), expectedModCount(0), lastPosition(-1) {
245 if (parent ==
NULL) {
247 __FILE__, __LINE__,
"List Iterator constructed with NULL parent" );
250 if (start < 0 || start > parent->
size()) {
252 __FILE__, __LINE__,
"start index passed was negative or greater than size()" );
255 this->numLeft = parent->
size() - start;
256 this->parent = parent;
257 this->expectedModCount = parent->
modCount;
260 virtual ~ConstSimpleListIterator() {}
262 virtual bool hasNext()
const {
263 return this->numLeft > 0;
268 if (this->expectedModCount != this->parent->
modCount) {
269 throw ConcurrentModificationException(
270 __FILE__, __LINE__,
"Concurrent Modification of Parent List detected." );
275 int index = this->parent->
size() - this->numLeft;
276 E result = this->parent->
get(index);
277 this->lastPosition = index;
283 __FILE__, __LINE__,
"Next called without a next element to process." );
287 virtual void remove() {
288 throw lang::exceptions::UnsupportedOperationException(
290 "AbstractList::Iterator::remove - Const Iterator." );
294 throw lang::exceptions::UnsupportedOperationException(
296 "AbstractList::ListIterator::radd - Const Iterator." );
299 virtual bool hasPrevious()
const {
300 return this->numLeft < this->parent->
size();
303 virtual int nextIndex()
const {
304 return this->parent->
size() - this->numLeft;
307 virtual E previous() {
309 if (this->expectedModCount != this->parent->
modCount) {
310 throw ConcurrentModificationException(
311 __FILE__, __LINE__,
"Concurrent Modification detected." );
316 int index = this->parent->
size() - this->numLeft - 1;
317 E result = this->parent->
get(index);
319 this->lastPosition = index;
324 __FILE__, __LINE__,
"No previous element exists." );
328 virtual int previousIndex()
const {
329 return this->parent->
size() - this->numLeft - 1;
333 throw lang::exceptions::UnsupportedOperationException(
335 "AbstractList::ListIterator::set - Const Iterator." );
346 return new SimpleListIterator(
this, 0);
349 return new ConstSimpleListIterator(
this, 0);
353 return new SimpleListIterator(
this, 0);
356 return new ConstSimpleListIterator(
this, 0);
360 return new SimpleListIterator(
this, index);
363 return new ConstSimpleListIterator(
this, index);
370 virtual bool add(
const E& value) {
371 this->
add(this->
size(), value);
377 __FILE__, __LINE__,
"Abstract list does not implement the add method." );
384 std::auto_ptr<decaf::util::Iterator<E> > iter(source.
iterator());
385 while (iter->hasNext()) {
386 this->
add(index++, iter->next());
394 __FILE__, __LINE__,
"Abstract list does not implement the removeAt method." );
399 __FILE__, __LINE__,
"Abstract list does not implement the set method." );
404 std::auto_ptr<decaf::util::ListIterator<E> > iter(this->
listIterator());
406 while (iter->hasNext()) {
407 if (value == iter->next()) {
408 return iter->previousIndex();
417 std::auto_ptr< decaf::util::ListIterator<E> > iter( this->
listIterator( this->
size() ) );
419 while (iter->hasPrevious()) {
420 if (value == iter->previous()) {
421 return iter->nextIndex();
431 std::auto_ptr<decaf::util::Iterator<E> > iter(this->
listIterator(start));
432 for (
int i = start; i < end; i++) {