Skip to content

Commit 1e246bf

Browse files
bedaHovorkaclaude
andcommitted
Replace Java collection types with Kotlin idiomatic equivalents
Modernize collection usage across the codebase by replacing explicit Java types with Kotlin stdlib alternatives. This improves type inference, reduces boilerplate, and aligns with Kotlin best practices. Changes: - Replace ArrayList/LinkedHashMap/HashMap constructors with mutableListOf(), linkedMapOf(), mutableMapOf() - Replace Collections.addAll() with collection.addAll(array.asList()) - Replace java.util.Deque with kotlin.collections.ArrayDeque in ArrayPath - Simplify iterator type declarations (remove explicit java.util.Iterator) - Remove unnecessary @Suppress annotations for type casts - Update Path interface to extend MutableCollection instead of java.util.Deque - Make Cell.getSpatialType() nullable (returns null for TrackBlockPart) - Remove redundant java.util.* imports throughout All 662 tests pass. No functional changes to simulation logic. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent 0c15cf6 commit 1e246bf

29 files changed

Lines changed: 164 additions & 211 deletions

src/main/kotlin/cz/vutbr/fit/interlockSim/context/AbstractRailwayNetGrid.kt

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@ package cz.vutbr.fit.interlockSim.context
1212
import cz.vutbr.fit.interlockSim.objects.cells.Cell
1313
import cz.vutbr.fit.interlockSim.util.Array2DMap
1414
import cz.vutbr.fit.interlockSim.util.Point
15-
import java.util.Iterator
16-
import java.util.Map.Entry
17-
import java.util.Set
1815
import java.util.WeakHashMap
1916

2017
/**
@@ -52,11 +49,9 @@ abstract class AbstractRailwayNetGrid(
5249

5350
override fun getRows(): Int = _rows
5451

55-
override fun iterator(): kotlin.collections.Iterator<Entry<Point, Cell>> {
52+
override fun iterator(): kotlin.collections.Iterator<Map.Entry<Point, Cell>> {
5653
// Build entries from cells and return an immutable iterator
57-
@Suppress("UNCHECKED_CAST")
58-
val entries = (cells.entries as java.util.Set<Entry<Point, Cell>>).toList()
59-
return entries.iterator()
54+
return cells.entries.toList().iterator()
6055
}
6156

6257
override fun getLocation(value: Cell): Point? = reverseTable[value]

src/main/kotlin/cz/vutbr/fit/interlockSim/context/DefaultContext.kt

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,8 @@ import jDisco.DiscoException
4848
import jDisco.Process
4949
import jDisco.Random
5050
import java.beans.PropertyChangeSupport
51-
import java.util.ArrayList
52-
import java.util.Collection
53-
import java.util.Collections
5451
import java.util.EnumSet
5552
import java.util.IdentityHashMap
56-
import java.util.LinkedHashMap
57-
import java.util.List
58-
import java.util.Map
59-
import java.util.Set
6053
import java.util.TreeMap
6154

6255
/**
@@ -90,7 +83,7 @@ abstract class DefaultContext :
9083
/**
9184
* List of entry/exit points in the railway network
9285
*/
93-
private var inouts: ArrayList<InOut> = ArrayList(2)
86+
private var inouts: MutableList<InOut> = mutableListOf()
9487

9588
/**
9689
* Workers for each entry/exit point
@@ -297,7 +290,7 @@ abstract class DefaultContext :
297290
val p1 = pi1
298291
val p2 = pi2
299292

300-
val keys: MutableList<Point> = ArrayList()
293+
val keys: MutableList<Point> = mutableListOf()
301294
val b = bresenham(key1, key2, p1, p2, keys)
302295
if (keys.isEmpty()) return null
303296

@@ -310,11 +303,9 @@ abstract class DefaultContext :
310303

311304
if (builtPath != null && builtPath.isNotEmpty()) {
312305
@Suppress("UNCHECKED_CAST")
313-
val mapToAdd = builtPath as java.util.Map<Point, TrackBlockPart>
306+
val mapToAdd = builtPath as MutableMap<Point, TrackBlockPart>
314307
getGrid().putMap(mapToAdd)
315-
@Suppress("UNCHECKED_CAST")
316-
val mapKeys: Set<Point> = mapToAdd.keySet() as Set<Point>
317-
linesKeys[trackBlock] = mapKeys
308+
linesKeys[trackBlock] = mapToAdd.keys.toSet()
318309
requireValidState(!extendedUnorientedGraph.contains(key1, key2)) {
319310
"Graph already contains edge between ($key1, $key2)"
320311
}
@@ -336,7 +327,7 @@ abstract class DefaultContext :
336327
requireValidArgument(bresenham != null && bresenham.isNotEmpty()) {
337328
"Bresenham path list cannot be null or empty"
338329
}
339-
val map: MutableMap<Point, TrackBlockPart> = LinkedHashMap()
330+
val map: MutableMap<Point, TrackBlockPart> = linkedMapOf()
340331
var from = key1
341332
var middle = bresenham[0]
342333
var part: TrackBlockPart?
@@ -559,7 +550,7 @@ abstract class DefaultContext :
559550
)
560551
}
561552
}
562-
if (nodeCell is InOut) getInOuts().add(nodeCell as InOut)
553+
if (nodeCell is InOut) inouts.add(nodeCell as InOut)
563554
changeSupport.firePropertyChange(ContextChangeListener.CELL_ADDED, null, key)
564555
logger.trace { "Added ${nodeCell.javaClass.simpleName} at (${key.x},${key.y})" }
565556
}
@@ -926,7 +917,7 @@ abstract class DefaultContext :
926917
if (types == null || types.isEmpty()) {
927918
allowedReportTypes.clear()
928919
} else {
929-
Collections.addAll(allowedReportTypes, *types)
920+
allowedReportTypes.addAll(types.asList())
930921
}
931922
}
932923

src/main/kotlin/cz/vutbr/fit/interlockSim/context/DefaultRailWayNetGrid.kt

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@ import cz.vutbr.fit.interlockSim.objects.cells.Cell
1414
import cz.vutbr.fit.interlockSim.objects.cells.TrackBlockPart
1515
import cz.vutbr.fit.interlockSim.util.Point
1616
import java.util.AbstractSet
17-
import java.util.Iterator
18-
import java.util.Map
19-
import java.util.Map.Entry
20-
import java.util.Set
2117

2218
/**
2319
* Grid represantation
@@ -27,18 +23,17 @@ class DefaultRailWayNetGrid(
2723
rows: Int
2824
) : AbstractRailwayNetGrid(cols, rows) {
2925
private inner class KeySet(
30-
private val set: Set<Entry<Point, Cell>>
26+
private val set: Set<Map.Entry<Point, Cell>>
3127
) : AbstractSet<Point>() {
3228
override fun iterator(): MutableIterator<Point> {
33-
@Suppress("UNCHECKED_CAST")
34-
val delegate: Iterator<Entry<Point, Cell>> = (set.iterator() as Iterator<Entry<Point, Cell>>)
29+
val delegate: Iterator<Map.Entry<Point, Cell>> = set.iterator()
3530
return PointIterator(delegate)
3631
}
3732

3833
private inner class PointIterator(
39-
private val delegate: Iterator<Entry<Point, Cell>>
34+
private val delegate: Iterator<Map.Entry<Point, Cell>>
4035
) : MutableIterator<Point> {
41-
private var next: Entry<Point, Cell>? = null
36+
private var next: Map.Entry<Point, Cell>? = null
4237

4338
override fun next(): Point {
4439
next = delegate.next()
@@ -50,10 +45,10 @@ class DefaultRailWayNetGrid(
5045
override fun remove() {
5146
if (delegate is MutableIterator<*>) {
5247
// Save the cell BEFORE removing from cells map
53-
// (Entry.getValue() will fail after removal since it looks up the key)
48+
// (Map.Entry.getValue() will fail after removal since it looks up the key)
5449
val cell = next?.value
5550
// Remove from cells map via delegate
56-
(delegate as MutableIterator<Entry<Point, Cell>>).remove()
51+
(delegate as MutableIterator<Map.Entry<Point, Cell>>).remove()
5752
// Also remove from reverse table to maintain invariant
5853
if (cell != null) {
5954
getReverseTable().remove(cell)
@@ -117,11 +112,9 @@ class DefaultRailWayNetGrid(
117112
* @param map of point to trackblock part
118113
*/
119114
@Synchronized
120-
fun putMap(map: java.util.Map<Point, TrackBlockPart>) {
121-
val iter = map.entrySet().iterator()
122-
while (iter.hasNext()) {
123-
val entry = iter.next()
124-
put(entry.key, entry.value)
115+
fun putMap(map: Map<Point, TrackBlockPart>) {
116+
for ((key, value) in map) {
117+
put(key, value)
125118
}
126119
}
127120

@@ -171,9 +164,9 @@ class DefaultRailWayNetGrid(
171164
* All cell in grid
172165
* @return set of cells
173166
*/
174-
fun keySet(): Set<Point> {
167+
fun keySet(): MutableSet<Point> {
175168
@Suppress("UNCHECKED_CAST")
176-
val entries = getCells().entries as java.util.Set<Entry<Point, Cell>>
177-
return KeySet(entries) as Set<Point>
169+
val entries = getCells().entries as Set<Map.Entry<Point, Cell>>
170+
return KeySet(entries) as MutableSet<Point>
178171
}
179172
}

src/main/kotlin/cz/vutbr/fit/interlockSim/context/RailwayNetGrid.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,14 @@ package cz.vutbr.fit.interlockSim.context
1111

1212
import cz.vutbr.fit.interlockSim.objects.cells.Cell
1313
import cz.vutbr.fit.interlockSim.util.Point
14-
import java.util.Iterator
15-
import java.util.Map.Entry
1614

1715
/**
1816
* Read-only access interface to Grid...
1917
*
2018
* aby bylo rozhrani dokonale tak iterator vraceny implementaci tohoto rozhrani
2119
* musi u [Iterator.remove] hazet [UnsupportedOperationException]
2220
*/
23-
interface RailwayNetGrid : Iterable<Entry<Point, Cell>> {
21+
interface RailwayNetGrid : Iterable<Map.Entry<Point, Cell>> {
2422
/**
2523
* @param x
2624
* @param y

src/main/kotlin/cz/vutbr/fit/interlockSim/context/SimulationContext.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import cz.vutbr.fit.interlockSim.objects.tracks.TrackBlock
2020
import cz.vutbr.fit.interlockSim.objects.tracks.TrackSection
2121
import cz.vutbr.fit.interlockSim.sim.InOutWorker
2222
import cz.vutbr.fit.interlockSim.exceptions.SimulationException
23-
import java.util.Collection
2423
import java.util.EnumSet
2524

2625
/**

src/main/kotlin/cz/vutbr/fit/interlockSim/gui/action/NodeCellAction.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import java.awt.Color
2020
import java.awt.RenderingHints
2121
import java.awt.event.ActionEvent
2222
import java.awt.image.BufferedImage
23-
import java.util.HashMap
2423
import javax.swing.AbstractAction
2524
import javax.swing.Icon
2625
import javax.swing.ImageIcon
@@ -43,8 +42,8 @@ class NodeCellAction(
4342
companion object {
4443
private const val iconSize: Int = 20
4544
private val editorCellRenderer = EditorCellRenderer(iconSize, iconSize)
46-
private val renderingHints: HashMap<RenderingHints.Key, Any> =
47-
HashMap<RenderingHints.Key, Any>().apply {
45+
private val renderingHints: MutableMap<RenderingHints.Key, Any> =
46+
mutableMapOf<RenderingHints.Key, Any>().apply {
4847
put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON)
4948
put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_SPEED)
5049
}

src/main/kotlin/cz/vutbr/fit/interlockSim/objects/cells/AbstractCell.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,18 @@ package cz.vutbr.fit.interlockSim.objects.cells
1111

1212
import cz.vutbr.fit.interlockSim.exceptions.requireValidState
1313
import java.util.EnumSet
14-
import java.util.Set
1514

1615
/**
1716
* Base implementation of {@link Cell}
1817
*
1918
*/
2019
abstract class AbstractCell : Cell {
21-
protected fun joinsOnLine(): Set<Cell.Segment> {
20+
protected fun joinsOnLine(): MutableSet<Cell.Segment> {
2221
val st = getSpatialType()
2322
requireValidState(st != null) { "Spatial type is null for cell: $this" }
2423
val arr2set = arr2set(st!!.segments)
2524
requireValidState(arr2set.size == 2) { "Expected 2 segments but got ${arr2set.size}" }
26-
return arr2set
25+
return arr2set.toMutableSet()
2726
}
2827

2928
protected fun secondOnLine(from: Cell.Segment?): Cell.Segment? {

src/main/kotlin/cz/vutbr/fit/interlockSim/objects/cells/Cell.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ package cz.vutbr.fit.interlockSim.objects.cells
1111

1212
import cz.vutbr.fit.interlockSim.exceptions.requireValidState
1313
import cz.vutbr.fit.interlockSim.util.Point
14-
import java.util.Set
1514

1615
/**
1716
* Cell in Grid
@@ -122,7 +121,7 @@ interface Cell {
122121
*
123122
* @return {@link SpatialType}
124123
*/
125-
fun getSpatialType(): SpatialType
124+
fun getSpatialType(): SpatialType?
126125

127126
/**
128127
* @return Possible joins

src/main/kotlin/cz/vutbr/fit/interlockSim/objects/cells/InOut.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import cz.vutbr.fit.interlockSim.objects.cells.RailSemaphore.Signal
1414
import cz.vutbr.fit.interlockSim.objects.paths.PathElement
1515
import cz.vutbr.fit.interlockSim.exceptions.PathSeparatorChangeException
1616
import java.util.EnumSet
17-
import java.util.Set
1817

1918
/**
2019
* predstavuje spojeni s externi zeleznicni siti

src/main/kotlin/cz/vutbr/fit/interlockSim/objects/cells/OrientedNodeCell.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ package cz.vutbr.fit.interlockSim.objects.cells
1212
import cz.vutbr.fit.interlockSim.exceptions.requireSimulationNotNull
1313
import cz.vutbr.fit.interlockSim.objects.paths.OrientedPathSeparator
1414
import java.util.EnumSet
15-
import java.util.Set
1615

1716
/**
1817
* Base implementation of {@link OrientedPathSeparator}

0 commit comments

Comments
 (0)