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
* 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,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
* 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. */
public ArrayMap (ArrayMap array) {
@SuppressWarnings("CopyConstructorMissesField")
public ArrayMap (ArrayMap array) {
this(array.ordered, array.size, array.keys.getClass().getComponentType(), array.values.getClass().getComponentType());
size = array.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;
}
public int hashCode () {
@Override
public int hashCode () {
K[] keys = this.keys;
V[] values = this.values;
int h = 0;
@ -418,7 +421,8 @@ public class ArrayMap<K, V> implements Iterable<ObjectMap.Entry<K, V>> {
return h;
}
public boolean equals (Object obj) {
@Override
public boolean equals (Object obj) {
if (obj == this) return true;
if (!(obj instanceof ArrayMap)) return false;
ArrayMap<K, V> other = (ArrayMap)obj;
@ -437,7 +441,8 @@ public class ArrayMap<K, V> implements Iterable<ObjectMap.Entry<K, V>> {
return true;
}
public String toString () {
@Override
public String toString () {
if (size == 0) return "{}";
K[] keys = this.keys;
V[] values = this.values;
@ -456,7 +461,8 @@ public class ArrayMap<K, V> implements Iterable<ObjectMap.Entry<K, V>> {
return buffer.toString();
}
public Iterator<Entry<K, V>> iterator () {
@Override
public Iterator<Entry<K, V>> iterator () {
return entries();
}
@ -527,17 +533,20 @@ public class ArrayMap<K, V> implements Iterable<ObjectMap.Entry<K, V>> {
this.map = map;
}
public boolean hasNext () {
@Override
public boolean hasNext () {
if (!valid) throw new RuntimeException("#iterator() cannot be used nested.");
return index < map.size;
}
public Iterator<Entry<K, V>> iterator () {
@Override
public Iterator<Entry<K, V>> iterator () {
return this;
}
/** 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 (!valid) throw new RuntimeException("#iterator() cannot be used nested.");
entry.key = map.keys[index];
@ -545,7 +554,8 @@ public class ArrayMap<K, V> implements Iterable<ObjectMap.Entry<K, V>> {
return entry;
}
public void remove () {
@Override
public void remove () {
index--;
map.removeIndex(index);
}
@ -564,22 +574,26 @@ public class ArrayMap<K, V> implements Iterable<ObjectMap.Entry<K, V>> {
this.map = map;
}
public boolean hasNext () {
@Override
public boolean hasNext () {
if (!valid) throw new RuntimeException("#iterator() cannot be used nested.");
return index < map.size;
}
public Iterator<V> iterator () {
@Override
public Iterator<V> iterator () {
return this;
}
public V next () {
@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++];
}
public void remove () {
@Override
public void remove () {
index--;
map.removeIndex(index);
}
@ -607,22 +621,26 @@ public class ArrayMap<K, V> implements Iterable<ObjectMap.Entry<K, V>> {
this.map = map;
}
public boolean hasNext () {
@Override
public boolean hasNext () {
if (!valid) throw new RuntimeException("#iterator() cannot be used nested.");
return index < map.size;
}
public Iterator<K> iterator () {
@Override
public Iterator<K> iterator () {
return this;
}
public K next () {
@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++];
}
public void remove () {
@Override
public void remove () {
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;
}
public int hashCode () {
@Override
public int hashCode () {
int h = 0;
K[] keyTable = this.keyTable;
V[] valueTable = this.valueTable;
@ -546,7 +547,8 @@ public class ObjectMap<K, V> implements Iterable<ObjectMap.Entry<K, V>> {
return h;
}
public boolean equals (Object obj) {
@Override
public boolean equals (Object obj) {
if (obj == this) return true;
if (!(obj instanceof ObjectMap)) return false;
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);
}
public String toString () {
@Override
public String toString () {
return toString(", ", true);
}
@ -602,7 +605,8 @@ public class ObjectMap<K, V> implements Iterable<ObjectMap.Entry<K, V>> {
return buffer.toString();
}
public Entries<K, V> iterator () {
@Override
public Entries<K, V> iterator () {
return entries();
}
@ -667,7 +671,8 @@ public class ObjectMap<K, V> implements Iterable<ObjectMap.Entry<K, V>> {
public K key;
public V value;
public String toString () {
@Override
public String toString () {
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 >= map.capacity) {
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. */
public Entry<K, V> next () {
@Override
public Entry<K, V> next () {
if (!hasNext) throw new NoSuchElementException();
if (!valid) throw new RuntimeException("#iterator() cannot be used nested.");
K[] keyTable = map.keyTable;
@ -735,27 +742,32 @@ public class ObjectMap<K, V> implements Iterable<ObjectMap.Entry<K, V>> {
return entry;
}
public boolean hasNext () {
@Override
public boolean hasNext () {
if (!valid) throw new RuntimeException("#iterator() cannot be used nested.");
return hasNext;
}
public Entries<K, V> iterator () {
@Override
public Entries<K, V> iterator () {
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) {
super((ObjectMap<Object, V>)map);
}
public boolean hasNext () {
@Override
public boolean hasNext () {
if (!valid) throw new RuntimeException("#iterator() cannot be used nested.");
return hasNext;
}
public V next () {
@Override
public V next () {
if (!hasNext) throw new NoSuchElementException();
if (!valid) throw new RuntimeException("#iterator() cannot be used nested.");
V value = map.valueTable[nextIndex];
@ -764,7 +776,8 @@ public class ObjectMap<K, V> implements Iterable<ObjectMap.Entry<K, V>> {
return value;
}
public Values<V> iterator () {
@Override
public Values<V> iterator () {
return this;
}
@ -786,12 +799,14 @@ public class ObjectMap<K, V> implements Iterable<ObjectMap.Entry<K, V>> {
super((ObjectMap<K, Object>)map);
}
public boolean hasNext () {
@Override
public boolean hasNext () {
if (!valid) throw new RuntimeException("#iterator() cannot be used nested.");
return hasNext;
}
public K next () {
@Override
public K next () {
if (!hasNext) throw new NoSuchElementException();
if (!valid) throw new RuntimeException("#iterator() cannot be used nested.");
K key = map.keyTable[nextIndex];
@ -800,7 +815,8 @@ public class ObjectMap<K, V> implements Iterable<ObjectMap.Entry<K, V>> {
return key;
}
public Keys<K> iterator () {
@Override
public Keys<K> iterator () {
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
* 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,12 +53,14 @@ public class OrderedMap<K, V> extends ObjectMap<K, V> {
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);
return super.put(key, value);
}
public V remove (K key) {
@Override
public V remove (K key) {
keys.removeValue(key, false);
return super.remove(key);
}
@ -66,12 +69,14 @@ public class OrderedMap<K, V> extends ObjectMap<K, V> {
return super.remove(keys.removeIndex(index));
}
public void clear (int maximumCapacity) {
@Override
public void clear (int maximumCapacity) {
keys.clear();
super.clear(maximumCapacity);
}
public void clear () {
@Override
public void clear () {
keys.clear();
super.clear();
}
@ -80,13 +85,15 @@ public class OrderedMap<K, V> extends ObjectMap<K, V> {
return keys;
}
public Entries<K, V> iterator () {
@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. */
public Entries<K, V> entries () {
@Override
public Entries<K, V> entries () {
if (entries1 == null) {
entries1 = 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
* 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) {
values1 = 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
* 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) {
keys1 = new OrderedMapKeys(this);
keys2 = new OrderedMapKeys(this);
@ -141,7 +150,8 @@ public class OrderedMap<K, V> extends ObjectMap<K, V> {
return keys2;
}
public String toString () {
@Override
public String toString () {
if (size == 0) return "{}";
StringBuilder buffer = new StringBuilder(32);
buffer.append('{');
@ -157,20 +167,23 @@ public class OrderedMap<K, V> extends ObjectMap<K, V> {
return buffer.toString();
}
static public class OrderedMapEntries<K, V> extends Entries<K, V> {
private Array<K> keys;
@SuppressWarnings("rawtypes")
static public class OrderedMapEntries<K, V> extends Entries<K, V> {
private final Array<K> keys;
public OrderedMapEntries (OrderedMap<K, V> map) {
super(map);
keys = map.keys;
}
public void reset () {
@Override
public void reset () {
nextIndex = 0;
hasNext = map.size > 0;
}
public Entry next () {
@Override
public Entry next () {
if (!hasNext) throw new NoSuchElementException();
if (!valid) throw new RuntimeException("#iterator() cannot be used nested.");
entry.key = keys.get(nextIndex);
@ -180,7 +193,8 @@ public class OrderedMap<K, V> extends ObjectMap<K, V> {
return entry;
}
public void remove () {
@Override
public void remove () {
if (currentIndex < 0) throw new IllegalStateException("next must be called before remove.");
map.remove(entry.key);
nextIndex--;
@ -188,19 +202,21 @@ 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;
}
public void reset () {
@Override
public void reset () {
nextIndex = 0;
hasNext = map.size > 0;
}
public K next () {
@Override
public K next () {
if (!hasNext) throw new NoSuchElementException();
if (!valid) throw new RuntimeException("#iterator() cannot be used nested.");
K key = keys.get(nextIndex);
@ -210,7 +226,8 @@ public class OrderedMap<K, V> extends ObjectMap<K, V> {
return key;
}
public void remove () {
@Override
public void remove () {
if (currentIndex < 0) throw new IllegalStateException("next must be called before remove.");
((OrderedMap)map).removeIndex(nextIndex - 1);
nextIndex = currentIndex;
@ -219,19 +236,21 @@ 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;
}
public void reset () {
@Override
public void reset () {
nextIndex = 0;
hasNext = map.size > 0;
}
public V next () {
@Override
public V next () {
if (!hasNext) throw new NoSuchElementException();
if (!valid) throw new RuntimeException("#iterator() cannot be used nested.");
V value = (V)map.get(keys.get(nextIndex));
@ -241,7 +260,8 @@ public class OrderedMap<K, V> extends ObjectMap<K, V> {
return value;
}
public void remove () {
@Override
public void remove () {
if (currentIndex < 0) throw new IllegalStateException("next must be called before remove.");
((OrderedMap)map).removeIndex(currentIndex);
nextIndex = currentIndex;