activemq-cpp-3.9.5
AbstractList.h
Go to the documentation of this file.
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#ifndef _DECAF_UTIL_ABSTRACTLIST_H_
19#define _DECAF_UTIL_ABSTRACTLIST_H_
20
21#include <decaf/util/Config.h>
27#include <decaf/lang/Iterable.h>
28#include <decaf/util/Iterator.h>
29#include <decaf/util/List.h>
31#include <memory>
32
33namespace decaf {
34namespace util {
35
64 template< typename E >
67 protected:
68
70
71 private:
72
73 class SimpleListIterator : public ListIterator<E> {
74 protected:
75
76 AbstractList<E>* parent;
77 int numLeft;
78 int expectedModCount;
79 int lastPosition;
80
81 private:
82
83 SimpleListIterator(const SimpleListIterator&);
84 SimpleListIterator operator=(const SimpleListIterator&);
85
86 public:
87
88 SimpleListIterator(AbstractList<E>* parent, int start) :
89 ListIterator<E>(), parent(NULL), numLeft(0), expectedModCount(0), lastPosition(-1) {
90
91 if (parent == NULL) {
93 __FILE__, __LINE__, "List Iterator constructed with NULL parent");
94 }
95
96 if (start < 0 || start > parent->size()) {
98 __FILE__, __LINE__, "start index passed was negative or greater than size()");
99 }
100
101 this->numLeft = parent->size() - start;
102 this->parent = parent;
103 this->expectedModCount = parent->modCount;
104 }
105
106 virtual ~SimpleListIterator() {}
107
108 virtual bool hasNext() const {
109 return this->numLeft > 0;
110 }
111
112 virtual E next() {
113
114 if (this->expectedModCount != this->parent->modCount) {
116 __FILE__, __LINE__, "Concurrent Modification of Parent List detected.");
117 }
118
119 try {
120
121 int index = this->parent->size() - this->numLeft;
122 E result = this->parent->get( index );
123 this->lastPosition = index;
124 this->numLeft--;
125
126 return result;
129 __FILE__, __LINE__, "Next called without a next element to process.");
130 }
131 }
132
133 virtual void remove() {
134
135 if (this->lastPosition == -1) {
137 __FILE__, __LINE__, "Remove called before next() was called.");
138 }
139
140 if (this->expectedModCount != this->parent->modCount) {
142 __FILE__, __LINE__, "Concurrent Modification of Parent List detected.");
143 }
144
145 try {
146
147 if (this->lastPosition == this->parent->size() - this->numLeft) {
148 this->numLeft--; // we're removing after a call to previous()
149 }
150
151 this->parent->removeAt( lastPosition );
152
155 __FILE__, __LINE__, "Concurrent Modification detected.");
156 }
157
158 this->expectedModCount = this->parent->modCount;
159 this->lastPosition = -1;
160 }
161
162 virtual void add(const E& value) {
163
164 if (this->expectedModCount != this->parent->modCount) {
166 __FILE__, __LINE__, "Concurrent Modification of Parent List detected.");
167 }
168
169 try {
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.");
176 }
177 }
178
179 virtual bool hasPrevious() const {
180 return this->numLeft < this->parent->size();
181 }
182
183 virtual int nextIndex() const {
184 return this->parent->size() - this->numLeft;
185 }
186
187 virtual E previous() {
188
189 if (this->expectedModCount != this->parent->modCount) {
191 __FILE__, __LINE__, "Concurrent Modification detected.");
192 }
193
194 try {
195
196 int index = this->parent->size() - this->numLeft - 1;
197 E result = this->parent->get(index);
198 this->numLeft++;
199 this->lastPosition = index;
200
201 return result;
204 __FILE__, __LINE__, "No previous element exists.");
205 }
206 }
207
208 virtual int previousIndex() const {
209 return this->parent->size() - this->numLeft - 1;
210 }
211
212 virtual void set(const E& value) {
213
214 if (this->expectedModCount != this->parent->modCount) {
216 __FILE__, __LINE__, "Concurrent Modification detected.");
217 }
218
219 try {
220 this->parent->set(this->lastPosition, value);
223 }
224 }
225 };
226
227 class ConstSimpleListIterator : public ListIterator<E> {
228 protected:
229
230 const AbstractList<E>* parent;
231 int numLeft;
232 int expectedModCount;
233 int lastPosition;
234
235 private:
236
237 ConstSimpleListIterator(const ConstSimpleListIterator&);
238 ConstSimpleListIterator operator=(const ConstSimpleListIterator&);
239
240 public:
241
242 ConstSimpleListIterator(const AbstractList<E>* parent, int start) :
243 ListIterator<E>(), parent(parent), numLeft(0), expectedModCount(0), lastPosition(-1) {
244
245 if (parent == NULL) {
247 __FILE__, __LINE__, "List Iterator constructed with NULL parent");
248 }
249
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()");
253 }
254
255 this->numLeft = parent->size() - start;
256 this->parent = parent;
257 this->expectedModCount = parent->modCount;
258 }
259
260 virtual ~ConstSimpleListIterator() {}
261
262 virtual bool hasNext() const {
263 return this->numLeft > 0;
264 }
265
266 virtual E next() {
267
268 if (this->expectedModCount != this->parent->modCount) {
269 throw ConcurrentModificationException(
270 __FILE__, __LINE__, "Concurrent Modification of Parent List detected.");
271 }
272
273 try {
274
275 int index = this->parent->size() - this->numLeft;
276 E result = this->parent->get(index);
277 this->lastPosition = index;
278 this->numLeft--;
279
280 return result;
281 } catch (decaf::lang::exceptions::IndexOutOfBoundsException& e) {
282 throw decaf::util::NoSuchElementException(
283 __FILE__, __LINE__, "Next called without a next element to process.");
284 }
285 }
286
287 virtual void remove() {
288 throw lang::exceptions::UnsupportedOperationException(
289 __FILE__, __LINE__,
290 "AbstractList::Iterator::remove - Const Iterator.");
291 }
292
293 virtual void add(const E& value DECAF_UNUSED) {
294 throw lang::exceptions::UnsupportedOperationException(
295 __FILE__, __LINE__,
296 "AbstractList::ListIterator::radd - Const Iterator.");
297 }
298
299 virtual bool hasPrevious() const {
300 return this->numLeft < this->parent->size();
301 }
302
303 virtual int nextIndex() const {
304 return this->parent->size() - this->numLeft;
305 }
306
307 virtual E previous() {
308
309 if (this->expectedModCount != this->parent->modCount) {
310 throw ConcurrentModificationException(
311 __FILE__, __LINE__, "Concurrent Modification detected.");
312 }
313
314 try {
315
316 int index = this->parent->size() - this->numLeft - 1;
317 E result = this->parent->get(index);
318 this->numLeft++;
319 this->lastPosition = index;
320
321 return result;
322 } catch (decaf::lang::exceptions::IndexOutOfBoundsException& e) {
323 throw decaf::util::NoSuchElementException(
324 __FILE__, __LINE__, "No previous element exists.");
325 }
326 }
327
328 virtual int previousIndex() const {
329 return this->parent->size() - this->numLeft - 1;
330 }
331
332 virtual void set(const E& value DECAF_UNUSED) {
333 throw lang::exceptions::UnsupportedOperationException(
334 __FILE__, __LINE__,
335 "AbstractList::ListIterator::set - Const Iterator.");
336 }
337 };
338
339 public:
340
342
343 virtual ~AbstractList() {}
344
346 return new SimpleListIterator(this, 0);
347 }
348 virtual Iterator<E>* iterator() const {
349 return new ConstSimpleListIterator(this, 0);
350 }
351
353 return new SimpleListIterator(this, 0);
354 }
355 virtual ListIterator<E>* listIterator() const {
356 return new ConstSimpleListIterator(this, 0);
357 }
358
359 virtual ListIterator<E>* listIterator(int index) {
360 return new SimpleListIterator(this, index);
361 }
362 virtual ListIterator<E>* listIterator(int index) const {
363 return new ConstSimpleListIterator(this, index);
364 }
365
366 virtual void clear() {
367 this->removeRange(0, this->size());
368 }
369
370 virtual bool add(const E& value) {
371 this->add(this->size(), value);
372 return true;
373 }
374
375 virtual void add(int index DECAF_UNUSED, const E& element DECAF_UNUSED) {
377 __FILE__, __LINE__, "Abstract list does not implement the add method.");
378 }
379
380 // Use this method since our own addAll will hide the base class version.
382
383 virtual bool addAll(int index, const Collection<E>& source) {
384 std::auto_ptr<decaf::util::Iterator<E> > iter(source.iterator());
385 while (iter->hasNext()) {
386 this->add(index++, iter->next());
387 }
388
389 return !source.isEmpty();
390 }
391
392 virtual E removeAt(int index DECAF_UNUSED) {
394 __FILE__, __LINE__, "Abstract list does not implement the removeAt method.");
395 }
396
397 virtual E set(int index DECAF_UNUSED, const E& element DECAF_UNUSED) {
399 __FILE__, __LINE__, "Abstract list does not implement the set method.");
400 }
401
402 virtual int indexOf(const E& value) const {
403
404 std::auto_ptr<decaf::util::ListIterator<E> > iter(this->listIterator());
405
406 while (iter->hasNext()) {
407 if (value == iter->next()) {
408 return iter->previousIndex();
409 }
410 }
411
412 return -1;
413 }
414
415 virtual int lastIndexOf(const E& value) const {
416
417 std::auto_ptr< decaf::util::ListIterator<E> > iter( this->listIterator( this->size() ) );
418
419 while (iter->hasPrevious()) {
420 if (value == iter->previous()) {
421 return iter->nextIndex();
422 }
423 }
424
425 return -1;
426 }
427
428 protected:
429
430 void removeRange(int start, int end) {
431 std::auto_ptr<decaf::util::Iterator<E> > iter(this->listIterator(start));
432 for (int i = start; i < end; i++) {
433 iter->next();
434 iter->remove();
435 }
436 }
437
438 };
439
440}}
441
442#endif /* _DECAF_UTIL_ABSTRACTLIST_H_ */
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