supress generic warnings
This commit is contained in:
parent
7161891b1f
commit
caa6e5e3fc
|
@ -30,6 +30,7 @@ import dorkbox.util.collections.ObjectMap.Entry;
|
|||
* makes iteration fast. Like {@link Array}, if ordered is false, this class avoids a memory copy when removing elements (the last
|
||||
* element is moved to the removed element's position).
|
||||
* @author Nathan Sweet */
|
||||
@SuppressWarnings({"unchecked", "rawtypes", "unused", "SuspiciousSystemArraycopy", "NullableProblems"})
|
||||
public class ArrayMap<K, V> implements Iterable<ObjectMap.Entry<K, V>> {
|
||||
public K[] keys;
|
||||
public V[] values;
|
||||
|
@ -77,6 +78,7 @@ public class ArrayMap<K, V> implements Iterable<ObjectMap.Entry<K, V>> {
|
|||
/** Creates a new map containing the elements in the specified map. The new map will have the same type of backing arrays and
|
||||
* will be ordered if the specified map is ordered. The capacity is set to the number of elements, so any subsequent elements
|
||||
* added will cause the backing arrays to be grown. */
|
||||
@SuppressWarnings("CopyConstructorMissesField")
|
||||
public ArrayMap (ArrayMap array) {
|
||||
this(array.ordered, array.size, array.keys.getClass().getComponentType(), array.values.getClass().getComponentType());
|
||||
size = array.size;
|
||||
|
@ -405,6 +407,7 @@ public class ArrayMap<K, V> implements Iterable<ObjectMap.Entry<K, V>> {
|
|||
size = newSize;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode () {
|
||||
K[] keys = this.keys;
|
||||
V[] values = this.values;
|
||||
|
@ -418,6 +421,7 @@ public class ArrayMap<K, V> implements Iterable<ObjectMap.Entry<K, V>> {
|
|||
return h;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals (Object obj) {
|
||||
if (obj == this) return true;
|
||||
if (!(obj instanceof ArrayMap)) return false;
|
||||
|
@ -437,6 +441,7 @@ public class ArrayMap<K, V> implements Iterable<ObjectMap.Entry<K, V>> {
|
|||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString () {
|
||||
if (size == 0) return "{}";
|
||||
K[] keys = this.keys;
|
||||
|
@ -456,6 +461,7 @@ public class ArrayMap<K, V> implements Iterable<ObjectMap.Entry<K, V>> {
|
|||
return buffer.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<Entry<K, V>> iterator () {
|
||||
return entries();
|
||||
}
|
||||
|
@ -527,16 +533,19 @@ public class ArrayMap<K, V> implements Iterable<ObjectMap.Entry<K, V>> {
|
|||
this.map = map;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext () {
|
||||
if (!valid) throw new RuntimeException("#iterator() cannot be used nested.");
|
||||
return index < map.size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<Entry<K, V>> iterator () {
|
||||
return this;
|
||||
}
|
||||
|
||||
/** Note the same entry instance is returned each time this method is called. */
|
||||
@Override
|
||||
public Entry<K, V> next () {
|
||||
if (index >= map.size) throw new NoSuchElementException(String.valueOf(index));
|
||||
if (!valid) throw new RuntimeException("#iterator() cannot be used nested.");
|
||||
|
@ -545,6 +554,7 @@ public class ArrayMap<K, V> implements Iterable<ObjectMap.Entry<K, V>> {
|
|||
return entry;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove () {
|
||||
index--;
|
||||
map.removeIndex(index);
|
||||
|
@ -564,21 +574,25 @@ public class ArrayMap<K, V> implements Iterable<ObjectMap.Entry<K, V>> {
|
|||
this.map = map;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext () {
|
||||
if (!valid) throw new RuntimeException("#iterator() cannot be used nested.");
|
||||
return index < map.size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<V> iterator () {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public V next () {
|
||||
if (index >= map.size) throw new NoSuchElementException(String.valueOf(index));
|
||||
if (!valid) throw new RuntimeException("#iterator() cannot be used nested.");
|
||||
return map.values[index++];
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove () {
|
||||
index--;
|
||||
map.removeIndex(index);
|
||||
|
@ -607,21 +621,25 @@ public class ArrayMap<K, V> implements Iterable<ObjectMap.Entry<K, V>> {
|
|||
this.map = map;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext () {
|
||||
if (!valid) throw new RuntimeException("#iterator() cannot be used nested.");
|
||||
return index < map.size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<K> iterator () {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public K next () {
|
||||
if (index >= map.size) throw new NoSuchElementException(String.valueOf(index));
|
||||
if (!valid) throw new RuntimeException("#iterator() cannot be used nested.");
|
||||
return map.keys[index++];
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove () {
|
||||
index--;
|
||||
map.removeIndex(index);
|
||||
|
|
|
@ -528,6 +528,7 @@ public class ObjectMap<K, V> implements Iterable<ObjectMap.Entry<K, V>> {
|
|||
return (h ^ h >>> hashShift) & mask;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode () {
|
||||
int h = 0;
|
||||
K[] keyTable = this.keyTable;
|
||||
|
@ -546,6 +547,7 @@ public class ObjectMap<K, V> implements Iterable<ObjectMap.Entry<K, V>> {
|
|||
return h;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals (Object obj) {
|
||||
if (obj == this) return true;
|
||||
if (!(obj instanceof ObjectMap)) return false;
|
||||
|
@ -571,6 +573,7 @@ public class ObjectMap<K, V> implements Iterable<ObjectMap.Entry<K, V>> {
|
|||
return toString(separator, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString () {
|
||||
return toString(", ", true);
|
||||
}
|
||||
|
@ -602,6 +605,7 @@ public class ObjectMap<K, V> implements Iterable<ObjectMap.Entry<K, V>> {
|
|||
return buffer.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Entries<K, V> iterator () {
|
||||
return entries();
|
||||
}
|
||||
|
@ -667,6 +671,7 @@ public class ObjectMap<K, V> implements Iterable<ObjectMap.Entry<K, V>> {
|
|||
public K key;
|
||||
public V value;
|
||||
|
||||
@Override
|
||||
public String toString () {
|
||||
return key + "=" + value;
|
||||
}
|
||||
|
@ -701,6 +706,7 @@ public class ObjectMap<K, V> implements Iterable<ObjectMap.Entry<K, V>> {
|
|||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove () {
|
||||
if (currentIndex < 0) throw new IllegalStateException("next must be called before remove.");
|
||||
if (currentIndex >= map.capacity) {
|
||||
|
@ -724,6 +730,7 @@ public class ObjectMap<K, V> implements Iterable<ObjectMap.Entry<K, V>> {
|
|||
}
|
||||
|
||||
/** Note the same entry instance is returned each time this method is called. */
|
||||
@Override
|
||||
public Entry<K, V> next () {
|
||||
if (!hasNext) throw new NoSuchElementException();
|
||||
if (!valid) throw new RuntimeException("#iterator() cannot be used nested.");
|
||||
|
@ -735,26 +742,31 @@ public class ObjectMap<K, V> implements Iterable<ObjectMap.Entry<K, V>> {
|
|||
return entry;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext () {
|
||||
if (!valid) throw new RuntimeException("#iterator() cannot be used nested.");
|
||||
return hasNext;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Entries<K, V> iterator () {
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("NullableProblems")
|
||||
static public class Values<V> extends MapIterator<Object, V, V> {
|
||||
public Values (ObjectMap<?, V> map) {
|
||||
super((ObjectMap<Object, V>)map);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext () {
|
||||
if (!valid) throw new RuntimeException("#iterator() cannot be used nested.");
|
||||
return hasNext;
|
||||
}
|
||||
|
||||
@Override
|
||||
public V next () {
|
||||
if (!hasNext) throw new NoSuchElementException();
|
||||
if (!valid) throw new RuntimeException("#iterator() cannot be used nested.");
|
||||
|
@ -764,6 +776,7 @@ public class ObjectMap<K, V> implements Iterable<ObjectMap.Entry<K, V>> {
|
|||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Values<V> iterator () {
|
||||
return this;
|
||||
}
|
||||
|
@ -786,11 +799,13 @@ public class ObjectMap<K, V> implements Iterable<ObjectMap.Entry<K, V>> {
|
|||
super((ObjectMap<K, Object>)map);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext () {
|
||||
if (!valid) throw new RuntimeException("#iterator() cannot be used nested.");
|
||||
return hasNext;
|
||||
}
|
||||
|
||||
@Override
|
||||
public K next () {
|
||||
if (!hasNext) throw new NoSuchElementException();
|
||||
if (!valid) throw new RuntimeException("#iterator() cannot be used nested.");
|
||||
|
@ -800,6 +815,7 @@ public class ObjectMap<K, V> implements Iterable<ObjectMap.Entry<K, V>> {
|
|||
return key;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Keys<K> iterator () {
|
||||
return this;
|
||||
}
|
||||
|
|
|
@ -26,6 +26,7 @@ import java.util.NoSuchElementException;
|
|||
* for faster iteration versus ObjectMap and the order does not actually matter, copying during remove can be greatly reduced by
|
||||
* setting {@link Array#ordered} to false for {@link OrderedMap#orderedKeys()}.
|
||||
* @author Nathan Sweet */
|
||||
@SuppressWarnings({"unchecked", "NullableProblems"})
|
||||
public class OrderedMap<K, V> extends ObjectMap<K, V> {
|
||||
final Array<K> keys;
|
||||
|
||||
|
@ -52,11 +53,13 @@ public class OrderedMap<K, V> extends ObjectMap<K, V> {
|
|||
keys = new Array(map.keys);
|
||||
}
|
||||
|
||||
@Override
|
||||
public V put (K key, V value) {
|
||||
if (!containsKey(key)) keys.add(key);
|
||||
return super.put(key, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public V remove (K key) {
|
||||
keys.removeValue(key, false);
|
||||
return super.remove(key);
|
||||
|
@ -66,11 +69,13 @@ public class OrderedMap<K, V> extends ObjectMap<K, V> {
|
|||
return super.remove(keys.removeIndex(index));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear (int maximumCapacity) {
|
||||
keys.clear();
|
||||
super.clear(maximumCapacity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear () {
|
||||
keys.clear();
|
||||
super.clear();
|
||||
|
@ -80,12 +85,14 @@ public class OrderedMap<K, V> extends ObjectMap<K, V> {
|
|||
return keys;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Entries<K, V> iterator () {
|
||||
return entries();
|
||||
}
|
||||
|
||||
/** Returns an iterator for the entries in the map. Remove is supported. Note that the same iterator instance is returned each
|
||||
* time this method is called. Use the {@link OrderedMapEntries} constructor for nested or multithreaded iteration. */
|
||||
@Override
|
||||
public Entries<K, V> entries () {
|
||||
if (entries1 == null) {
|
||||
entries1 = new OrderedMapEntries(this);
|
||||
|
@ -105,6 +112,7 @@ public class OrderedMap<K, V> extends ObjectMap<K, V> {
|
|||
|
||||
/** Returns an iterator for the values in the map. Remove is supported. Note that the same iterator instance is returned each
|
||||
* time this method is called. Use the {@link OrderedMapValues} constructor for nested or multithreaded iteration. */
|
||||
@Override
|
||||
public Values<V> values () {
|
||||
if (values1 == null) {
|
||||
values1 = new OrderedMapValues(this);
|
||||
|
@ -124,6 +132,7 @@ public class OrderedMap<K, V> extends ObjectMap<K, V> {
|
|||
|
||||
/** Returns an iterator for the keys in the map. Remove is supported. Note that the same iterator instance is returned each
|
||||
* time this method is called. Use the {@link OrderedMapKeys} constructor for nested or multithreaded iteration. */
|
||||
@Override
|
||||
public Keys<K> keys () {
|
||||
if (keys1 == null) {
|
||||
keys1 = new OrderedMapKeys(this);
|
||||
|
@ -141,6 +150,7 @@ public class OrderedMap<K, V> extends ObjectMap<K, V> {
|
|||
return keys2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString () {
|
||||
if (size == 0) return "{}";
|
||||
StringBuilder buffer = new StringBuilder(32);
|
||||
|
@ -157,19 +167,22 @@ public class OrderedMap<K, V> extends ObjectMap<K, V> {
|
|||
return buffer.toString();
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
static public class OrderedMapEntries<K, V> extends Entries<K, V> {
|
||||
private Array<K> keys;
|
||||
private final Array<K> keys;
|
||||
|
||||
public OrderedMapEntries (OrderedMap<K, V> map) {
|
||||
super(map);
|
||||
keys = map.keys;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reset () {
|
||||
nextIndex = 0;
|
||||
hasNext = map.size > 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Entry next () {
|
||||
if (!hasNext) throw new NoSuchElementException();
|
||||
if (!valid) throw new RuntimeException("#iterator() cannot be used nested.");
|
||||
|
@ -180,6 +193,7 @@ public class OrderedMap<K, V> extends ObjectMap<K, V> {
|
|||
return entry;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove () {
|
||||
if (currentIndex < 0) throw new IllegalStateException("next must be called before remove.");
|
||||
map.remove(entry.key);
|
||||
|
@ -188,18 +202,20 @@ public class OrderedMap<K, V> extends ObjectMap<K, V> {
|
|||
}
|
||||
|
||||
static public class OrderedMapKeys<K> extends Keys<K> {
|
||||
private Array<K> keys;
|
||||
private final Array<K> keys;
|
||||
|
||||
public OrderedMapKeys (OrderedMap<K, ?> map) {
|
||||
super(map);
|
||||
keys = map.keys;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reset () {
|
||||
nextIndex = 0;
|
||||
hasNext = map.size > 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public K next () {
|
||||
if (!hasNext) throw new NoSuchElementException();
|
||||
if (!valid) throw new RuntimeException("#iterator() cannot be used nested.");
|
||||
|
@ -210,6 +226,7 @@ public class OrderedMap<K, V> extends ObjectMap<K, V> {
|
|||
return key;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove () {
|
||||
if (currentIndex < 0) throw new IllegalStateException("next must be called before remove.");
|
||||
((OrderedMap)map).removeIndex(nextIndex - 1);
|
||||
|
@ -219,18 +236,20 @@ public class OrderedMap<K, V> extends ObjectMap<K, V> {
|
|||
}
|
||||
|
||||
static public class OrderedMapValues<V> extends Values<V> {
|
||||
private Array keys;
|
||||
private final Array keys;
|
||||
|
||||
public OrderedMapValues (OrderedMap<?, V> map) {
|
||||
super(map);
|
||||
keys = map.keys;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reset () {
|
||||
nextIndex = 0;
|
||||
hasNext = map.size > 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public V next () {
|
||||
if (!hasNext) throw new NoSuchElementException();
|
||||
if (!valid) throw new RuntimeException("#iterator() cannot be used nested.");
|
||||
|
@ -241,6 +260,7 @@ public class OrderedMap<K, V> extends ObjectMap<K, V> {
|
|||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove () {
|
||||
if (currentIndex < 0) throw new IllegalStateException("next must be called before remove.");
|
||||
((OrderedMap)map).removeIndex(currentIndex);
|
||||
|
|
Loading…
Reference in New Issue
Block a user