activemq-cpp-3.3.0

src/main/decaf/util/StlList.h

Go to the documentation of this file.
00001 /*
00002  * Licensed to the Apache Software Foundation (ASF) under one or more
00003  * contributor license agreements.  See the NOTICE file distributed with
00004  * this work for additional information regarding copyright ownership.
00005  * The ASF licenses this file to You under the Apache License, Version 2.0
00006  * (the "License"); you may not use this file except in compliance with
00007  * the License.  You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 #ifndef _DECAF_UTIL_STLLIST_H_
00019 #define _DECAF_UTIL_STLLIST_H_
00020 
00021 #include <list>
00022 #include <algorithm>
00023 #include <memory>
00024 #include <decaf/lang/exceptions/UnsupportedOperationException.h>
00025 #include <decaf/util/NoSuchElementException.h>
00026 #include <decaf/lang/exceptions/IndexOutOfBoundsException.h>
00027 #include <decaf/util/Config.h>
00028 #include <decaf/util/Iterator.h>
00029 #include <decaf/util/ListIterator.h>
00030 #include <decaf/util/List.h>
00031 #include <decaf/util/AbstractList.h>
00032 
00033 namespace decaf{
00034 namespace util{
00035 
00040     template <typename E>
00041     class StlList : public decaf::util::AbstractList<E> {
00042     private:
00043 
00044         std::list<E> values;
00045 
00046     private:
00047 
00048         class StlListIterator : public ListIterator<E> {
00049         private:
00050 
00051             typename std::list<E>::iterator current;
00052             typename std::list<E>::iterator prev;
00053             typename std::list<E>* list;
00054 
00055         private:
00056 
00057             StlListIterator( const StlListIterator& );
00058             StlListIterator operator= ( const StlListIterator& );
00059 
00060         public:
00061 
00062             StlListIterator( typename std::list<E>* list, int index ) :
00063                 current( list->begin() ), prev( list->end() ), list( list ) {
00064 
00065                 if( index < (int)list->size() ) {
00066                     std::advance( this->current, index );
00067                 } else {
00068                     this->current = list->end();
00069                 }
00070             }
00071 
00072             virtual ~StlListIterator() {}
00073 
00074             virtual E next() {
00075                 if( this->current == list->end() ) {
00076                     throw NoSuchElementException(
00077                         __FILE__, __LINE__,
00078                         "List::Iterator::next - No more elements to return" );
00079                 }
00080 
00081                 this->prev = this->current;
00082                 return *( this->current++ );
00083             }
00084 
00085             virtual bool hasNext() const {
00086                 return ( this->current != list->end() );
00087             }
00088 
00089             virtual void remove() {
00090                 if( this->prev == list->end() ) {
00091                     throw lang::exceptions::IllegalStateException(
00092                         __FILE__, __LINE__,
00093                         "List::Iterator::remove - Invalid State to call remove" );
00094                 }
00095 
00096                 this->list->erase( this->prev );
00097                 this->prev = this->list->end();
00098             }
00099 
00100             virtual void add( const E& e ) {
00101                 this->list->insert( this->current, e );
00102             }
00103 
00104             virtual void set( const E& e ) {
00105 
00106                 if( this->current == list->end() ) {
00107                     this->list->insert( this->current, e );
00108                 } else {
00109                     *( this->current ) = e;
00110                 }
00111             }
00112 
00113             virtual bool hasPrevious() const {
00114                 return ( this->current != this->list->begin() );
00115             }
00116 
00117             virtual E previous() {
00118                 if( this->current == this->list->begin() ) {
00119                     throw NoSuchElementException(
00120                         __FILE__, __LINE__,
00121                         "List::ListIterator::previous - No Previous element." );
00122                 }
00123 
00124                 typename std::list<E>::const_iterator iter = this->current;
00125                 return *( iter-- );
00126             }
00127 
00128             virtual int nextIndex() const {
00129                 if( this->current == this->list->end() ) {
00130                     return (int)this->list->size();
00131                 }
00132 
00133                 return (int)std::distance( this->list->begin(), this->current );
00134             }
00135 
00136             virtual int previousIndex() const {
00137                 if( this->current == this->list->begin() ) {
00138                     return -1;
00139                 }
00140 
00141                 return (int)std::distance( this->list->begin(), this->current ) - 1;
00142             }
00143 
00144         };
00145 
00146         class ConstStlListIterator : public decaf::util::ListIterator<E> {
00147         private:
00148 
00149             typename std::list<E>::const_iterator current;
00150             typename std::list<E>::const_iterator prev;
00151             const typename std::list<E>* list;
00152 
00153         private:
00154 
00155             ConstStlListIterator( const ConstStlListIterator& );
00156             ConstStlListIterator operator= ( const ConstStlListIterator& );
00157 
00158         public:
00159 
00160             ConstStlListIterator( const typename std::list<E>* list, int index ) :
00161                 ListIterator<E>(), current( list->begin() ), prev( list->end() ), list( list ) {
00162 
00163                 if( index < (int)list->size() ) {
00164                     std::advance( this->current, index );
00165                 } else {
00166                     this->current = list->end();
00167                 }
00168             }
00169 
00170             virtual ~ConstStlListIterator() {}
00171 
00172             virtual E next() {
00173                 if( this->current == list->end() ) {
00174                     throw NoSuchElementException(
00175                         __FILE__, __LINE__,
00176                         "List::Iterator::next - No more elements to return" );
00177                 }
00178 
00179                 this->prev = this->current;
00180                 return *( this->current++ );
00181             }
00182 
00183             virtual bool hasNext() const {
00184                 return ( this->current != list->end() );
00185             }
00186 
00187             virtual void remove() {
00188 
00189                 throw lang::exceptions::UnsupportedOperationException(
00190                     __FILE__, __LINE__,
00191                     "List::ListIterator::remove - Const Iterator." );
00192             }
00193 
00194             virtual void add( const E& e DECAF_UNUSED ) {
00195 
00196                 throw lang::exceptions::UnsupportedOperationException(
00197                     __FILE__, __LINE__,
00198                     "List::ListIterator::add - Const Iterator." );
00199             }
00200 
00201             virtual void set( const E& e DECAF_UNUSED ) {
00202 
00203                 throw lang::exceptions::UnsupportedOperationException(
00204                     __FILE__, __LINE__,
00205                     "List::ListIterator::set - Const Iterator." );
00206             }
00207 
00208             virtual bool hasPrevious() const {
00209                 return ( this->current != this->list->begin() );
00210             }
00211 
00212             virtual E previous() {
00213                 if( this->current == this->list->begin() ) {
00214                     throw NoSuchElementException(
00215                         __FILE__, __LINE__,
00216                         "List::ListIterator::previous - No Previous element." );
00217                 }
00218 
00219                 typename std::list<E>::const_iterator iter = this->current;
00220                 return *( iter-- );
00221             }
00222 
00223             virtual int nextIndex() const {
00224                 if( this->current == this->list->end() ) {
00225                     return (int)this->list->size();
00226                 }
00227 
00228                 return (int)std::distance( this->list->begin(), this->current );
00229             }
00230 
00231             virtual int previousIndex() const {
00232                 if( this->current == this->list->begin() ) {
00233                     return -1;
00234                 }
00235 
00236                 return (int)std::distance( this->list->begin(), this->current ) - 1;
00237             }
00238         };
00239 
00240     public:
00241 
00245         StlList() : AbstractList<E>(), values() {}
00246 
00252         StlList( const StlList& source ) : AbstractList<E>(), values() {
00253             copy( source );
00254         }
00255 
00261         StlList( const Collection<E>& source ) : AbstractList<E>(), values() {
00262             AbstractList<E>::copy( source );
00263         }
00264 
00268         virtual bool equals( const Collection<E>& collection ) const {
00269 
00270             const StlList<E>* listptr = dynamic_cast<const StlList<E>*>( &collection );
00271             if( listptr == NULL ) {
00272                 return AbstractList<E>::equals( collection );
00273             }
00274 
00275             return this->values == listptr->values;
00276         }
00277 
00281         virtual void copy( const Collection<E>& collection ) {
00282 
00283             const StlList<E>* listptr = dynamic_cast<const StlList<E>*>( &collection );
00284             if( listptr == NULL ) {
00285                 AbstractList<E>::copy( collection );
00286                 return;
00287             }
00288 
00289             this->values.clear();
00290             this->values = listptr->values;
00291         }
00292 
00296         virtual Iterator<E>* iterator() {
00297             return new StlListIterator( &values, 0 );
00298         }
00299         virtual Iterator<E>* iterator() const {
00300             return new ConstStlListIterator( &values, 0 );
00301         }
00302 
00306         virtual ListIterator<E>* listIterator() {
00307             return new StlListIterator( &values, 0 );
00308         }
00309         virtual ListIterator<E>* listIterator() const {
00310             return new ConstStlListIterator( &values, 0 );
00311         }
00312 
00316         virtual ListIterator<E>* listIterator( int index ) {
00317 
00318             if( index < 0 || index > this->size() ) {
00319                 throw decaf::lang::exceptions::IndexOutOfBoundsException(
00320                     __FILE__, __LINE__,
00321                     "List::listIterator - Index greater than size() or negative" );
00322             }
00323 
00324             return new StlListIterator( &values, index );
00325         }
00326         virtual ListIterator<E>* listIterator( int index ) const {
00327 
00328             if( index < 0 || index > this->size() ) {
00329                 throw decaf::lang::exceptions::IndexOutOfBoundsException(
00330                     __FILE__, __LINE__,
00331                     "List::listIterator - Index greater than size() or negative" );
00332             }
00333 
00334             return new ConstStlListIterator( &values, index );
00335         }
00336 
00340         virtual void clear() {
00341             values.clear();
00342         }
00343 
00347         virtual bool isEmpty() const {
00348             return values.empty();
00349         }
00350 
00354         virtual int size() const {
00355             return (int)values.size();
00356         }
00357 
00361         virtual E get( int index ) const {
00362 
00363             if( index < 0 || index >= this->size() ) {
00364                 throw decaf::lang::exceptions::IndexOutOfBoundsException(
00365                     __FILE__, __LINE__,
00366                     "List::get - Index greater than size() or negative" );
00367             }
00368 
00369             // Advance from begin and return the value at that location.
00370             typename std::list<E>::const_iterator iter = this->values.begin();
00371             std::advance( iter, index );
00372             return *( iter );
00373         }
00374 
00378         virtual E set( int index, const E& element ) {
00379 
00380             if( index < 0 || index >= this->size() ) {
00381                 throw decaf::lang::exceptions::IndexOutOfBoundsException(
00382                     __FILE__, __LINE__,
00383                     "List::get - Index greater than size() or negative" );
00384             }
00385 
00386             // Advance from begin and return the value at that location
00387             // after setting the value to the new value.
00388             typename std::list<E>::iterator iter = this->values.begin();
00389             std::advance( iter, index );
00390             E oldValue = *iter;
00391             *iter = element;
00392 
00393             return oldValue;
00394         }
00395 
00396         virtual void add( int index, const E& element ) {
00397 
00398             if( index < 0 || index > this->size() ) {
00399                 throw decaf::lang::exceptions::IndexOutOfBoundsException(
00400                     __FILE__, __LINE__,
00401                     "List::add - Index greater than size()" );
00402             }
00403 
00404             // Advance from begin and insert the value at that location
00405             typename std::list<E>::iterator iter = this->values.begin();
00406             std::advance( iter, index );
00407             this->values.insert( iter, element );
00408         }
00409 
00410         virtual bool add( const E& value ) {
00411             values.insert( values.end(), value );
00412             return true;
00413         }
00414 
00415         virtual bool addAll( const Collection<E>& collection ) {
00416 
00417             if( collection.isEmpty() ) {
00418                 return false;
00419             }
00420 
00421             std::vector<E> array = collection.toArray();
00422             typename std::vector<E>::const_iterator vecIter = array.begin();
00423 
00424             std::auto_ptr< ListIterator<E> > iter( this->listIterator( (int)this->values.size() ) );
00425 
00426             while( vecIter != array.end() ) {
00427                 iter->add( *( vecIter++) );
00428             }
00429 
00430             return true;
00431         }
00432 
00436         virtual bool addAll( int index, const Collection<E>& collection ) {
00437 
00438             if( index < 0 || index > this->size() ) {
00439                 throw decaf::lang::exceptions::IndexOutOfBoundsException(
00440                     __FILE__, __LINE__,
00441                     "List::addAll - Index greater than size()" );
00442             }
00443 
00444             if( collection.isEmpty() ) {
00445                 return false;
00446             }
00447 
00448             std::vector<E> array = collection.toArray();
00449             typename std::vector<E>::const_iterator vecIter = array.begin();
00450 
00451             std::auto_ptr< ListIterator<E> > iter( this->listIterator( index ) );
00452 
00453             while( vecIter != array.end() ) {
00454                 iter->add( *( vecIter++) );
00455             }
00456 
00457             return true;
00458         }
00459 
00463         virtual bool remove( const E& value ) {
00464 
00465             int origSize = this->size();
00466             this->values.remove( value );
00467             return origSize != this->size();
00468         }
00469 
00473         virtual E removeAt( int index ) {
00474 
00475             if( index < 0 || index >= this->size() ) {
00476                 throw decaf::lang::exceptions::IndexOutOfBoundsException(
00477                     __FILE__, __LINE__,
00478                     "List::removeAt - Index greater than size() or negative" );
00479             }
00480 
00481             // Advance from begin and insert the value at that location
00482             typename std::list<E>::iterator iter = this->values.begin();
00483             std::advance( iter, index );
00484             E oldValue = *iter;
00485             this->values.erase( iter );
00486 
00487             return oldValue;
00488         }
00489 
00490         virtual int indexOf( const E& value ) const {
00491 
00492             typename std::list<E>::const_iterator iter;
00493             iter = std::find( values.begin(), values.end(), value );
00494 
00495             if( iter == values.end() ) {
00496                 return -1;
00497             }
00498 
00499             return (int)std::distance( values.begin(), iter );
00500         }
00501 
00502         virtual int lastIndexOf( const E& value ) const {
00503 
00504             typename std::list<E>::const_reverse_iterator iter;
00505             iter = std::find( values.rbegin(), values.rend(), value );
00506 
00507             if( iter == values.rend() ) {
00508                 return -1;
00509             }
00510 
00511             // Now reverse the result to get the last index
00512             return (int)( this->size() - std::distance( values.rbegin(), iter ) - 1 );
00513         }
00514 
00515         virtual bool contains( const E& value ) const {
00516             typename std::list<E>::const_iterator iter;
00517             iter = std::find( values.begin(), values.end(), value );
00518             return iter != values.end();
00519         }
00520 
00521     };
00522 
00523 }}
00524 
00525 #endif /*_DECAF_UTIL_STLLIST_H_*/