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,7 +78,8 @@ 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. */
public ArrayMap (ArrayMap array) { @SuppressWarnings("CopyConstructorMissesField")
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;
System.arraycopy(array.keys, 0, keys, 0, size); System.arraycopy(array.keys, 0, keys, 0, size);
@ -405,7 +407,8 @@ public class ArrayMap<K, V> implements Iterable<ObjectMap.Entry<K, V>> {
size = newSize; size = newSize;
} }
public int hashCode () { @Override
public int hashCode () {
K[] keys = this.keys; K[] keys = this.keys;
V[] values = this.values; V[] values = this.values;
int h = 0; int h = 0;
@ -418,7 +421,8 @@ public class ArrayMap<K, V> implements Iterable<ObjectMap.Entry<K, V>> {
return h; return h;
} }
public boolean equals (Object obj) { @Override
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;
ArrayMap<K, V> other = (ArrayMap)obj; ArrayMap<K, V> other = (ArrayMap)obj;
@ -437,7 +441,8 @@ public class ArrayMap<K, V> implements Iterable<ObjectMap.Entry<K, V>> {
return true; return true;
} }
public String toString () { @Override
public String toString () {
if (size == 0) return "{}"; if (size == 0) return "{}";
K[] keys = this.keys; K[] keys = this.keys;
V[] values = this.values; V[] values = this.values;
@ -456,7 +461,8 @@ public class ArrayMap<K, V> implements Iterable<ObjectMap.Entry<K, V>> {
return buffer.toString(); return buffer.toString();
} }
public Iterator<Entry<K, V>> iterator () { @Override
public Iterator<Entry<K, V>> iterator () {
return entries(); return entries();
} }
@ -527,17 +533,20 @@ public class ArrayMap<K, V> implements Iterable<ObjectMap.Entry<K, V>> {
this.map = map; this.map = map;
} }
public boolean hasNext () { @Override
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;
} }
public Iterator<Entry<K, V>> iterator () { @Override
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. */
public Entry<K, V> next () { @Override
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.");
entry.key = map.keys[index]; entry.key = map.keys[index];
@ -545,7 +554,8 @@ public class ArrayMap<K, V> implements Iterable<ObjectMap.Entry<K, V>> {
return entry; return entry;
} }
public void remove () { @Override
public void remove () {
index--; index--;
map.removeIndex(index); map.removeIndex(index);
} }
@ -564,22 +574,26 @@ public class ArrayMap<K, V> implements Iterable<ObjectMap.Entry<K, V>> {
this.map = map; this.map = map;
} }
public boolean hasNext () { @Override
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;
} }
public Iterator<V> iterator () { @Override
public Iterator<V> iterator () {
return this; return this;
} }
public V next () { @Override
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++];
} }
public void remove () { @Override
public void remove () {
index--; index--;
map.removeIndex(index); map.removeIndex(index);
} }
@ -607,22 +621,26 @@ public class ArrayMap<K, V> implements Iterable<ObjectMap.Entry<K, V>> {
this.map = map; this.map = map;
} }
public boolean hasNext () { @Override
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;
} }
public Iterator<K> iterator () { @Override
public Iterator<K> iterator () {
return this; return this;
} }
public K next () { @Override
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++];
} }
public void remove () { @Override
public void remove () {
index--; index--;
map.removeIndex(index); map.removeIndex(index);
} }

View File

@ -528,7 +528,8 @@ public class ObjectMap<K, V> implements Iterable<ObjectMap.Entry<K, V>> {
return (h ^ h >>> hashShift) & mask; return (h ^ h >>> hashShift) & mask;
} }
public int hashCode () { @Override
public int hashCode () {
int h = 0; int h = 0;
K[] keyTable = this.keyTable; K[] keyTable = this.keyTable;
V[] valueTable = this.valueTable; V[] valueTable = this.valueTable;
@ -546,7 +547,8 @@ public class ObjectMap<K, V> implements Iterable<ObjectMap.Entry<K, V>> {
return h; return h;
} }
public boolean equals (Object obj) { @Override
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;
ObjectMap<K, V> other = (ObjectMap)obj; ObjectMap<K, V> other = (ObjectMap)obj;
@ -571,7 +573,8 @@ public class ObjectMap<K, V> implements Iterable<ObjectMap.Entry<K, V>> {
return toString(separator, false); return toString(separator, false);
} }
public String toString () { @Override
public String toString () {
return toString(", ", true); return toString(", ", true);
} }
@ -602,7 +605,8 @@ public class ObjectMap<K, V> implements Iterable<ObjectMap.Entry<K, V>> {
return buffer.toString(); return buffer.toString();
} }
public Entries<K, V> iterator () { @Override
public Entries<K, V> iterator () {
return entries(); return entries();
} }
@ -667,7 +671,8 @@ public class ObjectMap<K, V> implements Iterable<ObjectMap.Entry<K, V>> {
public K key; public K key;
public V value; public V value;
public String toString () { @Override
public String toString () {
return key + "=" + value; return key + "=" + value;
} }
} }
@ -701,7 +706,8 @@ public class ObjectMap<K, V> implements Iterable<ObjectMap.Entry<K, V>> {
} }
} }
public void remove () { @Override
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) {
map.removeStashIndex(currentIndex); map.removeStashIndex(currentIndex);
@ -724,7 +730,8 @@ 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. */
public Entry<K, V> next () { @Override
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.");
K[] keyTable = map.keyTable; K[] keyTable = map.keyTable;
@ -735,27 +742,32 @@ public class ObjectMap<K, V> implements Iterable<ObjectMap.Entry<K, V>> {
return entry; return entry;
} }
public boolean hasNext () { @Override
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;
} }
public Entries<K, V> iterator () { @Override
public Entries<K, V> iterator () {
return this; return this;
} }
} }
static public class Values<V> extends MapIterator<Object, V, V> { @SuppressWarnings("NullableProblems")
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);
} }
public boolean hasNext () { @Override
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;
} }
public V next () { @Override
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.");
V value = map.valueTable[nextIndex]; V value = map.valueTable[nextIndex];
@ -764,7 +776,8 @@ public class ObjectMap<K, V> implements Iterable<ObjectMap.Entry<K, V>> {
return value; return value;
} }
public Values<V> iterator () { @Override
public Values<V> iterator () {
return this; return this;
} }
@ -786,12 +799,14 @@ public class ObjectMap<K, V> implements Iterable<ObjectMap.Entry<K, V>> {
super((ObjectMap<K, Object>)map); super((ObjectMap<K, Object>)map);
} }
public boolean hasNext () { @Override
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;
} }
public K next () { @Override
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.");
K key = map.keyTable[nextIndex]; K key = map.keyTable[nextIndex];
@ -800,7 +815,8 @@ public class ObjectMap<K, V> implements Iterable<ObjectMap.Entry<K, V>> {
return key; return key;
} }
public Keys<K> iterator () { @Override
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,12 +53,14 @@ public class OrderedMap<K, V> extends ObjectMap<K, V> {
keys = new Array(map.keys); keys = new Array(map.keys);
} }
public V put (K key, V value) { @Override
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);
} }
public V remove (K key) { @Override
public V remove (K key) {
keys.removeValue(key, false); keys.removeValue(key, false);
return super.remove(key); return super.remove(key);
} }
@ -66,12 +69,14 @@ public class OrderedMap<K, V> extends ObjectMap<K, V> {
return super.remove(keys.removeIndex(index)); return super.remove(keys.removeIndex(index));
} }
public void clear (int maximumCapacity) { @Override
public void clear (int maximumCapacity) {
keys.clear(); keys.clear();
super.clear(maximumCapacity); super.clear(maximumCapacity);
} }
public void clear () { @Override
public void clear () {
keys.clear(); keys.clear();
super.clear(); super.clear();
} }
@ -80,13 +85,15 @@ public class OrderedMap<K, V> extends ObjectMap<K, V> {
return keys; return keys;
} }
public Entries<K, V> iterator () { @Override
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. */
public Entries<K, V> entries () { @Override
public Entries<K, V> entries () {
if (entries1 == null) { if (entries1 == null) {
entries1 = new OrderedMapEntries(this); entries1 = new OrderedMapEntries(this);
entries2 = new OrderedMapEntries(this); entries2 = new OrderedMapEntries(this);
@ -105,7 +112,8 @@ 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. */
public Values<V> values () { @Override
public Values<V> values () {
if (values1 == null) { if (values1 == null) {
values1 = new OrderedMapValues(this); values1 = new OrderedMapValues(this);
values2 = new OrderedMapValues(this); values2 = new OrderedMapValues(this);
@ -124,7 +132,8 @@ 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. */
public Keys<K> keys () { @Override
public Keys<K> keys () {
if (keys1 == null) { if (keys1 == null) {
keys1 = new OrderedMapKeys(this); keys1 = new OrderedMapKeys(this);
keys2 = new OrderedMapKeys(this); keys2 = new OrderedMapKeys(this);
@ -141,7 +150,8 @@ public class OrderedMap<K, V> extends ObjectMap<K, V> {
return keys2; return keys2;
} }
public String toString () { @Override
public String toString () {
if (size == 0) return "{}"; if (size == 0) return "{}";
StringBuilder buffer = new StringBuilder(32); StringBuilder buffer = new StringBuilder(32);
buffer.append('{'); buffer.append('{');
@ -157,20 +167,23 @@ public class OrderedMap<K, V> extends ObjectMap<K, V> {
return buffer.toString(); return buffer.toString();
} }
static public class OrderedMapEntries<K, V> extends Entries<K, V> { @SuppressWarnings("rawtypes")
private Array<K> keys; static public class OrderedMapEntries<K, V> extends Entries<K, V> {
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;
} }
public void reset () { @Override
public void reset () {
nextIndex = 0; nextIndex = 0;
hasNext = map.size > 0; hasNext = map.size > 0;
} }
public Entry next () { @Override
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.");
entry.key = keys.get(nextIndex); entry.key = keys.get(nextIndex);
@ -180,7 +193,8 @@ public class OrderedMap<K, V> extends ObjectMap<K, V> {
return entry; return entry;
} }
public void remove () { @Override
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);
nextIndex--; nextIndex--;
@ -188,19 +202,21 @@ 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;
} }
public void reset () { @Override
public void reset () {
nextIndex = 0; nextIndex = 0;
hasNext = map.size > 0; hasNext = map.size > 0;
} }
public K next () { @Override
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.");
K key = keys.get(nextIndex); K key = keys.get(nextIndex);
@ -210,7 +226,8 @@ public class OrderedMap<K, V> extends ObjectMap<K, V> {
return key; return key;
} }
public void remove () { @Override
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);
nextIndex = currentIndex; nextIndex = currentIndex;
@ -219,19 +236,21 @@ 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;
} }
public void reset () { @Override
public void reset () {
nextIndex = 0; nextIndex = 0;
hasNext = map.size > 0; hasNext = map.size > 0;
} }
public V next () { @Override
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.");
V value = (V)map.get(keys.get(nextIndex)); V value = (V)map.get(keys.get(nextIndex));
@ -241,7 +260,8 @@ public class OrderedMap<K, V> extends ObjectMap<K, V> {
return value; return value;
} }
public void remove () { @Override
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);
nextIndex = currentIndex; nextIndex = currentIndex;