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.");
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()) {
251 throw decaf::lang::exceptions::IndexOutOfBoundsException(
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;
281 }
catch (decaf::lang::exceptions::IndexOutOfBoundsException& e) {
282 throw decaf::util::NoSuchElementException(
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.");
293 virtual void add(
const E& value DECAF_UNUSED) {
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;
322 }
catch (decaf::lang::exceptions::IndexOutOfBoundsException& e) {
323 throw decaf::util::NoSuchElementException(
324 __FILE__, __LINE__,
"No previous element exists.");
328 virtual int previousIndex()
const {
329 return this->parent->size() - this->numLeft - 1;
332 virtual void set(
const E& value DECAF_UNUSED) {
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);
375 virtual void add(
int index DECAF_UNUSED,
const E& element DECAF_UNUSED) {
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.");
397 virtual E
set(
int index DECAF_UNUSED,
const E& element DECAF_UNUSED) {
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++) {
virtual decaf::util::Iterator< E > * iterator()=0
Definition IllegalStateException.h:32
Definition IndexOutOfBoundsException.h:31
Definition NullPointerException.h:32
Definition UnsupportedOperationException.h:32
This class provides a skeletal implementation of the Collection interface, to minimize the effort req...
Definition AbstractCollection.h:58
virtual bool remove(const E &value)
Removes a single instance of the specified element from the collection.More formally,...
Definition AbstractCollection.h:259
AbstractCollection< E > & operator=(const AbstractCollection< E > &collection)
Assignment Operator, copy element from the source collection to this collection after clearing any el...
Definition AbstractCollection.h:92
This class provides a skeletal implementation of the List interface to minimize the effort required t...
Definition AbstractList.h:66
virtual ListIterator< E > * listIterator(int index) const
Definition AbstractList.h:362
virtual void clear()
Removes all of the elements from this collection (optional operation).
Definition AbstractList.h:366
virtual int lastIndexOf(const E &value) const
Returns the index of the last occurrence of the specified element in this list, or -1 if this list do...
Definition AbstractList.h:415
void removeRange(int start, int end)
Definition AbstractList.h:430
virtual ListIterator< E > * listIterator()
Definition AbstractList.h:352
int modCount
Definition AbstractList.h:69
virtual Iterator< E > * iterator() const
Definition AbstractList.h:348
virtual Iterator< E > * iterator()
Definition AbstractList.h:345
virtual void add(int index DECAF_UNUSED, const E &element DECAF_UNUSED)
Definition AbstractList.h:375
virtual ListIterator< E > * listIterator(int index)
Definition AbstractList.h:359
virtual bool add(const E &value)
Returns true if this collection changed as a result of the call.
Definition AbstractList.h:370
virtual bool addAll(int index, const Collection< E > &source)
Inserts all of the elements in the specified collection into this list at the specified position (opt...
Definition AbstractList.h:383
virtual ListIterator< E > * listIterator() const
Definition AbstractList.h:355
virtual E removeAt(int index DECAF_UNUSED)
Removes the element at the specified position in this list.
Definition AbstractList.h:392
AbstractList()
Definition AbstractList.h:341
virtual int indexOf(const E &value) const
Returns the index of the first occurrence of the specified element in this list, or -1 if this list d...
Definition AbstractList.h:402
virtual ~AbstractList()
Definition AbstractList.h:343
virtual E set(int index DECAF_UNUSED, const E &element DECAF_UNUSED)
Definition AbstractList.h:397
The root interface in the collection hierarchy.
Definition Collection.h:69
virtual int size() const =0
Returns the number of elements in this collection.
virtual bool isEmpty() const =0
Definition ConcurrentModificationException.h:28
Defines an object that can be used to iterate over the elements of a collection.
Definition Iterator.h:34
An ordered collection (also known as a sequence).
Definition List.h:47
virtual E get(int index) const =0
Gets the element contained at position passed.
An iterator for lists that allows the programmer to traverse the list in either direction,...
Definition ListIterator.h:38
Definition NoSuchElementException.h:31
#define NULL
Definition Config.h:33
Definition AbstractCollection.h:33
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.
Definition AprPool.h:25