PageRenderTime 58ms CodeModel.GetById 52ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/gabral6_gmailcom/hazelcast
Java | 98 lines | 68 code | 15 blank | 15 comment | 5 complexity | f4df69da8bfee14434c5b19c692db198 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.core.IMap;
20import com.hazelcast.core.Instance;
21
22import java.util.Iterator;
23import java.util.Map.Entry;
24
25public class MapEntryIterator<K, V> implements Iterator<java.util.Map.Entry<K, V>> {
26    protected final Iterator<K> it;
27    protected final EntryHolder<K, V> proxy;
28    protected final Instance.InstanceType instanceType;
29    protected volatile Entry<K, V> lastEntry;
30    K currentIteratedKey;
31    V currentIteratedValue;
32    boolean hasNextCalled = false;
33
34    public MapEntryIterator(Iterator<K> it, EntryHolder<K, V> proxy, Instance.InstanceType instanceType) {
35        this.it = it;
36        this.proxy = proxy;
37        this.instanceType = instanceType;
38    }
39
40    public boolean hasNext() {
41        hasNextCalled = true;
42        if (!it.hasNext()) {
43            return false;
44        }
45        currentIteratedKey = it.next();
46        currentIteratedValue = proxy.get(currentIteratedKey);
47        if (currentIteratedValue == null) {
48            return hasNext();
49        }
50        return true;
51    }
52
53    public Entry<K, V> next() {
54        if (!hasNextCalled) {
55            hasNext();
56        }
57        hasNextCalled = false;
58        K key = this.currentIteratedKey;
59        V value = this.currentIteratedValue;
60        lastEntry = new MapEntry(key, value, proxy);
61        return lastEntry;
62    }
63
64    public void remove() {
65        it.remove();
66        proxy.remove(lastEntry.getKey(), lastEntry.getValue());
67    }
68
69    protected class MapEntry implements Entry<K, V> {
70
71        private K key;
72        private V value;
73        private EntryHolder<K, V> proxy;
74
75        public MapEntry(K key, V value, EntryHolder<K, V> proxy) {
76            this.key = key;
77            this.value = value;
78            this.proxy = proxy;
79        }
80
81        public K getKey() {
82            return key;
83        }
84
85        public V getValue() {
86            return value;
87        }
88
89        public V setValue(V arg0) {
90            if (instanceType.equals(Instance.InstanceType.MULTIMAP)) {
91                throw new UnsupportedOperationException();
92            }
93            return (V) ((IMap) proxy).put(key, arg0);
94        }
95    }
96
97    ;
98}