activemq-cpp-3.8.2
CopyOnWriteArraySet.h
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #ifndef _DECAF_UTIL_CONCURRENT_COPYONWRITEARRAYSET_H_
19 #define _DECAF_UTIL_CONCURRENT_COPYONWRITEARRAYSET_H_
20 
24 #include <decaf/lang/Pointer.h>
27 #include <decaf/util/Set.h>
28 #include <decaf/util/Arrays.h>
29 #include <decaf/util/AbstractSet.h>
30 
31 namespace decaf {
32 namespace util {
33 namespace concurrent {
34 
48  template< typename E >
49  class CopyOnWriteArraySet : public AbstractSet<E> {
50  private:
51 
53 
54  public:
55 
56  CopyOnWriteArraySet() : AbstractSet<E> (), array() {
57  }
58 
59  CopyOnWriteArraySet(const Collection<E>& collection) : AbstractSet<E> (), array() {
60  this->copy(collection);
61  }
62 
63  CopyOnWriteArraySet(const E* array, int size) : AbstractSet<E> (), array() {
64  for (int i = 0; i < size; ++i) {
65  this->array.addIfAbsent(array[i]);
66  }
67  }
68 
70  }
71 
72  public:
73 
74  virtual void copy(const Collection<E>& collection) {
75  this->array.copy(collection);
76  }
77 
79  return this->array.iterator();
80  }
81 
83  return this->array.iterator();
84  }
85 
86  virtual int size() const {
87  return this->array.size();
88  }
89 
90  virtual bool isEmpty() const {
91  return this->array.isEmpty();
92  }
93 
94  virtual bool add(const E& value) {
95  return this->array.addIfAbsent(value);
96  }
97 
98  virtual bool addAll(const Collection<E>& collection) {
99  return this->array.addAllAbsent(collection) > 0 ? true : false;
100  }
101 
102  virtual void clear() {
103  this->array.clear();
104  }
105 
106  virtual bool contains(const E& value) const {
107  return this->array.contains(value);
108  }
109 
110  virtual bool containsAll(const Collection<E>& collection) const {
111  return this->array.containsAll(collection);
112  }
113 
114  virtual bool remove(const E& value) {
115  return this->array.remove(value);
116  }
117 
118  virtual bool removeAll(const Collection<E>& collection) {
119  return this->array.removeAll(collection);
120  }
121 
122  virtual bool retainAll(const Collection<E>& collection) {
123  return this->array.retainAll(collection);
124  }
125 
126  virtual std::vector<E> toArray() const {
127  return this->array.toArray();
128  }
129 
130  virtual bool equals(const Collection<E>& collection) const {
131 
132  if ((void*) this == &collection) {
133  return true;
134  }
135 
136  const Set<E>* asSet = dynamic_cast<const Set<E>*> (&collection);
137  if (asSet == NULL) {
138  return false;
139  }
140 
141  if (this->size() != asSet->size()) {
142  return false;
143  }
144 
145  std::auto_ptr<Iterator<E> > setIter(asSet->iterator());
146 
147  // Use a single snapshot of underlying array
148  CopyOnWriteArrayList<E> array(this->array);
149  int length = array.size();
150  decaf::lang::ArrayPointer<bool> matched(length, false);
151 
152  while (setIter->hasNext()) {
153  E value = setIter->next();
154  int matchedAt = array.indexOf(value);
155  if (matchedAt >= 0) {
156  matched[matchedAt] = true;
157  }
158  }
159 
160  for (int i = 0; i < length; ++i) {
161  if (matched[i] == false) {
162  return false;
163  }
164  }
165 
166  return true;
167  }
168 
169  };
170 
171 }}}
172 
173 #endif /* _DECAF_UTIL_CONCURRENT_COPYONWRITEARRAYSET_H_ */
virtual std::vector< E > toArray() const
Returns an array containing all of the elements in this collection.
Definition: CopyOnWriteArrayList.h:490
virtual ~CopyOnWriteArraySet()
Definition: CopyOnWriteArraySet.h:69
The root interface in the collection hierarchy.
Definition: Collection.h:68
virtual bool contains(const E &value) const
Returns true if this collection contains the specified element.More formally, returns true if and onl...
Definition: CopyOnWriteArraySet.h:106
virtual void copy(const Collection< E > &collection)
Renders this Collection as a Copy of the given Collection.
Definition: CopyOnWriteArraySet.h:74
virtual int size() const
Returns the number of elements in this collection.
Definition: CopyOnWriteArrayList.h:476
A collection that contains no duplicate elements.
Definition: Set.h:45
virtual decaf::util::Iterator< E > * iterator() const
Definition: CopyOnWriteArraySet.h:82
virtual bool containsAll(const Collection< E > &collection) const
Returns true if this collection contains all of the elements in the specified collection.The Collection to compare to this one.if the Collection contains pointers and the Collection does not allow for NULL elements (optional check).This implementation iterates over the specified collection, checking each element returned by the iterator in turn to see if it&#39;s contained in this collection. If all elements are so contained true is returned, otherwise false.
Definition: CopyOnWriteArraySet.h:110
#define NULL
Definition: Config.h:33
virtual bool addAll(const Collection< E > &collection)
Adds all of the elements in the specified collection to this collection.The behavior of this operatio...
Definition: CopyOnWriteArraySet.h:98
virtual bool retainAll(const Collection< E > &collection)
Retains only the elements in this collection that are contained in the specified collection (optional...
Definition: CopyOnWriteArrayList.h:433
Definition: CopyOnWriteArrayList.h:35
virtual int size() const =0
Returns the number of elements in this collection.
virtual bool equals(const Collection< E > &collection) const
Answers true if this Collection and the one given are the same size and if each element contained in ...
Definition: CopyOnWriteArraySet.h:130
Defines an object that can be used to iterate over the elements of a collection.
Definition: Iterator.h:34
virtual decaf::util::Iterator< E > * iterator()
Definition: CopyOnWriteArraySet.h:78
int addAllAbsent(const Collection< E > &collection)
Every element in the given collection that is not already contained in this Collection is added to th...
Definition: CopyOnWriteArrayList.h:821
virtual decaf::util::Iterator< E > * iterator()
Definition: CopyOnWriteArrayList.h:510
virtual void clear()
Removes all of the elements from this collection (optional operation).
Definition: CopyOnWriteArrayList.h:279
CopyOnWriteArraySet()
Definition: CopyOnWriteArraySet.h:56
virtual decaf::util::Iterator< E > * iterator()=0
virtual bool add(const E &value)
Returns true if this collection changed as a result of the call.
Definition: CopyOnWriteArraySet.h:94
virtual bool isEmpty() const
Returns true if this collection contains no elements.
Definition: CopyOnWriteArraySet.h:90
Since the CopyOnWriteArraySet and the CopyOnWriteArrayList share much of the same operational semanti...
Definition: CopyOnWriteArraySet.h:49
Decaf&#39;s implementation of a Smart Pointer that is a template on a Type and is Thread Safe if the defa...
Definition: ArrayPointer.h:51
bool addIfAbsent(const E &value)
Adds the given value to the end of this List if it is not already contained in this List...
Definition: CopyOnWriteArrayList.h:786
virtual bool remove(const E &value)
Removes a single instance of the specified element from the collection.
Definition: CopyOnWriteArrayList.h:376
virtual bool retainAll(const Collection< E > &collection)
Retains only the elements in this collection that are contained in the specified collection (optional...
Definition: CopyOnWriteArraySet.h:122
virtual bool removeAll(const Collection< E > &collection)
Removes all this collection&#39;s elements that are also contained in the specified collection (optional ...
Definition: CopyOnWriteArrayList.h:393
virtual void copy(const Collection< E > &collection)
Renders this Collection as a Copy of the given Collection.
Definition: CopyOnWriteArrayList.h:231
virtual bool contains(const E &value) const
Returns true if this collection contains the specified element.
Definition: CopyOnWriteArrayList.h:292
virtual std::vector< E > toArray() const
Answers an STL vector containing copies of all elements contained in this Collection.
Definition: CopyOnWriteArraySet.h:126
This class provides a skeletal implementation of the Set interface to minimize the effort required to...
Definition: AbstractSet.h:46
virtual int size() const
Returns the number of elements in this collection.
Definition: CopyOnWriteArraySet.h:86
virtual void clear()
Removes all of the elements from this collection (optional operation).
Definition: CopyOnWriteArraySet.h:102
virtual bool containsAll(const Collection< E > &collection) const
Returns true if this collection contains all of the elements in the specified collection.
Definition: CopyOnWriteArrayList.h:312
CopyOnWriteArraySet(const E *array, int size)
Definition: CopyOnWriteArraySet.h:63
virtual bool removeAll(const Collection< E > &collection)
Removes all this collection&#39;s elements that are also contained in the specified collection (optional ...
Definition: CopyOnWriteArraySet.h:118
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements...
Definition: AprPool.h:25
virtual bool isEmpty() const
Definition: CopyOnWriteArrayList.h:362
CopyOnWriteArraySet(const Collection< E > &collection)
Definition: CopyOnWriteArraySet.h:59