supress generic warnings

This commit is contained in:
nathan 2020-08-18 21:20:12 +02:00
parent 7161891b1f
commit caa6e5e3fc
3 changed files with 109 additions and 55 deletions

View File

@ -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 * 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). * element is moved to the removed element's position).
* @author Nathan Sweet */ * @author Nathan Sweet */
@SuppressWarnings({"unchecked", "rawtypes", "unused", "SuspiciousSystemArraycopy", "NullableProblems"})
public class ArrayMap<K, V> implements Iterable<ObjectMap.Entry<K, V>> { public class ArrayMap<K, V> implements Iterable<ObjectMap.Entry<K, V>> {
public K[] keys; public K[] keys;
public V[] values; 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 /** 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 * 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. */ * added will cause the backing arrays to be grown. */
@SuppressWarnings("CopyConstructorMissesField")
public ArrayMap (ArrayMap array) { public ArrayMap (ArrayMap array) {
this(array.ordered, array.size, array.keys.getClass().getComponentType(), array.values.getClass().getComponentType()); this(array.ordered, array.size, array.keys.getClass().getComponentType(), array.values.getClass().getComponentType());
size = array.size; size = array.size;
@ -405,6 +407,7 @@ public class ArrayMap<K, V> implements Iterable<ObjectMap.Entry<K, V>> {
size = newSize; size = newSize;
} }
@Override
public int hashCode () { public int hashCode () {
K[] keys = this.keys; K[] keys = this.keys;
V[] values = this.values; V[] values = this.values;
@ -418,6 +421,7 @@ public class ArrayMap<K, V> implements Iterable<ObjectMap.Entry<K, V>> {
return h; return h;
} }
@Override
public boolean equals (Object obj) { public boolean equals (Object obj) {
if (obj == this) return true; if (obj == this) return true;
if (!(obj instanceof ArrayMap)) return false; if (!(obj instanceof ArrayMap)) return false;
@ -437,6 +441,7 @@ public class ArrayMap<K, V> implements Iterable<ObjectMap.Entry<K, V>> {
return true; return true;
} }
@Override
public String toString () { public String toString () {
if (size == 0) return "{}"; if (size == 0) return "{}";
K[] keys = this.keys; K[] keys = this.keys;
@ -456,6 +461,7 @@ public class ArrayMap<K, V> implements Iterable<ObjectMap.Entry<K, V>> {
return buffer.toString(); return buffer.toString();
} }
@Override
public Iterator<Entry<K, V>> iterator () { public Iterator<Entry<K, V>> iterator () {
return entries(); return entries();
} }
@ -527,16 +533,19 @@ public class ArrayMap<K, V> implements Iterable<ObjectMap.Entry<K, V>> {
this.map = map; this.map = map;
} }
@Override
public boolean hasNext () { public boolean hasNext () {
if (!valid) throw new RuntimeException("#iterator() cannot be used nested."); if (!valid) throw new RuntimeException("#iterator() cannot be used nested.");
return index < map.size; return index < map.size;
} }
@Override
public Iterator<Entry<K, V>> iterator () { public Iterator<Entry<K, V>> iterator () {
return this; return this;
} }
/** Note the same entry instance is returned each time this method is called. */ /** Note the same entry instance is returned each time this method is called. */
@Override
public Entry<K, V> next () { public Entry<K, V> next () {
if (index >= map.size) throw new NoSuchElementException(String.valueOf(index)); if (index >= map.size) throw new NoSuchElementException(String.valueOf(index));
if (!valid) throw new RuntimeException("#iterator() cannot be used nested."); 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; return entry;
} }
@Override
public void remove () { public void remove () {
index--; index--;
map.removeIndex(index); map.removeIndex(index);
@ -564,21 +574,25 @@ public class ArrayMap<K, V> implements Iterable<ObjectMap.Entry<K, V>> {
this.map = map; this.map = map;
} }
@Override
public boolean hasNext () { public boolean hasNext () {
if (!valid) throw new RuntimeException("#iterator() cannot be used nested."); if (!valid) throw new RuntimeException("#iterator() cannot be used nested.");
return index < map.size; return index < map.size;
} }
@Override
public Iterator<V> iterator () { public Iterator<V> iterator () {
return this; return this;
} }
@Override
public V next () { public V next () {
if (index >= map.size) throw new NoSuchElementException(String.valueOf(index)); if (index >= map.size) throw new NoSuchElementException(String.valueOf(index));
if (!valid) throw new RuntimeException("#iterator() cannot be used nested."); if (!valid) throw new RuntimeException("#iterator() cannot be used nested.");
return map.values[index++]; return map.values[index++];
} }
@Override
public void remove () { public void remove () {
index--; index--;
map.removeIndex(index); map.removeIndex(index);
@ -607,21 +621,25 @@ public class ArrayMap<K, V> implements Iterable<ObjectMap.Entry<K, V>> {
this.map = map; this.map = map;
} }
@Override
public boolean hasNext () { public boolean hasNext () {
if (!valid) throw new RuntimeException("#iterator() cannot be used nested."); if (!valid) throw new RuntimeException("#iterator() cannot be used nested.");
return index < map.size; return index < map.size;
} }
@Override
public Iterator<K> iterator () { public Iterator<K> iterator () {
return this; return this;
} }
@Override
public K next () { public K next () {
if (index >= map.size) throw new NoSuchElementException(String.valueOf(index)); if (index >= map.size) throw new NoSuchElementException(String.valueOf(index));
if (!valid) throw new RuntimeException("#iterator() cannot be used nested."); if (!valid) throw new RuntimeException("#iterator() cannot be used nested.");
return map.keys[index++]; return map.keys[index++];
} }
@Override
public void remove () { public void remove () {
index--; index--;
map.removeIndex(index); map.removeIndex(index);

View File

@ -528,6 +528,7 @@ public class ObjectMap<K, V> implements Iterable<ObjectMap.Entry<K, V>> {
return (h ^ h >>> hashShift) & mask; return (h ^ h >>> hashShift) & mask;
} }
@Override
public int hashCode () { public int hashCode () {
int h = 0; int h = 0;
K[] keyTable = this.keyTable; K[] keyTable = this.keyTable;
@ -546,6 +547,7 @@ public class ObjectMap<K, V> implements Iterable<ObjectMap.Entry<K, V>> {
return h; return h;
} }
@Override
public boolean equals (Object obj) { public boolean equals (Object obj) {
if (obj == this) return true; if (obj == this) return true;
if (!(obj instanceof ObjectMap)) return false; 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); return toString(separator, false);
} }
@Override
public String toString () { public String toString () {
return toString(", ", true); return toString(", ", true);
} }
@ -602,6 +605,7 @@ public class ObjectMap<K, V> implements Iterable<ObjectMap.Entry<K, V>> {
return buffer.toString(); return buffer.toString();
} }
@Override
public Entries<K, V> iterator () { public Entries<K, V> iterator () {
return entries(); return entries();
} }
@ -667,6 +671,7 @@ public class ObjectMap<K, V> implements Iterable<ObjectMap.Entry<K, V>> {
public K key; public K key;
public V value; public V value;
@Override
public String toString () { public String toString () {
return key + "=" + value; return key + "=" + value;
} }
@ -701,6 +706,7 @@ public class ObjectMap<K, V> implements Iterable<ObjectMap.Entry<K, V>> {
} }
} }
@Override
public void remove () { public void remove () {
if (currentIndex < 0) throw new IllegalStateException("next must be called before remove."); if (currentIndex < 0) throw new IllegalStateException("next must be called before remove.");
if (currentIndex >= map.capacity) { 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. */ /** Note the same entry instance is returned each time this method is called. */
@Override
public Entry<K, V> next () { public Entry<K, V> next () {
if (!hasNext) throw new NoSuchElementException(); if (!hasNext) throw new NoSuchElementException();
if (!valid) throw new RuntimeException("#iterator() cannot be used nested."); 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; return entry;
} }
@Override
public boolean hasNext () { public boolean hasNext () {
if (!valid) throw new RuntimeException("#iterator() cannot be used nested."); if (!valid) throw new RuntimeException("#iterator() cannot be used nested.");
return hasNext; return hasNext;
} }
@Override
public Entries<K, V> iterator () { public Entries<K, V> iterator () {
return this; return this;
} }
} }
@SuppressWarnings("NullableProblems")
static public class Values<V> extends MapIterator<Object, V, V> { static public class Values<V> extends MapIterator<Object, V, V> {
public Values (ObjectMap<?, V> map) { public Values (ObjectMap<?, V> map) {
super((ObjectMap<Object, V>)map); super((ObjectMap<Object, V>)map);
} }
@Override
public boolean hasNext () { public boolean hasNext () {
if (!valid) throw new RuntimeException("#iterator() cannot be used nested."); if (!valid) throw new RuntimeException("#iterator() cannot be used nested.");
return hasNext; return hasNext;
} }
@Override
public V next () { public V next () {
if (!hasNext) throw new NoSuchElementException(); if (!hasNext) throw new NoSuchElementException();
if (!valid) throw new RuntimeException("#iterator() cannot be used nested."); 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; return value;
} }
@Override
public Values<V> iterator () { public Values<V> iterator () {
return this; return this;
} }
@ -786,11 +799,13 @@ public class ObjectMap<K, V> implements Iterable<ObjectMap.Entry<K, V>> {
super((ObjectMap<K, Object>)map); super((ObjectMap<K, Object>)map);
} }
@Override
public boolean hasNext () { public boolean hasNext () {
if (!valid) throw new RuntimeException("#iterator() cannot be used nested."); if (!valid) throw new RuntimeException("#iterator() cannot be used nested.");
return hasNext; return hasNext;
} }
@Override
public K next () { public K next () {
if (!hasNext) throw new NoSuchElementException(); if (!hasNext) throw new NoSuchElementException();
if (!valid) throw new RuntimeException("#iterator() cannot be used nested."); 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; return key;
} }
@Override
public Keys<K> iterator () { public Keys<K> iterator () {
return this; return this;
} }

View File

@ -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 * 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()}. * setting {@link Array#ordered} to false for {@link OrderedMap#orderedKeys()}.
* @author Nathan Sweet */ * @author Nathan Sweet */
@SuppressWarnings({"unchecked", "NullableProblems"})
public class OrderedMap<K, V> extends ObjectMap<K, V> { public class OrderedMap<K, V> extends ObjectMap<K, V> {
final Array<K> keys; final Array<K> keys;
@ -52,11 +53,13 @@ public class OrderedMap<K, V> extends ObjectMap<K, V> {
keys = new Array(map.keys); keys = new Array(map.keys);
} }
@Override
public V put (K key, V value) { public V put (K key, V value) {
if (!containsKey(key)) keys.add(key); if (!containsKey(key)) keys.add(key);
return super.put(key, value); return super.put(key, value);
} }
@Override
public V remove (K key) { public V remove (K key) {
keys.removeValue(key, false); keys.removeValue(key, false);
return super.remove(key); return super.remove(key);
@ -66,11 +69,13 @@ public class OrderedMap<K, V> extends ObjectMap<K, V> {
return super.remove(keys.removeIndex(index)); return super.remove(keys.removeIndex(index));
} }
@Override
public void clear (int maximumCapacity) { public void clear (int maximumCapacity) {
keys.clear(); keys.clear();
super.clear(maximumCapacity); super.clear(maximumCapacity);
} }
@Override
public void clear () { public void clear () {
keys.clear(); keys.clear();
super.clear(); super.clear();
@ -80,12 +85,14 @@ public class OrderedMap<K, V> extends ObjectMap<K, V> {
return keys; return keys;
} }
@Override
public Entries<K, V> iterator () { public Entries<K, V> iterator () {
return entries(); return entries();
} }
/** Returns an iterator for the entries in the map. Remove is supported. Note that the same iterator instance is returned each /** 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. */ * time this method is called. Use the {@link OrderedMapEntries} constructor for nested or multithreaded iteration. */
@Override
public Entries<K, V> entries () { public Entries<K, V> entries () {
if (entries1 == null) { if (entries1 == null) {
entries1 = new OrderedMapEntries(this); 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 /** 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. */ * time this method is called. Use the {@link OrderedMapValues} constructor for nested or multithreaded iteration. */
@Override
public Values<V> values () { public Values<V> values () {
if (values1 == null) { if (values1 == null) {
values1 = new OrderedMapValues(this); 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 /** 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. */ * time this method is called. Use the {@link OrderedMapKeys} constructor for nested or multithreaded iteration. */
@Override
public Keys<K> keys () { public Keys<K> keys () {
if (keys1 == null) { if (keys1 == null) {
keys1 = new OrderedMapKeys(this); keys1 = new OrderedMapKeys(this);
@ -141,6 +150,7 @@ public class OrderedMap<K, V> extends ObjectMap<K, V> {
return keys2; return keys2;
} }
@Override
public String toString () { public String toString () {
if (size == 0) return "{}"; if (size == 0) return "{}";
StringBuilder buffer = new StringBuilder(32); StringBuilder buffer = new StringBuilder(32);
@ -157,19 +167,22 @@ public class OrderedMap<K, V> extends ObjectMap<K, V> {
return buffer.toString(); return buffer.toString();
} }
@SuppressWarnings("rawtypes")
static public class OrderedMapEntries<K, V> extends Entries<K, V> { 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) { public OrderedMapEntries (OrderedMap<K, V> map) {
super(map); super(map);
keys = map.keys; keys = map.keys;
} }
@Override
public void reset () { public void reset () {
nextIndex = 0; nextIndex = 0;
hasNext = map.size > 0; hasNext = map.size > 0;
} }
@Override
public Entry next () { public Entry next () {
if (!hasNext) throw new NoSuchElementException(); if (!hasNext) throw new NoSuchElementException();
if (!valid) throw new RuntimeException("#iterator() cannot be used nested."); 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; return entry;
} }
@Override
public void remove () { public void remove () {
if (currentIndex < 0) throw new IllegalStateException("next must be called before remove."); if (currentIndex < 0) throw new IllegalStateException("next must be called before remove.");
map.remove(entry.key); 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> { static public class OrderedMapKeys<K> extends Keys<K> {
private Array<K> keys; private final Array<K> keys;
public OrderedMapKeys (OrderedMap<K, ?> map) { public OrderedMapKeys (OrderedMap<K, ?> map) {
super(map); super(map);
keys = map.keys; keys = map.keys;
} }
@Override
public void reset () { public void reset () {
nextIndex = 0; nextIndex = 0;
hasNext = map.size > 0; hasNext = map.size > 0;
} }
@Override
public K next () { public K next () {
if (!hasNext) throw new NoSuchElementException(); if (!hasNext) throw new NoSuchElementException();
if (!valid) throw new RuntimeException("#iterator() cannot be used nested."); 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; return key;
} }
@Override
public void remove () { public void remove () {
if (currentIndex < 0) throw new IllegalStateException("next must be called before remove."); if (currentIndex < 0) throw new IllegalStateException("next must be called before remove.");
((OrderedMap)map).removeIndex(nextIndex - 1); ((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> { static public class OrderedMapValues<V> extends Values<V> {
private Array keys; private final Array keys;
public OrderedMapValues (OrderedMap<?, V> map) { public OrderedMapValues (OrderedMap<?, V> map) {
super(map); super(map);
keys = map.keys; keys = map.keys;
} }
@Override
public void reset () { public void reset () {
nextIndex = 0; nextIndex = 0;
hasNext = map.size > 0; hasNext = map.size > 0;
} }
@Override
public V next () { public V next () {
if (!hasNext) throw new NoSuchElementException(); if (!hasNext) throw new NoSuchElementException();
if (!valid) throw new RuntimeException("#iterator() cannot be used nested."); 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; return value;
} }
@Override
public void remove () { public void remove () {
if (currentIndex < 0) throw new IllegalStateException("next must be called before remove."); if (currentIndex < 0) throw new IllegalStateException("next must be called before remove.");
((OrderedMap)map).removeIndex(currentIndex); ((OrderedMap)map).removeIndex(currentIndex);