You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by gb...@apache.org on 2010/02/13 16:15:04 UTC
svn commit: r909836 - in /pivot/trunk/core:
src/org/apache/pivot/collections/ src/org/apache/pivot/util/
test/org/apache/pivot/collections/test/
Author: gbrown
Date: Sat Feb 13 15:15:03 2010
New Revision: 909836
URL: http://svn.apache.org/viewvc?rev=909836&view=rev
Log:
Implement remove() in HashMap and HashSet iterators.
Modified:
pivot/trunk/core/src/org/apache/pivot/collections/HashMap.java
pivot/trunk/core/src/org/apache/pivot/collections/HashSet.java
pivot/trunk/core/src/org/apache/pivot/util/ImmutableIterator.java
pivot/trunk/core/test/org/apache/pivot/collections/test/HashMapTest.java
pivot/trunk/core/test/org/apache/pivot/collections/test/HashSetTest.java
Modified: pivot/trunk/core/src/org/apache/pivot/collections/HashMap.java
URL: http://svn.apache.org/viewvc/pivot/trunk/core/src/org/apache/pivot/collections/HashMap.java?rev=909836&r1=909835&r2=909836&view=diff
==============================================================================
--- pivot/trunk/core/src/org/apache/pivot/collections/HashMap.java (original)
+++ pivot/trunk/core/src/org/apache/pivot/collections/HashMap.java Sat Feb 13 15:15:03 2010
@@ -38,6 +38,8 @@
private Iterator<Pair<K, V>> entryIterator;
private int count;
+ private Pair<K, V> entry = null;
+
public KeyIterator() {
bucketIndex = 0;
entryIterator = getBucketIterator(bucketIndex);
@@ -67,14 +69,26 @@
throw new NoSuchElementException();
}
- Pair<K, V> entry = entryIterator.next();
-
+ entry = entryIterator.next();
return entry.key;
}
@Override
public void remove() {
- throw new UnsupportedOperationException();
+ if (entry == null
+ || entryIterator == null) {
+ throw new IllegalStateException();
+ }
+
+ entryIterator.remove();
+ count--;
+ HashMap.this.count--;
+
+ if (mapListeners != null) {
+ mapListeners.valueRemoved(HashMap.this, entry.key, entry.value);
+ }
+
+ entry = null;
}
private Iterator<Pair<K, V>> getBucketIterator(int bucketIndex) {
Modified: pivot/trunk/core/src/org/apache/pivot/collections/HashSet.java
URL: http://svn.apache.org/viewvc/pivot/trunk/core/src/org/apache/pivot/collections/HashSet.java?rev=909836&r1=909835&r2=909836&view=diff
==============================================================================
--- pivot/trunk/core/src/org/apache/pivot/collections/HashSet.java (original)
+++ pivot/trunk/core/src/org/apache/pivot/collections/HashSet.java Sat Feb 13 15:15:03 2010
@@ -19,6 +19,7 @@
import java.io.Serializable;
import java.util.Comparator;
import java.util.Iterator;
+import java.util.NoSuchElementException;
import org.apache.pivot.util.ListenerList;
@@ -29,6 +30,50 @@
public class HashSet<E> implements Set<E>, Serializable {
private static final long serialVersionUID = 4095129319373194969L;
+ private class ElementIterator implements Iterator<E> {
+ private Iterator<E> iterator;
+
+ private E element = null;
+
+ public ElementIterator(Iterator<E> iterator) {
+ if (iterator == null) {
+ throw new IllegalArgumentException("iterator is null.");
+ }
+
+ this.iterator = iterator;
+ }
+
+ @Override
+ public boolean hasNext() {
+ return iterator.hasNext();
+ }
+
+ @Override
+ public E next() {
+ if (!hasNext()) {
+ throw new NoSuchElementException();
+ }
+
+ element = iterator.next();
+ return element;
+ }
+
+ @Override
+ public void remove() {
+ if (element == null) {
+ throw new IllegalStateException();
+ }
+
+ iterator.remove();
+
+ if (setListeners != null) {
+ setListeners.elementRemoved(HashSet.this, element);
+ }
+
+ element = null;
+ }
+ }
+
protected HashMap<E, Void> hashMap = new HashMap<E, Void>();
private transient SetListenerList<E> setListeners = null;
@@ -129,7 +174,7 @@
@Override
public Iterator<E> iterator() {
- return hashMap.iterator();
+ return new ElementIterator(hashMap.iterator());
}
@Override
Modified: pivot/trunk/core/src/org/apache/pivot/util/ImmutableIterator.java
URL: http://svn.apache.org/viewvc/pivot/trunk/core/src/org/apache/pivot/util/ImmutableIterator.java?rev=909836&r1=909835&r2=909836&view=diff
==============================================================================
--- pivot/trunk/core/src/org/apache/pivot/util/ImmutableIterator.java (original)
+++ pivot/trunk/core/src/org/apache/pivot/util/ImmutableIterator.java Sat Feb 13 15:15:03 2010
@@ -22,7 +22,7 @@
* Immutable implementation of the {@link Iterator} interface.
*/
public class ImmutableIterator<T> implements Iterator<T> {
- Iterator<T> iterator;
+ private Iterator<T> iterator;
public ImmutableIterator(Iterator<T> iterator) {
if (iterator == null) {
@@ -43,7 +43,7 @@
}
@Override
- public void remove() {
+ public final void remove() {
throw new UnsupportedOperationException();
}
}
Modified: pivot/trunk/core/test/org/apache/pivot/collections/test/HashMapTest.java
URL: http://svn.apache.org/viewvc/pivot/trunk/core/test/org/apache/pivot/collections/test/HashMapTest.java?rev=909836&r1=909835&r2=909836&view=diff
==============================================================================
--- pivot/trunk/core/test/org/apache/pivot/collections/test/HashMapTest.java (original)
+++ pivot/trunk/core/test/org/apache/pivot/collections/test/HashMapTest.java Sat Feb 13 15:15:03 2010
@@ -82,13 +82,16 @@
}
assertEquals(3, count);
- try {
+ iter = map.iterator();
+ while (iter.hasNext()) {
+ iter.next();
iter.remove();
- fail("Expecting " + UnsupportedOperationException.class);
- } catch (UnsupportedOperationException ex) {
- // ignore, we're expecting this as part of the test
}
+ assertEquals(0, map.getCount());
+ map.put("a", 1);
+ map.put("b", 2);
+ map.put("c", 3);
map.clear();
assertEquals(0, map.getCount());
Modified: pivot/trunk/core/test/org/apache/pivot/collections/test/HashSetTest.java
URL: http://svn.apache.org/viewvc/pivot/trunk/core/test/org/apache/pivot/collections/test/HashSetTest.java?rev=909836&r1=909835&r2=909836&view=diff
==============================================================================
--- pivot/trunk/core/test/org/apache/pivot/collections/test/HashSetTest.java (original)
+++ pivot/trunk/core/test/org/apache/pivot/collections/test/HashSetTest.java Sat Feb 13 15:15:03 2010
@@ -18,25 +18,27 @@
import static org.junit.Assert.*;
+import java.util.Iterator;
+
import org.apache.pivot.collections.HashSet;
import org.junit.Test;
public class HashSetTest {
@Test
public void basicTest() {
- HashSet<String> hashSet = new HashSet<String>();
+ HashSet<String> set = new HashSet<String>();
- hashSet.add("A");
- assertTrue(hashSet.contains("A"));
+ set.add("A");
+ assertTrue(set.contains("A"));
- hashSet.add("B");
- assertTrue(hashSet.contains("A"));
- assertTrue(hashSet.contains("B"));
+ set.add("B");
+ assertTrue(set.contains("A"));
+ assertTrue(set.contains("B"));
- assertEquals(hashSet.getCount(), 2);
+ assertEquals(set.getCount(), 2);
int i = 0;
- for (String element : hashSet) {
+ for (String element : set) {
assertTrue(element.equals("A")
|| element.equals("B"));
i++;
@@ -44,12 +46,34 @@
assertEquals(i, 2);
- hashSet.remove("B");
- assertFalse(hashSet.contains("B"));
+ set.remove("B");
+ assertFalse(set.contains("B"));
+
+ set.remove("A");
+ assertFalse(set.contains("A"));
- hashSet.remove("A");
- assertFalse(hashSet.contains("A"));
+ assertTrue(set.isEmpty());
- assertTrue(hashSet.isEmpty());
+ set.add("A");
+ set.add("B");
+ set.add("C");
+
+ Iterator<String> iter = set.iterator();
+ int count = 0;
+ while (iter.hasNext()) {
+ String s = iter.next();
+ if (!set.contains(s)) {
+ fail("Unknown element in set " + s);
+ }
+ count++;
+ }
+ assertEquals(3, count);
+
+ iter = set.iterator();
+ while (iter.hasNext()) {
+ iter.next();
+ iter.remove();
+ }
+ assertEquals(0, set.getCount());
}
}