00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef _DECAF_UTIL_ABSTRACTSEQUENTIALLIST_H_
00019 #define _DECAF_UTIL_ABSTRACTSEQUENTIALLIST_H_
00020
00021 #include <decaf/util/Config.h>
00022 #include <decaf/lang/exceptions/UnsupportedOperationException.h>
00023 #include <decaf/lang/exceptions/NullPointerException.h>
00024 #include <decaf/lang/exceptions/IllegalArgumentException.h>
00025 #include <decaf/lang/Iterable.h>
00026 #include <decaf/util/Iterator.h>
00027 #include <decaf/util/AbstractList.h>
00028 #include <memory>
00029
00030 namespace decaf {
00031 namespace util {
00032
00058 template< typename E >
00059 class AbstractSequentialList : public decaf::util::AbstractList<E> {
00060 public:
00061
00062 using AbstractList<E>::add;
00063 using AbstractList<E>::addAll;
00064
00065 public:
00066
00067 virtual ~AbstractSequentialList() {}
00068
00069 virtual Iterator<E>* iterator() {
00070 return this->listIterator( 0 );
00071 }
00072 virtual Iterator<E>* iterator() const {
00073 return this->listIterator( 0 );
00074 }
00075
00076 virtual ListIterator<E>* listIterator() {
00077 return this->listIterator( 0 );
00078 }
00079 virtual ListIterator<E>* listIterator() const {
00080 return this->listIterator( 0 );
00081 }
00082
00083 virtual ListIterator<E>* listIterator( int index DECAF_UNUSED ) {
00084 throw decaf::lang::exceptions::UnsupportedOperationException(
00085 __FILE__, __LINE__, "Abstract sequential list does not implement the listIterator." );
00086 }
00087 virtual ListIterator<E>* listIterator( int index DECAF_UNUSED ) const {
00088 throw decaf::lang::exceptions::UnsupportedOperationException(
00089 __FILE__, __LINE__, "Abstract sequential list does not implement the listIterator." );
00090 }
00091
00099 virtual E get( int index ) const {
00100
00101 try {
00102 std::auto_ptr< ListIterator<E> > iter( this->listIterator( index ) );
00103 return iter->next();
00104 } catch( decaf::util::NoSuchElementException& ex ) {
00105 throw decaf::lang::exceptions::IndexOutOfBoundsException(
00106 __FILE__, __LINE__, "get called with invalid index." );
00107 }
00108 }
00109
00117 virtual E set( int index, const E& element ) {
00118
00119 try {
00120 std::auto_ptr< ListIterator<E> > iter( this->listIterator( index ) );
00121 E result = iter->next();
00122 iter->set( element );
00123 return result;
00124 } catch( decaf::util::NoSuchElementException& ex ) {
00125 throw decaf::lang::exceptions::IndexOutOfBoundsException(
00126 __FILE__, __LINE__, "set called with invalid index." );
00127 }
00128 }
00129
00137 virtual void add( int index, const E& element ) {
00138
00139 try {
00140 std::auto_ptr< ListIterator<E> > iter( this->listIterator( index ) );
00141 iter->add( element );
00142 } catch( decaf::util::NoSuchElementException& ex ) {
00143 throw decaf::lang::exceptions::IndexOutOfBoundsException(
00144 __FILE__, __LINE__, "add called with invalid index." );
00145 }
00146 }
00147
00157 virtual bool addAll( int index, const Collection<E>& source ) {
00158
00159 std::auto_ptr< ListIterator<E> > iter( this->listIterator( index ) );
00160 std::auto_ptr< Iterator<E> > srcIter( source.iterator() );
00161 int next = iter->nextIndex();
00162 while( srcIter->hasNext() ) {
00163 iter->add( srcIter->next() );
00164 }
00165 return next != iter->nextIndex();
00166 }
00167
00174 virtual E removeAt( int index ) {
00175
00176 try {
00177 std::auto_ptr< ListIterator<E> > iter( this->listIterator( index ) );
00178 E result = iter->next();
00179 iter->remove();
00180 return result;
00181 } catch( decaf::util::NoSuchElementException& ex ) {
00182 throw decaf::lang::exceptions::IndexOutOfBoundsException(
00183 __FILE__, __LINE__, "set called with invalid index." );
00184 }
00185 }
00186
00187 };
00188
00189 }}
00190
00191 #endif