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