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