activemq-cpp-3.9.5
AbstractCollection.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_ABSTRACTCOLLECTION_H_
19 #define _DECAF_UTIL_ABSTRACTCOLLECTION_H_
20 
21 #include <decaf/util/Config.h>
25 #include <decaf/lang/Iterable.h>
26 #include <decaf/util/Iterator.h>
27 #include <decaf/util/Collection.h>
30 #include <memory>
31 
32 namespace decaf {
33 namespace util {
34 
57  template< typename E >
58  class AbstractCollection : public virtual decaf::util::Collection<E> {
59  protected:
60 
62 
63  public:
64 
66 
74  if (other.isEmpty()) {
75  return;
76  }
77  std::auto_ptr<Iterator<E> > iter(other.iterator());
78  while (iter->hasNext()) {
79  this->add(iter->next());
80  }
81  }
82 
83  virtual ~AbstractCollection() {}
84 
93  this->clear();
94 
95  std::auto_ptr<Iterator<E> > iter(collection.iterator());
96  while (iter->hasNext()) {
97  this->add(iter->next());
98  }
99 
100  return *this;
101  }
102 
118  virtual void clear() {
119  std::auto_ptr<Iterator<E> > iter(this->iterator());
120  while (iter->hasNext()) {
121  iter->next();
122  iter->remove();
123  }
124  }
125 
132  virtual bool contains(const E& value) const {
133  bool result = false;
134  std::auto_ptr<Iterator<E> > iter(this->iterator());
135  while (iter->hasNext()) {
136  if (iter->next() == value) {
137  result = true;
138  }
139  }
140 
141  return result;
142  }
143 
151  virtual bool containsAll(const Collection<E>& collection) const {
152 
153  std::auto_ptr<Iterator<E> > iter(collection.iterator());
154  while (iter->hasNext()) {
155  if (!this->contains(iter->next())) {
156  return false;
157  }
158  }
159 
160  return true;
161  }
162 
172  virtual bool equals(const Collection<E>& collection) const {
173 
174  if (this == &collection) {
175  return true;
176  }
177 
178  if (this->size() == collection.size() && this->containsAll(collection)) {
179  return true;
180  }
181 
182  return false;
183  }
184 
198  virtual void copy(const Collection<E>& collection) {
199  this->clear();
200 
201  std::auto_ptr<Iterator<E> > iter(collection.iterator());
202  while (iter->hasNext()) {
203  this->add(iter->next());
204  }
205  }
206 
214  virtual bool isEmpty() const {
215  return this->size() == 0;
216  }
217 
223  virtual bool add(const E& value DECAF_UNUSED) {
225  __FILE__, __LINE__, "AbstractCollection add is not implemented.");
226  }
227 
237  virtual bool addAll(const Collection<E>& collection) {
238 
239  bool result = false;
240  std::auto_ptr<Iterator<E> > iter(collection.iterator());
241  while (iter->hasNext()) {
242  result = this->add(iter->next()) || result;
243  }
244 
245  return result;
246  }
247 
259  virtual bool remove(const E& value) {
260 
261  std::auto_ptr<Iterator<E> > iter(this->iterator());
262  while (iter->hasNext()) {
263  if (value == iter->next()) {
264  iter->remove();
265  return true;
266  }
267  }
268 
269  return false;
270  }
271 
283  virtual bool removeAll(const Collection<E>& collection) {
284 
285  bool result = false;
286  std::auto_ptr<Iterator<E> > iter(this->iterator());
287  while (iter->hasNext()) {
288  if (collection.contains(iter->next())) {
289  iter->remove();
290  result = true;
291  }
292  }
293 
294  return result;
295  }
296 
308  virtual bool retainAll(const Collection<E>& collection) {
309 
310  bool result = false;
311  std::auto_ptr<Iterator<E> > iter(this->iterator());
312  while (iter->hasNext()) {
313  if (!collection.contains(iter->next())) {
314  iter->remove();
315  result = true;
316  }
317  }
318 
319  return result;
320  }
321 
330  virtual std::vector<E> toArray() const {
331  std::vector<E> valueArray;
332  valueArray.reserve((std::size_t) this->size());
333 
334  std::auto_ptr<Iterator<E> > iter(this->iterator());
335  while (iter->hasNext()) {
336  valueArray.push_back(iter->next());
337  }
338 
339  return valueArray;
340  }
341 
342  public:
343 
344  virtual void lock() {
345  mutex.lock();
346  }
347 
348  virtual bool tryLock() {
349  return mutex.tryLock();
350  }
351 
352  virtual void unlock() {
353  mutex.unlock();
354  }
355 
356  virtual void wait() {
357  mutex.wait();
358  }
359 
360  virtual void wait(long long millisecs) {
361  mutex.wait(millisecs);
362  }
363 
364  virtual void wait(long long millisecs, int nanos) {
365  mutex.wait(millisecs, nanos);
366  }
367 
368  virtual void notify() {
369  mutex.notify();
370  }
371 
372  virtual void notifyAll() {
373  mutex.notifyAll();
374  }
375 
376  };
377 
378 }}
379 
380 #endif /*_DECAF_UTIL_ABSTRACTCOLLECTION_H_*/
The root interface in the collection hierarchy.
Definition: Collection.h:68
virtual bool tryLock()
Attempts to Lock the object, if the lock is already held by another thread than this method returns f...
virtual void lock()
Locks the object.
Definition: AbstractCollection.h:344
virtual void notify()
Signals a waiter on this object that it can now wake up and continue.
Definition: AbstractCollection.h:368
virtual bool add(const E &value DECAF_UNUSED)
Definition: AbstractCollection.h:223
virtual std::vector< E > toArray() const
Answers an STL vector containing copies of all elements contained in this Collection.
Definition: AbstractCollection.h:330
virtual bool retainAll(const Collection< E > &collection)
Retains only the elements in this collection that are contained in the specified collection (optional...
Definition: AbstractCollection.h:308
virtual void wait()
Waits on a signal from this object, which is generated by a call to Notify.
Definition: AbstractCollection.h:356
Mutex object that offers recursive support on all platforms as well as providing the ability to use t...
Definition: Mutex.h:39
This class provides a skeletal implementation of the Collection interface, to minimize the effort req...
Definition: AbstractCollection.h:58
virtual bool tryLock()
Attempts to Lock the object, if the lock is already held by another thread than this method returns f...
Definition: AbstractCollection.h:348
virtual void unlock()
Unlocks the object.
virtual void unlock()
Unlocks the object.
Definition: AbstractCollection.h:352
virtual int size() const =0
Returns the number of elements in this collection.
virtual void wait()
Waits on a signal from this object, which is generated by a call to Notify.
Definition: UnsupportedOperationException.h:32
AbstractCollection(const AbstractCollection &other)
Copy Constructor, copy element from the source collection to this collection after clearing any eleme...
Definition: AbstractCollection.h:73
virtual bool contains(const E &value) const =0
Returns true if this collection contains the specified element.
virtual void wait(long long millisecs)
Waits on a signal from this object, which is generated by a call to Notify.
Definition: AbstractCollection.h:360
virtual void notifyAll()
Signals the waiters on this object that it can now wake up and continue.
virtual decaf::util::Iterator< E > * iterator()=0
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: AbstractCollection.h:237
virtual void notify()
Signals a waiter on this object that it can now wake up and continue.
AbstractCollection< E > & operator=(const AbstractCollection< E > &collection)
Assignment Operator, copy element from the source collection to this collection after clearing any el...
Definition: AbstractCollection.h:92
virtual void lock()
Locks the object.
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).
Definition: AbstractCollection.h:151
virtual void wait(long long millisecs, int nanos)
Waits on a signal from this object, which is generated by a call to Notify.
Definition: AbstractCollection.h:364
virtual bool contains(const E &value) const
Returns true if this collection contains the specified element.More formally, returns true if and onl...
Definition: AbstractCollection.h:132
util::concurrent::Mutex mutex
Definition: AbstractCollection.h:61
virtual bool removeAll(const Collection< E > &collection)
Removes all this collection's elements that are also contained in the specified collection (optional ...
Definition: AbstractCollection.h:283
virtual void clear()
Removes all of the elements from this collection (optional operation).
Definition: AbstractCollection.h:118
#define DECAF_UNUSED
Definition: Config.h:160
virtual ~AbstractCollection()
Definition: AbstractCollection.h:83
virtual void notifyAll()
Signals the waiters on this object that it can now wake up and continue.
Definition: AbstractCollection.h:372
virtual bool isEmpty() const
Returns true if this collection contains no elements.
Definition: AbstractCollection.h:214
virtual void copy(const Collection< E > &collection)
Renders this Collection as a Copy of the given Collection.
Definition: AbstractCollection.h:198
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: AbstractCollection.h:172
AbstractCollection()
Definition: AbstractCollection.h:65