Skip to content

Commit 89adc74

Browse files
authored
Suppress unchecked cast warnings in collection type conversions (#87)
1 parent 5a17bd2 commit 89adc74

2 files changed

Lines changed: 22 additions & 0 deletions

File tree

src/main/kotlin/cz/vutbr/fit/interlockSim/util/Array2DMap.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ class Array2DMap<V> : AbstractMap<Point, V>() /* Future: implements NavigableMap
6161

6262
private inner class Array2DEntrySet : AbstractSet<MutableMap.MutableEntry<Point, V>>() {
6363
private inner class Array2DIterator : MutableIterator<MutableMap.MutableEntry<Point, V>> {
64+
// Safe: Kotlin TreeSet.iterator() returns MutableIterator which is compatible with Java Iterator
65+
@Suppress("UNCHECKED_CAST")
6466
private val iterator: java.util.Iterator<Point> = _keys.iterator() as java.util.Iterator<Point>
6567
private var current: Point? = null
6668

src/main/kotlin/cz/vutbr/fit/interlockSim/util/HashMapGraph.kt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ class HashMapGraph<N, E, X> :
2929
ExtendedUnorientedGraph<N, E, X> {
3030
inner class NodeCollection : AbstractCollection<N>() {
3131
private inner class NodeCollectionIterator : MutableIterator<N> {
32+
// Safe: Kotlin MutableSet.iterator() returns MutableIterator which is compatible with Java Iterator
33+
@Suppress("UNCHECKED_CAST")
3234
private val keySetIterator: java.util.Iterator<Doubleton<N, X>> =
3335
keySet.iterator() as java.util.Iterator<Doubleton<N, X>>
3436
private var currentPair: Iterator<N>? = null
@@ -39,6 +41,8 @@ class HashMapGraph<N, E, X> :
3941
*/
4042
init {
4143
if (keySetIterator.hasNext()) {
44+
// Safe: Doubleton.iterator() returns MutableIterator which is compatible with Java Iterator
45+
@Suppress("UNCHECKED_CAST")
4246
currentPair = keySetIterator.next().iterator() as Iterator<N>
4347
}
4448
}
@@ -51,6 +55,8 @@ class HashMapGraph<N, E, X> :
5155

5256
override fun next(): N {
5357
if (currentPair?.hasNext() != true) {
58+
// Safe: Doubleton.iterator() returns MutableIterator which is compatible with Java Iterator
59+
@Suppress("UNCHECKED_CAST")
5460
currentPair = keySetIterator.next().iterator() as Iterator<N>
5561
}
5662
current = currentPair!!.next()
@@ -62,6 +68,8 @@ class HashMapGraph<N, E, X> :
6268
keySetIterator.hasNext()
6369
}
6470

71+
// Safe: Kotlin MutableMap.keys returns MutableSet which is compatible with Java Set
72+
@Suppress("UNCHECKED_CAST")
6573
private val keySet: java.util.Set<Doubleton<N, X>> = map.keys as java.util.Set<Doubleton<N, X>>
6674

6775
override fun iterator(): MutableIterator<N> = NodeCollectionIterator()
@@ -132,6 +140,8 @@ class HashMapGraph<N, E, X> :
132140
if (remove) iterator.remove()
133141
}
134142
}
143+
// Safe: Kotlin MutableList is compatible with Java Collection
144+
@Suppress("UNCHECKED_CAST")
135145
return collection as java.util.Collection<E>
136146
}
137147

@@ -149,6 +159,8 @@ class HashMapGraph<N, E, X> :
149159
iterator.remove()
150160
}
151161
}
162+
// Safe: Kotlin MutableSet is compatible with Java Collection
163+
@Suppress("UNCHECKED_CAST")
152164
return collection as java.util.Collection<N>
153165
}
154166

@@ -162,6 +174,8 @@ class HashMapGraph<N, E, X> :
162174
// for (Doubleton<N> c: map.keySet()) {
163175
// set.addAll(c);
164176
// }
177+
// Safe: Kotlin MutableSet is compatible with Java Set
178+
@Suppress("UNCHECKED_CAST")
165179
return set as java.util.Set<N>
166180
}
167181

@@ -203,6 +217,8 @@ class HashMapGraph<N, E, X> :
203217

204218
override fun assignedEdges(node: N): Map<X, E> {
205219
val lmap = mutableMapOf<X, E>()
220+
// Safe: Kotlin MutableMap is compatible with Java Map
221+
@Suppress("UNCHECKED_CAST")
206222
(
207223
object : DoubletonEntrySetProcessor<X>(map as java.util.Map<Doubleton<N, X>, E>) {
208224
override fun processEntryNode(
@@ -218,13 +234,17 @@ class HashMapGraph<N, E, X> :
218234
}
219235
}
220236
).process()
237+
// Safe: Kotlin MutableMap is compatible with Java Map
238+
@Suppress("UNCHECKED_CAST")
221239
return lmap as java.util.Map<X, E>
222240
}
223241

224242
override fun extensionalObject(
225243
node: N,
226244
edge: E
227245
): X {
246+
// Safe: Kotlin MutableMap is compatible with Java Map
247+
@Suppress("UNCHECKED_CAST")
228248
val p =
229249
object : DoubletonEntrySetProcessor<X>(map as java.util.Map<Doubleton<N, X>, E>) {
230250
override fun processEntryNode(

0 commit comments

Comments
 (0)