activemq-cpp-3.4.0

src/main/decaf/util/AbstractCollection.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_ABSTRACTCOLLECTION_H_
00019 #define _DECAF_UTIL_ABSTRACTCOLLECTION_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/Collection.h>
00028 #include <decaf/util/concurrent/Synchronizable.h>
00029 #include <decaf/util/concurrent/Mutex.h>
00030 #include <memory>
00031 
00032 namespace decaf {
00033 namespace util {
00034 
00057     template< typename E >
00058     class AbstractCollection : public virtual decaf::util::Collection<E> {
00059     protected:
00060 
00061         mutable util::concurrent::Mutex mutex;
00062 
00063     public:
00064 
00065         AbstractCollection() : Collection<E>(), mutex() {}
00066 
00067         virtual ~AbstractCollection() {}
00068 
00076         AbstractCollection<E>& operator= ( const AbstractCollection<E>& collection ) {
00077             this->clear();
00078 
00079             std::auto_ptr< Iterator<E> > iter( collection.iterator() );
00080             while( iter->hasNext() ) {
00081                 this->add( iter->next() );
00082             }
00083 
00084             return *this;
00085         }
00086 
00102         virtual void clear() {
00103 
00104             std::auto_ptr< Iterator<E> > iter( this->iterator() );
00105             while( iter->hasNext() ) {
00106                 iter->next();
00107                 iter->remove();
00108             }
00109         }
00110 
00117         virtual bool contains( const E& value ) const {
00118 
00119             bool result = false;
00120             std::auto_ptr< Iterator<E> > iter( this->iterator() );
00121             while( iter->hasNext() ) {
00122                 if( iter->next() == value ) {
00123                     result = true;
00124                 }
00125             }
00126 
00127             return result;
00128         }
00129 
00137         virtual bool containsAll( const Collection<E>& collection ) const {
00138 
00139             std::auto_ptr< Iterator<E> > iter( collection.iterator() );
00140             while( iter->hasNext() ) {
00141                 if( !this->contains( iter->next() ) ) {
00142                     return false;
00143                 }
00144             }
00145 
00146             return true;
00147         }
00148 
00158         virtual bool equals( const Collection<E>& collection ) const {
00159 
00160             if( this == &collection ) {
00161                 return true;
00162             }
00163 
00164             if( this->size() == collection.size() && this->containsAll( collection ) ) {
00165                 return true;
00166             }
00167 
00168             return false;
00169         }
00170 
00184         virtual void copy( const Collection<E>& collection ) {
00185             this->clear();
00186 
00187             std::auto_ptr< Iterator<E> > iter( collection.iterator() );
00188             while( iter->hasNext() ) {
00189                 this->add( iter->next() );
00190             }
00191         }
00192 
00200         virtual bool isEmpty() const {
00201             return this->size() == 0;
00202         }
00203 
00209         virtual bool add( const E& value DECAF_UNUSED ) {
00210             throw decaf::lang::exceptions::UnsupportedOperationException(
00211                 __FILE__, __LINE__, "AbstractCollection add is not implemented.");
00212         }
00213 
00223         virtual bool addAll( const Collection<E>& collection ) {
00224 
00225             bool result = false;
00226             std::auto_ptr< Iterator<E> > iter( collection.iterator() );
00227             while( iter->hasNext() ) {
00228                 result = this->add( iter->next() ) || result;
00229             }
00230 
00231             return result;
00232         }
00233 
00245         virtual bool remove( const E& value ) {
00246 
00247             std::auto_ptr< Iterator<E> > iter( this->iterator() );
00248             while( iter->hasNext() ) {
00249                 if( value == iter->next() ) {
00250                     iter->remove();
00251                     return true;
00252                 }
00253             }
00254 
00255             return false;
00256         }
00257 
00269         virtual bool removeAll( const Collection<E>& collection ) {
00270 
00271             bool result = false;
00272             std::auto_ptr< Iterator<E> > iter( this->iterator() );
00273             while( iter->hasNext() ) {
00274                 if( collection.contains( iter->next() ) ) {
00275                     iter->remove();
00276                     result = true;
00277                 }
00278             }
00279 
00280             return result;
00281         }
00282 
00294         virtual bool retainAll( const Collection<E>& collection ) {
00295 
00296             bool result = false;
00297             std::auto_ptr< Iterator<E> > iter( this->iterator() );
00298             while( iter->hasNext() ) {
00299                 if( !collection.contains( iter->next() ) ) {
00300                     iter->remove();
00301                     result = true;
00302                 }
00303             }
00304 
00305             return result;
00306         }
00307 
00316         virtual std::vector<E> toArray() const {
00317             std::vector<E> valueArray;
00318             valueArray.reserve( this->size() );
00319 
00320             std::auto_ptr< Iterator<E> > iter( this->iterator() );
00321             while( iter->hasNext() ) {
00322                 valueArray.push_back( iter->next() );
00323             }
00324 
00325             return valueArray;
00326         }
00327 
00328     public:
00329 
00330         virtual void lock() {
00331             mutex.lock();
00332         }
00333 
00334         virtual bool tryLock() {
00335             return mutex.tryLock();
00336         }
00337 
00338         virtual void unlock() {
00339             mutex.unlock();
00340         }
00341 
00342         virtual void wait()  {
00343             mutex.wait();
00344         }
00345 
00346         virtual void wait( long long millisecs ) {
00347             mutex.wait( millisecs );
00348         }
00349 
00350         virtual void wait( long long millisecs, int nanos ) {
00351             mutex.wait( millisecs, nanos );
00352         }
00353 
00354         virtual void notify() {
00355             mutex.notify();
00356         }
00357 
00358         virtual void notifyAll() {
00359             mutex.notifyAll();
00360         }
00361 
00362     };
00363 
00364 }}
00365 
00366 #endif /*_DECAF_UTIL_ABSTRACTCOLLECTION_H_*/