Hazelcast C++ Client
 All Classes Functions Variables Enumerations Pages
IQueue.h
1 /*
2  * Copyright (c) 2008-2015, Hazelcast, Inc. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #ifndef HAZELCAST_IQUEUE
17 #define HAZELCAST_IQUEUE
18 
19 #include "hazelcast/client/proxy/IQueueImpl.h"
20 #include "hazelcast/client/ItemListener.h"
21 #include "hazelcast/client/impl/ItemEventHandler.h"
22 #include <stdexcept>
23 #include "hazelcast/client/protocol/codec/QueueAddListenerCodec.h"
24 
25 namespace hazelcast {
26  namespace client {
27 
33  template<typename E>
34  class IQueue : public proxy::IQueueImpl {
35  friend class HazelcastClient;
36 
37  public:
52  std::string addItemListener(ItemListener<E>& listener, bool includeValue) {
53  spi::ClusterService& cs = context->getClusterService();
54  serialization::pimpl::SerializationService& ss = context->getSerializationService();
55  impl::ItemEventHandler<E, protocol::codec::QueueAddListenerCodec::AbstractEventHandler> *itemEventHandler =
56  new impl::ItemEventHandler<E, protocol::codec::QueueAddListenerCodec::AbstractEventHandler>(getName(), cs, ss, listener, includeValue);
57  return proxy::IQueueImpl::addItemListener(itemEventHandler, includeValue);
58  }
59 
68  bool removeItemListener(const std::string& registrationId) {
69  return proxy::IQueueImpl::removeItemListener(registrationId);
70  }
71 
80  bool offer(const E& element) {
81  return offer(element, 0);
82  }
83 
88  void put(const E& e) {
89  offer(e, -1);
90  }
91 
101  bool offer(const E& element, long timeoutInMillis) {
102  return proxy::IQueueImpl::offer(toData(element), timeoutInMillis);
103  }
104 
109  boost::shared_ptr<E> take() {
110  return poll(-1);
111  }
112 
118  boost::shared_ptr<E> poll(long timeoutInMillis) {
119  return toObject<E>(proxy::IQueueImpl::poll(timeoutInMillis));
120  }
121 
127  return proxy::IQueueImpl::remainingCapacity();
128  }
129 
135  bool remove(const E& element) {
136  return proxy::IQueueImpl::remove(toData(element));
137  }
138 
144  bool contains(const E& element) {
145  return proxy::IQueueImpl::contains(toData(element));
146  }
147 
154  int drainTo(std::vector<E>& elements) {
155  return drainTo(elements, -1);
156  }
157 
165  int drainTo(std::vector<E>& elements, int maxElements) {
166  std::vector<serialization::pimpl::Data> coll = proxy::IQueueImpl::drainTo(maxElements);
167  for (std::vector<serialization::pimpl::Data>::const_iterator it = coll.begin(); it != coll.end(); ++it) {
168  boost::shared_ptr<E> e = context->getSerializationService().template toObject<E>(*it);
169  elements.push_back(*e);
170  }
171  return coll.size();
172  }
173 
179  boost::shared_ptr<E> poll() {
180  return poll(0);
181  }
182 
188  boost::shared_ptr<E> peek() {
189  return toObject<E>(proxy::IQueueImpl::peek());
190  }
191 
196  int size() {
197  return proxy::IQueueImpl::size();
198  }
199 
204  bool isEmpty() {
205  return size() == 0;
206  }
207 
212  std::vector<E> toArray() {
213  return toObjectCollection<E>(proxy::IQueueImpl::toArray());
214  }
215 
222  bool containsAll(const std::vector<E>& elements) {
223  std::vector<serialization::pimpl::Data> list = toDataCollection(elements);
224  return proxy::IQueueImpl::containsAll(list);
225  }
226 
233  bool addAll(const std::vector<E>& elements) {
234  std::vector<serialization::pimpl::Data> dataList = toDataCollection(elements);
235  return proxy::IQueueImpl::addAll(dataList);
236  }
237 
244  bool removeAll(const std::vector<E>& elements) {
245  std::vector<serialization::pimpl::Data> dataList = toDataCollection(elements);
246  return proxy::IQueueImpl::removeAll(dataList);
247  }
248 
256  bool retainAll(const std::vector<E>& elements) {
257  std::vector<serialization::pimpl::Data> dataList = toDataCollection(elements);
258  return proxy::IQueueImpl::retainAll(dataList);
259  }
260 
264  void clear() {
265  proxy::IQueueImpl::clear();
266  }
267  private:
268  IQueue(const std::string& instanceName, spi::ClientContext *context)
269  : proxy::IQueueImpl(instanceName, context){
270 
271  }
272  };
273  }
274 }
275 
276 #endif /* HAZELCAST_IQUEUE */
277 
void clear()
Removes all elements from queue.
Definition: IQueue.h:264
bool offer(const E &element, long timeoutInMillis)
Inserts the specified element into this queue.
Definition: IQueue.h:101
Concurrent, blocking, distributed, observable, client queue.
Definition: IQueue.h:34
bool offer(const E &element)
Inserts the specified element into this queue.
Definition: IQueue.h:80
std::vector< E > toArray()
Definition: IQueue.h:212
void put(const E &e)
Puts the element into queue.
Definition: IQueue.h:88
bool contains(const E &element)
Definition: IQueue.h:144
bool removeAll(const std::vector< E > &elements)
Definition: IQueue.h:244
int remainingCapacity()
Definition: IQueue.h:126
bool removeItemListener(const std::string &registrationId)
Removes the specified item listener.
Definition: IQueue.h:68
std::string addItemListener(ItemListener< E > &listener, bool includeValue)
Adds an item listener for this collection.
Definition: IQueue.h:52
boost::shared_ptr< E > poll(long timeoutInMillis)
Definition: IQueue.h:118
int size()
Definition: IQueue.h:196
int drainTo(std::vector< E > &elements, int maxElements)
Note that elements will be pushed_back to vector.
Definition: IQueue.h:165
bool isEmpty()
Definition: IQueue.h:204
int drainTo(std::vector< E > &elements)
Note that elements will be pushed_back to vector.
Definition: IQueue.h:154
bool retainAll(const std::vector< E > &elements)
Removes the elements from this queue that are not available in given "elements" vector.
Definition: IQueue.h:256
boost::shared_ptr< E > peek()
Returns immediately without waiting.
Definition: IQueue.h:188
Item listener for IQueue, ISet and IList.
Definition: ItemListener.h:40
bool containsAll(const std::vector< E > &elements)
Definition: IQueue.h:222
bool addAll(const std::vector< E > &elements)
Definition: IQueue.h:233
boost::shared_ptr< E > poll()
Returns immediately without waiting.
Definition: IQueue.h:179
boost::shared_ptr< E > take()
Definition: IQueue.h:109
Hazelcast Client enables you to do all Hazelcast operations without being a member of the cluster...
Definition: HazelcastClient.h:410