activemq-cpp-3.3.0

src/main/decaf/util/StlSet.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_STLSET_H_
00019 #define _DECAF_UTIL_STLSET_H_
00020 
00021 #include <set>
00022 #include <vector>
00023 #include <memory>
00024 #include <decaf/util/NoSuchElementException.h>
00025 #include <decaf/util/concurrent/Synchronizable.h>
00026 #include <decaf/util/concurrent/Mutex.h>
00027 #include <decaf/util/Iterator.h>
00028 #include <decaf/util/AbstractSet.h>
00029 
00030 namespace decaf{
00031 namespace util{
00032 
00038     template <typename E>
00039     class StlSet : public decaf::util::AbstractSet<E> {
00040     private:
00041 
00042         std::set<E> values;
00043 
00044     private:
00045 
00046         class SetIterator : public Iterator<E> {
00047         private:
00048 
00049             typename std::set<E>::iterator current;
00050             typename std::set<E>::iterator previous;
00051             typename std::set<E>* set;
00052 
00053         private:
00054 
00055             SetIterator( const SetIterator& );
00056             SetIterator operator= ( const SetIterator& );
00057 
00058         public:
00059 
00060             SetIterator( typename std::set<E>* set ) :
00061                 Iterator<E>(), current( set->begin() ), previous( set->begin() ), set( set ) {
00062             }
00063 
00064             virtual ~SetIterator() {}
00065 
00066             virtual E next() {
00067                 if( this->current == set->end() ) {
00068                     throw NoSuchElementException(
00069                         __FILE__, __LINE__,
00070                         "Set::Iterator::next - No more elements to return" );
00071                 }
00072 
00073                 this->previous = this->current;
00074                 return *( this->current++ );
00075             }
00076 
00077             virtual bool hasNext() const {
00078                 return ( this->current != set->end() );
00079             }
00080 
00081             virtual void remove() {
00082                 if( this->previous == set->end() ) {
00083                     throw lang::exceptions::IllegalStateException(
00084                         __FILE__, __LINE__,
00085                         "Set::Iterator::remove - Invalid State to call remove" );
00086                 }
00087 
00088                 this->set->erase( this->previous );
00089                 this->previous = this->set->end();
00090             }
00091         };
00092 
00093         class ConstSetIterator : public Iterator<E> {
00094         private:
00095 
00096             typename std::set<E>::const_iterator current;
00097             typename std::set<E>::const_iterator previous;
00098             const typename std::set<E>* set;
00099 
00100         private:
00101 
00102             ConstSetIterator( const ConstSetIterator& );
00103             ConstSetIterator operator= ( const ConstSetIterator& );
00104 
00105         public:
00106 
00107             ConstSetIterator( const typename std::set<E>* set ) :
00108                 Iterator<E>(), current( set->begin() ), previous( set->begin() ), set( set ) {
00109             }
00110 
00111             virtual ~ConstSetIterator() {}
00112 
00113             virtual E next() {
00114                 if( this->current == set->end() ) {
00115                     throw NoSuchElementException(
00116                         __FILE__, __LINE__,
00117                         "Set::Iterator::next - No more elements to return" );
00118                 }
00119 
00120                 this->previous = this->current;
00121                 return *( this->current++ );
00122             }
00123 
00124             virtual bool hasNext() const {
00125                 return ( this->current != set->end() );
00126             }
00127 
00128             virtual void remove() {
00129                 throw lang::exceptions::UnsupportedOperationException(
00130                     __FILE__, __LINE__,
00131                     "Set::Iterator::remove - Not Valid on a Const Iterator" );
00132             }
00133         };
00134 
00135     public:
00136 
00140         StlSet() : AbstractSet<E>(), values() {}
00141 
00147         StlSet( const StlSet& source ) : AbstractSet<E>(), values() {
00148             copy( source );
00149         }
00150 
00156         StlSet( const Collection<E>& source ) : AbstractSet<E>(), values() {
00157             AbstractSet<E>::copy( source );
00158         }
00159 
00160         virtual ~StlSet() {}
00161 
00165         Iterator<E>* iterator() {
00166             return new SetIterator( &values );
00167         }
00168         Iterator<E>* iterator() const {
00169             return new ConstSetIterator( &values );
00170         }
00171 
00175         virtual bool equals( const Collection<E>& collection ) const {
00176 
00177             const StlSet<E>* setptr = dynamic_cast<const StlSet<E>*>( &collection );
00178             if( setptr == NULL ) {
00179                 return AbstractSet<E>::equals( collection );
00180             }
00181 
00182             return this->values == setptr->values;
00183         }
00184 
00188         virtual void copy( const Collection<E>& collection ) {
00189 
00190             const StlSet<E>* setptr = dynamic_cast<const StlSet<E>*>( &collection );
00191             if( setptr == NULL ) {
00192                 AbstractSet<E>::copy( collection );
00193                 return;
00194             }
00195 
00196             this->values.clear();
00197             this->values = setptr->values;
00198         }
00199 
00203         virtual void clear() {
00204             values.clear();
00205         }
00206 
00210         virtual bool contains( const E& value ) const {
00211             typename std::set<E>::const_iterator iter;
00212             iter = values.find( value );
00213             return iter != values.end();
00214         }
00215 
00219         virtual bool isEmpty() const {
00220             return values.empty();
00221         }
00222 
00226         virtual int size() const {
00227             return (int)values.size();
00228         }
00229 
00233         virtual bool add( const E& value ) {
00234             return values.insert( value ).second;
00235         }
00236 
00240         virtual bool remove( const E& value ) {
00241             return values.erase( value ) != 0;
00242         }
00243 
00244     };
00245 
00246 }}
00247 
00248 #endif /*_DECAF_UTIL_STLSET_H_*/