Backport the IntObjectHashMap changes in f23f3b9617
Motivation: The mentioned commit contains a bug fix and an improvement in IntObjectHashMap that requires backporting. Modifications: Update IntObjectMap, IntObjectHashMap, and IntObjectHashMapTest Result: Easier to backport HTTP/2 and other changes in master in the future
This commit is contained in:
parent
948eafdce2
commit
9da4250917
@ -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<V> implements IntObjectMap<V>, Iterable<IntObjectM
|
||||
values[index] = toInternal(value);
|
||||
growSize();
|
||||
return null;
|
||||
} else if (keys[index] == key) {
|
||||
}
|
||||
if (keys[index] == key) {
|
||||
// Found existing entry with this key, just replace the value.
|
||||
V previousValue = values[index];
|
||||
values[index] = toInternal(value);
|
||||
@ -220,16 +222,34 @@ public class IntObjectHashMap<V> implements IntObjectMap<V>, Iterable<IntObjectM
|
||||
}
|
||||
|
||||
@Override
|
||||
public V[] values(Class<V> 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<V> values() {
|
||||
return new AbstractCollection<V>() {
|
||||
@Override
|
||||
public Iterator<V> iterator() {
|
||||
return new Iterator<V>() {
|
||||
final Iterator<Entry<V>> 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<V> implements IntObjectMap<V>, Iterable<IntObjectM
|
||||
Object otherValue = other.get(key);
|
||||
if (value == NULL_VALUE) {
|
||||
if (otherValue != null) {
|
||||
return false;
|
||||
return false;
|
||||
}
|
||||
} else if (!value.equals(otherValue)) {
|
||||
return false;
|
||||
@ -361,7 +381,7 @@ public class IntObjectHashMap<V> implements IntObjectMap<V>, Iterable<IntObjectM
|
||||
for (int i = probeNext(index); values[i] != null; i = probeNext(i)) {
|
||||
int bucket = hashIndex(keys[i]);
|
||||
if ((i < bucket && (bucket <= nextFree || nextFree <= i))
|
||||
|| (bucket <= nextFree && nextFree <= i)) {
|
||||
|| (bucket <= nextFree && nextFree <= i)) {
|
||||
// Move the displaced entry "back" to the first available position.
|
||||
keys[nextFree] = keys[i];
|
||||
values[nextFree] = values[i];
|
||||
@ -498,9 +518,16 @@ public class IntObjectHashMap<V> implements IntObjectMap<V>, Iterable<IntObjectM
|
||||
V value = values[i];
|
||||
if (value != null) {
|
||||
sb.append(sb.length() == 0 ? "{" : ", ");
|
||||
sb.append(keys[i]).append('=').append(value == this ? "(this Map)" : value);
|
||||
sb.append(keyToString(keys[i])).append('=').append(value == this ? "(this Map)" : value);
|
||||
}
|
||||
}
|
||||
return sb.append('}').toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method called by {@link #toString()} in order to convert a single map key into a string.
|
||||
*/
|
||||
protected String keyToString(int key) {
|
||||
return Integer.toString(key);
|
||||
}
|
||||
}
|
||||
|
@ -14,6 +14,8 @@
|
||||
*/
|
||||
package io.netty.util.collection;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
* Interface for a primitive map that uses {@code int}s as keys.
|
||||
*
|
||||
@ -112,5 +114,5 @@ public interface IntObjectMap<V> {
|
||||
/**
|
||||
* Gets the values contained in this map.
|
||||
*/
|
||||
V[] values(Class<V> clazz);
|
||||
Collection<V> values();
|
||||
}
|
||||
|
@ -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<Value> values = map.values();
|
||||
assertEquals(3, values.size());
|
||||
|
||||
Set<Value> expected = new HashSet<Value>();
|
||||
expected.add(v1);
|
||||
|
Loading…
Reference in New Issue
Block a user