diff --git a/common/src/main/java/io/netty/util/collection/IntObjectHashMap.java b/common/src/main/java/io/netty/util/collection/IntObjectHashMap.java index 5c9c84d643..560489928b 100644 --- a/common/src/main/java/io/netty/util/collection/IntObjectHashMap.java +++ b/common/src/main/java/io/netty/util/collection/IntObjectHashMap.java @@ -15,8 +15,9 @@ package io.netty.util.collection; -import java.lang.reflect.Array; +import java.util.AbstractCollection; import java.util.Arrays; +import java.util.Collection; import java.util.Iterator; import java.util.NoSuchElementException; @@ -112,7 +113,8 @@ public class IntObjectHashMap implements IntObjectMap, Iterable implements IntObjectMap, Iterable clazz) { - @SuppressWarnings("unchecked") - V[] outValues = (V[]) Array.newInstance(clazz, size()); - int targetIx = 0; - for (int i = 0; i < values.length; ++i) { - if (values[i] != null) { - outValues[targetIx++] = values[i]; + public Collection values() { + return new AbstractCollection() { + @Override + public Iterator iterator() { + return new Iterator() { + final Iterator> iter = IntObjectHashMap.this.iterator(); + @Override + public boolean hasNext() { + return iter.hasNext(); + } + + @Override + public V next() { + return iter.next().value(); + } + + @Override + public void remove() { + throw new UnsupportedOperationException(); + } + }; } - } - return outValues; + + @Override + public int size() { + return size; + } + }; } @Override @@ -271,7 +291,7 @@ public class IntObjectHashMap implements IntObjectMap, Iterable implements IntObjectMap, Iterable implements IntObjectMap, Iterable { /** * Gets the values contained in this map. */ - V[] values(Class clazz); + Collection values(); } diff --git a/common/src/test/java/io/netty/util/collection/IntObjectHashMapTest.java b/common/src/test/java/io/netty/util/collection/IntObjectHashMapTest.java index 0f5de5811b..d47df64700 100644 --- a/common/src/test/java/io/netty/util/collection/IntObjectHashMapTest.java +++ b/common/src/test/java/io/netty/util/collection/IntObjectHashMapTest.java @@ -14,21 +14,18 @@ */ package io.netty.util.collection; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertSame; -import static org.junit.Assert.assertTrue; - import org.junit.Before; import org.junit.Test; import java.util.Arrays; +import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.Random; import java.util.Set; +import static org.junit.Assert.*; + /** * Tests for {@link IntObjectHashMap}. */ @@ -280,8 +277,8 @@ public class IntObjectHashMapTest { map.put(4, new Value("v4")); map.remove(4); - Value[] values = map.values(Value.class); - assertEquals(3, values.length); + Collection values = map.values(); + assertEquals(3, values.size()); Set expected = new HashSet(); expected.add(v1);