PageRenderTime 25ms CodeModel.GetById 13ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/hazelcast-client/src/main/java/com/hazelcast/client/QueueClientProxy.java

https://bitbucket.org/gabral6_gmailcom/hazelcast
Java | 207 lines | 159 code | 33 blank | 15 comment | 18 complexity | 7a288770142b616badb759dbc5ff7bb2 MD5 | raw file
  1/*
  2 * Copyright (c) 2008-2013, 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
 17package com.hazelcast.client;
 18
 19import com.hazelcast.client.impl.QueueItemListenerManager;
 20import com.hazelcast.core.IQueue;
 21import com.hazelcast.core.ItemListener;
 22import com.hazelcast.core.Prefix;
 23import com.hazelcast.impl.ClusterOperation;
 24import com.hazelcast.impl.Keys;
 25import com.hazelcast.monitor.LocalQueueStats;
 26import com.hazelcast.nio.Data;
 27
 28import java.util.AbstractQueue;
 29import java.util.ArrayList;
 30import java.util.Collection;
 31import java.util.List;
 32import java.util.concurrent.TimeUnit;
 33
 34import static com.hazelcast.client.IOUtil.toObject;
 35import static com.hazelcast.client.ProxyHelper.check;
 36
 37public class QueueClientProxy<E> extends AbstractQueue<E> implements IQueue<E> {
 38    final protected ProxyHelper proxyHelper;
 39    final protected String name;
 40
 41    final Object lock = new Object();
 42
 43    public QueueClientProxy(HazelcastClient hazelcastClient, String name) {
 44        super();
 45        this.name = name;
 46        proxyHelper = new ProxyHelper(name, hazelcastClient);
 47    }
 48
 49    public String getName() {
 50        return name.substring(Prefix.QUEUE.length());
 51    }
 52
 53    public InstanceType getInstanceType() {
 54        return InstanceType.QUEUE;
 55    }
 56
 57    public void destroy() {
 58        proxyHelper.destroy();
 59    }
 60
 61    public Object getId() {
 62        return name;
 63    }
 64
 65    @Override
 66    public String toString() {
 67        return "Queue{" +
 68                "name='" + name + '\'' +
 69                '}';
 70    }
 71
 72    @Override
 73    public int hashCode() {
 74        return name.hashCode();
 75    }
 76
 77    public LocalQueueStats getLocalQueueStats() {
 78        throw new UnsupportedOperationException();
 79    }
 80
 81    public boolean offer(E e) {
 82        check(e);
 83        return innerOffer(e, 0);
 84    }
 85
 86    public E poll() {
 87        return innerPoll(0);
 88    }
 89
 90    public E peek() {
 91        return (E) proxyHelper.doOp(ClusterOperation.BLOCKING_QUEUE_PEEK, null, null);
 92    }
 93
 94    public boolean offer(E e, long l, TimeUnit timeUnit) throws InterruptedException {
 95        check(e);
 96        ProxyHelper.checkTime(l, timeUnit);
 97        l = (l < 0) ? 0 : l;
 98        if (e == null) {
 99            throw new NullPointerException();
100        }
101        return innerOffer(e, timeUnit.toMillis(l));
102    }
103
104    private boolean innerOffer(E e, long millis) {
105        return (Boolean) proxyHelper.doOp(ClusterOperation.BLOCKING_QUEUE_OFFER, e, millis);
106    }
107
108    public E poll(long l, TimeUnit timeUnit) throws InterruptedException {
109        ProxyHelper.checkTime(l, timeUnit);
110        l = (l < 0) ? 0 : l;
111        return innerPoll(timeUnit.toMillis(l));
112    }
113
114    private E innerPoll(long millis) {
115        return (E) proxyHelper.doOp(ClusterOperation.BLOCKING_QUEUE_POLL, null, millis);
116    }
117
118    public E take() throws InterruptedException {
119        return innerPoll(-1);
120    }
121
122    public void put(E e) throws InterruptedException {
123        check(e);
124        innerOffer(e, -1);
125    }
126
127    public int remainingCapacity() {
128        return (Integer) proxyHelper.doOp(ClusterOperation.BLOCKING_QUEUE_REMAINING_CAPACITY, null, null);
129    }
130
131    public int drainTo(Collection<? super E> objects) {
132        return drainTo(objects, Integer.MAX_VALUE);
133    }
134
135    public int drainTo(Collection<? super E> objects, int i) {
136        if (objects == null) throw new NullPointerException("drainTo null!");
137        if (i < 0) throw new IllegalArgumentException("Negative maxElements:" + i);
138        if (i == 0) return 0;
139        if (objects instanceof IQueue) {
140            if (((IQueue) objects).getName().equals(getName())) {
141                throw new IllegalArgumentException("Cannot drainTo self!");
142            }
143        }
144        E e;
145        int counter = 0;
146        while (counter < i && (e = poll()) != null) {
147            objects.add(e);
148            counter++;
149        }
150        return counter;
151    }
152
153    @Override
154    public int size() {
155        return (Integer) proxyHelper.doOp(ClusterOperation.BLOCKING_QUEUE_SIZE, null, null);
156    }
157
158    @Override
159    public boolean equals(Object o) {
160        if (o instanceof IQueue && o != null) {
161            return getName().equals(((IQueue) o).getName());
162        } else {
163            return false;
164        }
165    }
166
167    @Override
168    public boolean remove(Object o) {
169        return (Boolean) proxyHelper.doOp(ClusterOperation.BLOCKING_QUEUE_REMOVE, null, o);
170    }
171
172    @Override
173    public java.util.Iterator<E> iterator() {
174        Keys keys = (Keys) proxyHelper.doOp(ClusterOperation.BLOCKING_QUEUE_ENTRIES, null, null);
175        List<E> list = new ArrayList<E>();
176        for (Data d : keys) {
177            list.add((E) toObject(d.buffer));
178        }
179        return new QueueItemIterator(list.toArray(), this);
180    }
181
182    public void addItemListener(ItemListener<E> listener, boolean includeValue) {
183        check(listener);
184        synchronized (lock) {
185            boolean shouldCall = listenerManager().noListenerRegistered(name);
186            listenerManager().registerListener(name, listener, includeValue);
187            if (shouldCall) {
188                Call c = listenerManager().createNewAddItemListenerCall(proxyHelper, includeValue);
189                proxyHelper.doCall(c);
190            }
191        }
192    }
193
194    public void removeItemListener(ItemListener<E> listener) {
195        check(listener);
196        synchronized (lock) {
197            listenerManager().removeListener(name, listener);
198            Packet request = proxyHelper.createRequestPacket(ClusterOperation.REMOVE_LISTENER, null, null);
199            Call c = proxyHelper.createCall(request);
200            proxyHelper.doCall(c);
201        }
202    }
203
204    private QueueItemListenerManager listenerManager() {
205        return proxyHelper.getHazelcastClient().getListenerManager().getQueueItemListenerManager();
206    }
207}