activemq-cpp-3.4.0

src/main/decaf/util/concurrent/CopyOnWriteArraySet.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_CONCURRENT_COPYONWRITEARRAYSET_H_
00019 #define _DECAF_UTIL_CONCURRENT_COPYONWRITEARRAYSET_H_
00020 
00021 #include <decaf/util/NoSuchElementException.h>
00022 #include <decaf/lang/exceptions/IndexOutOfBoundsException.h>
00023 #include <decaf/lang/ArrayPointer.h>
00024 #include <decaf/util/concurrent/CopyOnWriteArrayList.h>
00025 #include <decaf/util/concurrent/Synchronizable.h>
00026 #include <decaf/util/Set.h>
00027 #include <decaf/util/Arrays.h>
00028 #include <decaf/util/AbstractSet.h>
00029 
00030 namespace decaf {
00031 namespace util {
00032 namespace concurrent {
00033 
00034     using decaf::lang::ArrayPointer;
00035 
00049     template< typename E >
00050     class CopyOnWriteArraySet : public AbstractSet<E> {
00051     private:
00052 
00053         CopyOnWriteArrayList<E> array;
00054 
00055     public:
00056 
00057         CopyOnWriteArraySet() : AbstractSet<E>(), array() {
00058         }
00059 
00060         CopyOnWriteArraySet( const Collection<E>& collection ) : AbstractSet<E>(), array() {
00061             this->copy( collection );
00062         }
00063 
00064         CopyOnWriteArraySet( const E* array, int size ) : AbstractSet<E>(), array() {
00065 
00066             for( int i = 0; i < size; ++i ) {
00067                 this->array.addIfAbsent( array[i] );
00068             }
00069         }
00070 
00071         virtual ~CopyOnWriteArraySet() {}
00072 
00073     public:
00074 
00075         virtual void copy( const Collection<E>& collection ) {
00076             this->array.copy( collection );
00077         }
00078 
00079         virtual decaf::util::Iterator<E>* iterator() {
00080             return this->array.iterator();
00081         }
00082 
00083         virtual decaf::util::Iterator<E>* iterator() const {
00084             return this->array.iterator();
00085         }
00086 
00087         virtual int size() const {
00088             return this->array.size();
00089         }
00090 
00091         virtual bool isEmpty() const {
00092             return this->array.isEmpty();
00093         }
00094 
00095         virtual bool add( const E& value ) {
00096             return this->array.addIfAbsent( value );
00097         }
00098 
00099         virtual bool addAll( const Collection<E>& collection ) {
00100             return this->array.addAllAbsent( collection ) > 0 ? true : false;
00101         }
00102 
00103         virtual void clear() {
00104             this->array.clear();
00105         }
00106 
00107         virtual bool contains( const E& value ) const {
00108             return this->array.contains( value );
00109         }
00110 
00111         virtual bool containsAll( const Collection<E>& collection ) const {
00112             return this->array.containsAll( collection );
00113         }
00114 
00115         virtual bool remove( const E& value ) {
00116             return this->array.remove( value );
00117         }
00118 
00119         virtual bool removeAll( const Collection<E>& collection ) {
00120             return this->array.removeAll( collection );
00121         }
00122 
00123         virtual bool retainAll( const Collection<E>& collection ) {
00124             return this->array.retainAll( collection );
00125         }
00126 
00127         virtual std::vector<E> toArray() const {
00128             return this->array.toArray();
00129         }
00130 
00131         virtual bool equals( const Collection<E>& collection ) const {
00132 
00133             if( (void*)this == &collection ) {
00134                 return true;
00135             }
00136 
00137             const Set<E>* asSet = dynamic_cast<const Set<E>*>( &collection );
00138             if( asSet == NULL ) {
00139                 return false;
00140             }
00141 
00142             if( this->size() != asSet->size() ) {
00143                 return false;
00144             }
00145 
00146             std::auto_ptr< Iterator<E> > setIter( asSet->iterator() );
00147 
00148             //  Use a single snapshot of underlying array
00149             ArrayPointer<E> elements = this->array.getArray();
00150             int length = elements.length();
00151 
00152             ArrayPointer<bool> matched( length, false );
00153 
00154             while( setIter->hasNext() ) {
00155 
00156                 E value = setIter->next();
00157                 for( int i = 0; i < length; ++i ) {
00158                     if( !matched[i] && value == elements[i] ) {
00159                         matched[i] = true;
00160                         break;
00161                     }
00162                 }
00163             }
00164 
00165             for( int i = 0; i < length; ++i ) {
00166                 if( matched[i] == false ) {
00167                     return false;
00168                 }
00169             }
00170 
00171             return true;
00172         }
00173 
00174     };
00175 
00176 }}}
00177 
00178 #endif /* _DECAF_UTIL_CONCURRENT_COPYONWRITEARRAYSET_H_ */