activemq-cpp-3.8.2
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 
65  AbstractCollection() : Collection<E>(), mutex() {}
66 
67  virtual ~AbstractCollection() {}
68 
77  this->clear();
78 
79  std::auto_ptr< Iterator<E> > iter( collection.iterator() );
80  while( iter->hasNext() ) {
81  this->add( iter->next() );
82  }
83 
84  return *this;
85  }
86 
102  virtual void clear() {
103 
104  std::auto_ptr< Iterator<E> > iter( this->iterator() );
105  while( iter->hasNext() ) {
106  iter->next();
107  iter->remove();
108  }
109  }
110 
117  virtual bool contains( const E& value ) const {
118 
119  bool result = false;
120  std::auto_ptr< Iterator<E> > iter( this->iterator() );
121  while( iter->hasNext() ) {
122  if( iter->next() == value ) {
123  result = true;
124  }
125  }
126 
127  return result;
128  }
129 
137  virtual bool containsAll( const Collection<E>& collection ) const {
138 
139  std::auto_ptr< Iterator<E> > iter( collection.iterator() );
140  while( iter->hasNext() ) {
141  if( !this->contains( iter->next() ) ) {
142  return false;
143  }
144  }
145 
146  return true;
147  }
148 
158  virtual bool equals( const Collection<E>& collection ) const {
159 
160  if( this == &collection ) {
161  return true;
162  }
163 
164  if( this->size() == collection.size() && this->containsAll( collection ) ) {
165  return true;
166  }
167 
168  return false;
169  }
170 
184  virtual void copy( const Collection<E>& collection ) {
185  this->clear();
186 
187  std::auto_ptr< Iterator<E> > iter( collection.iterator() );
188  while( iter->hasNext() ) {
189  this->add( iter->next() );
190  }
191  }
192 
200  virtual bool isEmpty() const {
201  return this->size() == 0;
202  }
203 
209  virtual bool add( const E& value DECAF_UNUSED ) {
211  __FILE__, __LINE__, "AbstractCollection add is not implemented.");
212  }
213 
223  virtual bool addAll( const Collection<E>& collection ) {
224 
225  bool result = false;
226  std::auto_ptr< Iterator<E> > iter( collection.iterator() );
227  while( iter->hasNext() ) {
228  result = this->add( iter->next() ) || result;
229  }
230 
231  return result;
232  }
233 
245  virtual bool remove( const E& value ) {
246 
247  std::auto_ptr< Iterator<E> > iter( this->iterator() );
248  while( iter->hasNext() ) {
249  if( value == iter->next() ) {
250  iter->remove();
251  return true;
252  }
253  }
254 
255  return false;
256  }
257 
269  virtual bool removeAll( const Collection<E>& collection ) {
270 
271  bool result = false;
272  std::auto_ptr< Iterator<E> > iter( this->iterator() );
273  while( iter->hasNext() ) {
274  if( collection.contains( iter->next() ) ) {
275  iter->remove();
276  result = true;
277  }
278  }
279 
280  return result;
281  }
282 
294  virtual bool retainAll( const Collection<E>& collection ) {
295 
296  bool result = false;
297  std::auto_ptr< Iterator<E> > iter( this->iterator() );
298  while( iter->hasNext() ) {
299  if( !collection.contains( iter->next() ) ) {
300  iter->remove();
301  result = true;
302  }
303  }
304 
305  return result;
306  }
307 
316  virtual std::vector<E> toArray() const {
317  std::vector<E> valueArray;
318  valueArray.reserve( this->size() );
319 
320  std::auto_ptr< Iterator<E> > iter( this->iterator() );
321  while( iter->hasNext() ) {
322  valueArray.push_back( iter->next() );
323  }
324 
325  return valueArray;
326  }
327 
328  public:
329 
330  virtual void lock() {
331  mutex.lock();
332  }
333 
334  virtual bool tryLock() {
335  return mutex.tryLock();
336  }
337 
338  virtual void unlock() {
339  mutex.unlock();
340  }
341 
342  virtual void wait() {
343  mutex.wait();
344  }
345 
346  virtual void wait( long long millisecs ) {
347  mutex.wait( millisecs );
348  }
349 
350  virtual void wait( long long millisecs, int nanos ) {
351  mutex.wait( millisecs, nanos );
352  }
353 
354  virtual void notify() {
355  mutex.notify();
356  }
357 
358  virtual void notifyAll() {
359  mutex.notifyAll();
360  }
361 
362  };
363 
364 }}
365 
366 #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:330
virtual void notify()
Signals a waiter on this object that it can now wake up and continue.
Definition: AbstractCollection.h:354
virtual bool add(const E &value DECAF_UNUSED)
Definition: AbstractCollection.h:209
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:294
virtual void wait()
Waits on a signal from this object, which is generated by a call to Notify.
Definition: AbstractCollection.h:342
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 std::vector< E > toArray() const
Answers an STL vector containing copies of all elements contained in this Collection.
Definition: AbstractCollection.h:316
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:334
virtual void unlock()
Unlocks the object.
virtual void unlock()
Unlocks the object.
Definition: AbstractCollection.h:338
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.
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:158
Definition: UnsupportedOperationException.h:32
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:346
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:223
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:76
virtual void lock()
Locks the object.
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:350
virtual bool isEmpty() const
Returns true if this collection contains no elements.
Definition: AbstractCollection.h:200
util::concurrent::Mutex mutex
Definition: AbstractCollection.h:61
virtual bool removeAll(const Collection< E > &collection)
Removes all this collection&#39;s elements that are also contained in the specified collection (optional ...
Definition: AbstractCollection.h:269
virtual void clear()
Removes all of the elements from this collection (optional operation).
Definition: AbstractCollection.h:102
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:137
#define DECAF_UNUSED
Definition: Config.h:160
virtual ~AbstractCollection()
Definition: AbstractCollection.h:67
virtual void notifyAll()
Signals the waiters on this object that it can now wake up and continue.
Definition: AbstractCollection.h:358
virtual void copy(const Collection< E > &collection)
Renders this Collection as a Copy of the given Collection.
Definition: AbstractCollection.h:184
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements...
Definition: AprPool.h:25
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:117
AbstractCollection()
Definition: AbstractCollection.h:65