1 16 package org.apache.commons.collections.iterators; 17 18 import java.util.Iterator ; 19 import java.util.Map ; 20 21 import org.apache.commons.collections.MapIterator; 22 import org.apache.commons.collections.ResettableIterator; 23 24 41 public class EntrySetMapIterator implements MapIterator, ResettableIterator { 42 43 private final Map map; 44 private Iterator iterator; 45 private Map.Entry last; 46 private boolean canRemove = false; 47 48 53 public EntrySetMapIterator(Map map) { 54 super(); 55 this.map = map; 56 this.iterator = map.entrySet().iterator(); 57 } 58 59 65 public boolean hasNext() { 66 return iterator.hasNext(); 67 } 68 69 75 public Object next() { 76 last = (Map.Entry ) iterator.next(); 77 canRemove = true; 78 return last.getKey(); 79 } 80 81 92 public void remove() { 93 if (canRemove == false) { 94 throw new IllegalStateException ("Iterator remove() can only be called once after next()"); 95 } 96 iterator.remove(); 97 last = null; 98 canRemove = false; 99 } 100 101 109 public Object getKey() { 110 if (last == null) { 111 throw new IllegalStateException ("Iterator getKey() can only be called after next() and before remove()"); 112 } 113 return last.getKey(); 114 } 115 116 123 public Object getValue() { 124 if (last == null) { 125 throw new IllegalStateException ("Iterator getValue() can only be called after next() and before remove()"); 126 } 127 return last.getValue(); 128 } 129 130 140 public Object setValue(Object value) { 141 if (last == null) { 142 throw new IllegalStateException ("Iterator setValue() can only be called after next() and before remove()"); 143 } 144 return last.setValue(value); 145 } 146 147 151 public void reset() { 152 iterator = map.entrySet().iterator(); 153 last = null; 154 canRemove = false; 155 } 156 157 162 public String toString() { 163 if (last != null) { 164 return "MapIterator[" + getKey() + "=" + getValue() + "]"; 165 } else { 166 return "MapIterator[]"; 167 } 168 } 169 170 } 171 | Popular Tags |