activemq-cpp-3.8.2
SynchronousQueue.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_SYNCHRONOUSQUEUE_H_
19 #define _DECAF_UTIL_CONCURRENT_SYNCHRONOUSQUEUE_H_
20 
21 #include <decaf/util/Config.h>
22 
24 
25 #include <vector>
26 
27 namespace decaf {
28 namespace util {
29 namespace concurrent {
30 
63  template< typename E >
64  class SynchronousQueue : public BlockingQueue<E> {
65  private:
66 
67  class EmptyIterator : public Iterator<E> {
68  public:
69 
70  virtual E next() {
71 
73  __FILE__, __LINE__,
74  "Cannot traverse a Synchronous Queue." );
75  }
76 
77  virtual bool hasNext() const {
78  return false;
79  }
80 
81  virtual void remove() {
82 
84  __FILE__, __LINE__,
85  "No Elements to remove from a Synchronous Queue." );
86  }
87  };
88 
89  private:
90 
92  SynchronousQueue& operator= ( const SynchronousQueue& );
93 
94  public:
95 
97 
98  virtual ~SynchronousQueue() {}
99 
110  virtual void put( const E& value ) {
111 
112  //if (o == null) throw new NullPointerException();
113  //if (transferer.transfer(o, false, 0) == null) {
114  // Thread.interrupted();
115  // throw new InterruptedException();
116  //}
117  }
118 
130  virtual bool offer( const E& e, long long timeout, const TimeUnit& unit ) {
131 
132  //if (o == null) throw new NullPointerException();
133  //if (transferer.transfer(o, true, unit.toNanos(timeout)) != null)
134  // return true;
135  //if (!Thread.interrupted())
136  // return false;
137  //throw new InterruptedException();
138 
139  throw false;
140  }
141 
156  virtual bool offer( const E& value ) {
157 
158  //if (e == null) throw new NullPointerException();
159  //return transferer.transfer(e, true, 0) != null;
160 
161  return false;
162  }
163 
171  virtual E take() {
172  //Object e = transferer.transfer(null, false, 0);
173  //if (e != null)
174  // return (E)e;
175  //Thread.interrupted();
176  //throw new InterruptedException();
177 
178  return E();
179  }
180 
195  virtual bool poll( E& result, long long timeout, const TimeUnit& unit ) {
196 
197  //Object e = transferer.transfer(null, true, unit.toNanos(timeout));
198  //if (e != null || !Thread.interrupted())
199  // return (E)e;
200  //throw new InterruptedException();
201 
202  return false;
203  }
204 
215  virtual bool poll( E& result ) {
216  return false; // (E)transferer.transfer(null, true, 0);
217  }
218 
219  virtual bool equals( const Collection<E>& value ) const {
220  if( (void*)&value == this ) {
221  return true;
222  }
223 
224  return false;
225  }
226 
228  return new EmptyIterator();
229  }
230 
232  return new EmptyIterator();
233  }
234 
235  virtual bool isEmpty() const {
236  return true;
237  }
238 
239  virtual int size() const {
240  return 0;
241  }
242 
243  virtual int remainingCapacity() const {
244  return 0;
245  }
246 
247  virtual void clear() {}
248 
249  virtual bool contains( const E& value DECAF_UNUSED ) const {
250  return false;
251  }
252 
253  virtual bool containsAll( const Collection<E>& collection ) const {
254  return collection.isEmpty();
255  }
256 
257  virtual bool remove( const E& value DECAF_UNUSED ) {
258  return false;
259  }
260 
261  virtual bool removeAll( const Collection<E>& collection DECAF_UNUSED ) {
262  return false;
263  }
264 
265  virtual bool retainAll( const Collection<E>& collection DECAF_UNUSED ) {
266  return false;
267  }
268 
269  virtual bool peek( E& result DECAF_UNUSED ) const {
270  return false;
271  }
272 
273  virtual std::vector<E> toArray() const { return std::vector<E>(); }
274 
275  virtual int drainTo( Collection<E>& c ) {
276 
277  if( (void*)&c == this ) {
279  __FILE__, __LINE__,
280  "Cannot drain a Collection to Itself." );
281  }
282 
283  int count = 0;
284  E element;
285 
286  while( ( poll( element ) ) != false ) {
287  c.add( element );
288  ++count;
289  }
290 
291  return count;
292  }
293 
294  virtual int drainTo( Collection<E>& c, int maxElements ) {
295 
296  if( (void*)&c == this ) {
298  __FILE__, __LINE__,
299  "Cannot drain a Collection to Itself." );
300  }
301 
302  int count = 0;
303  E element;
304 
305  while( count < maxElements && ( poll( element ) != false ) ) {
306  c.add( element );
307  ++count;
308  }
309 
310  return count;
311  }
312 
313  };
314 
315 }}}
316 
317 #endif /* _DECAF_UTIL_CONCURRENT_SYNCHRONOUSQUEUE_H_ */
virtual std::vector< E > toArray() const
Answers an STL vector containing copies of all elements contained in this Collection.
Definition: SynchronousQueue.h:273
virtual bool equals(const Collection< E > &value) const
Answers true if this Collection and the one given are the same size and if each element contained in ...
Definition: SynchronousQueue.h:219
virtual bool peek(E &result DECAF_UNUSED) const
Definition: SynchronousQueue.h:269
virtual E element() const
Gets but not removes the element in the head of the queue.Throws a NoSuchElementException if there is...
Definition: AbstractQueue.h:112
The root interface in the collection hierarchy.
Definition: Collection.h:68
A decaf::util::Queue that additionally supports operations that wait for the queue to become non-empt...
Definition: BlockingQueue.h:164
virtual bool retainAll(const Collection< E > &collection DECAF_UNUSED)
Definition: SynchronousQueue.h:265
SynchronousQueue()
Definition: SynchronousQueue.h:96
virtual void put(const E &value)
Adds the specified element to this queue, waiting if necessary for another thread to receive it...
Definition: SynchronousQueue.h:110
virtual bool isEmpty() const =0
virtual bool contains(const E &value DECAF_UNUSED) const
Definition: SynchronousQueue.h:249
Defines an object that can be used to iterate over the elements of a collection.
Definition: Iterator.h:34
virtual int size() const
Returns the number of elements in this collection.
Definition: SynchronousQueue.h:239
virtual bool isEmpty() const
Returns true if this collection contains no elements.
Definition: SynchronousQueue.h:235
virtual int remainingCapacity() const
Returns the number of additional elements that this queue can ideally (in the absence of memory or re...
Definition: SynchronousQueue.h:243
A TimeUnit represents time durations at a given unit of granularity and provides utility methods to c...
Definition: TimeUnit.h:62
virtual ~SynchronousQueue()
Definition: SynchronousQueue.h:98
virtual bool add(const E &value)=0
Returns true if this collection changed as a result of the call.
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: SynchronousQueue.h:253
virtual bool poll(E &result, long long timeout, const TimeUnit &unit)
Retrieves and removes the head of this queue, waiting if necessary up to the specified wait time...
Definition: SynchronousQueue.h:195
virtual bool poll(E &result)
Retrieves and removes the head of this queue, if another thread is currently making an element availa...
Definition: SynchronousQueue.h:215
virtual E take()
Retrieves and removes the head of this queue, waiting if necessary for another thread to insert it...
Definition: SynchronousQueue.h:171
virtual int drainTo(Collection< E > &c, int maxElements)
Removes at most the given number of available elements from this queue and adds them to the given col...
Definition: SynchronousQueue.h:294
Definition: IllegalArgumentException.h:31
Definition: IllegalStateException.h:32
virtual bool offer(const E &e, long long timeout, const TimeUnit &unit)
Inserts the specified element into this queue, waiting if necessary up to the specified wait time for...
Definition: SynchronousQueue.h:130
virtual int drainTo(Collection< E > &c)
Removes all available elements from this queue and adds them to the given collection.
Definition: SynchronousQueue.h:275
#define DECAF_UNUSED
Definition: Config.h:160
Definition: NoSuchElementException.h:31
virtual decaf::util::Iterator< E > * iterator()
Definition: SynchronousQueue.h:227
A blocking queue in which each insert operation must wait for a corresponding remove operation by ano...
Definition: SynchronousQueue.h:64
virtual bool offer(const E &value)
Inserts the specified element into this queue, if another thread is waiting to receive it...
Definition: SynchronousQueue.h:156
virtual void clear()
Removes all of the elements from this collection (optional operation).The collection will be empty af...
Definition: SynchronousQueue.h:247
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements...
Definition: AprPool.h:25
virtual decaf::util::Iterator< E > * iterator() const
Definition: SynchronousQueue.h:231
virtual bool removeAll(const Collection< E > &collection DECAF_UNUSED)
Definition: SynchronousQueue.h:261