activemq-cpp-3.6.0
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 
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_*/