Skip to content

Commit 014231d

Browse files
authored
Remove redundant null checks from Java-to-Kotlin migration (#88)
1 parent b3d4521 commit 014231d

14 files changed

Lines changed: 12 additions & 60 deletions

File tree

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

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ import cz.vutbr.fit.interlockSim.sim.InOutWorker
2828
import cz.vutbr.fit.interlockSim.sim.LoopProcess
2929
import cz.vutbr.fit.interlockSim.sim.ShuntingLoop
3030
import cz.vutbr.fit.interlockSim.exceptions.SimulationException
31-
import cz.vutbr.fit.interlockSim.exceptions.requireEditor
32-
import cz.vutbr.fit.interlockSim.exceptions.requireEditorNotNull
3331
import cz.vutbr.fit.interlockSim.exceptions.requireSimulation
3432
import cz.vutbr.fit.interlockSim.exceptions.requireSimulationNotNull
3533
import cz.vutbr.fit.interlockSim.exceptions.requireValidArgument
@@ -245,11 +243,9 @@ abstract class DefaultContext :
245243
val nodecell2: NodeCell = Util.assertNodeCell(getGrid().get(key2)!!)
246244

247245
for (s1: Segment in nodecell1.joins()) {
248-
requireEditorNotNull(s1) { "Segment s1 cannot be null for nodecell $nodecell1" }
249246
val p1 = s1.transform(key1)
250247
if (used(p1)) continue
251248
for (s2: Segment in nodecell2.joins()) {
252-
requireEditorNotNull(s2) { "Segment s2 cannot be null for nodecell $nodecell2" }
253249
if (s1 == s2) continue // stejne segmenty se nepropoji
254250
val p2 = s2.transform(key2)
255251
if (used(p2)) continue
@@ -284,7 +280,6 @@ abstract class DefaultContext :
284280
key2: Point,
285281
trackBlock: TrackBlock
286282
): Boolean {
287-
requireEditor(key1 != null && key2 != null) { "Keys cannot be null in hardJoin" }
288283
if (key1.distance(key2) > SQRT2) {
289284
val blockEndFrom = s1.transform(key1)
290285
val blockEndTo = s2.transform(key2)
@@ -321,7 +316,6 @@ abstract class DefaultContext :
321316
key2: Point,
322317
trackBlock: TrackBlock
323318
): Map<Point, TrackBlockPart>? {
324-
if (pi1 == null || pi2 == null) throw IllegalArgumentException()
325319
val p1 = pi1
326320
val p2 = pi2
327321

@@ -359,8 +353,8 @@ abstract class DefaultContext :
359353
key2: Point,
360354
trackBlock: TrackBlock
361355
): MutableMap<Point, TrackBlockPart>? {
362-
requireValidArgument(bresenham != null && bresenham.isNotEmpty()) {
363-
"Bresenham path list cannot be null or empty"
356+
requireValidArgument(bresenham.isNotEmpty()) {
357+
"Bresenham path list cannot be empty"
364358
}
365359
val map: MutableMap<Point, TrackBlockPart> = linkedMapOf()
366360
var from = key1
@@ -394,7 +388,6 @@ abstract class DefaultContext :
394388
to: Point,
395389
block: TrackBlock
396390
): TrackBlockPart? {
397-
requireEditorNotNull(block) { "TrackBlock cannot be null in createPart" }
398391
if (used(middle)) return null
399392
if (from == to || from == middle || middle == to) return null
400393

@@ -426,9 +419,6 @@ abstract class DefaultContext :
426419
p2: Point,
427420
points: MutableList<Point>
428421
): Boolean {
429-
requireValidArgument(key1 != null && key2 != null && p1 != null && p2 != null) {
430-
"All points must be non-null in bresenham algorithm"
431-
}
432422
requireValidArgument(key1 != p1 && key2 != p2 && key1 != p2 && key2 != p1) {
433423
"Keys and intermediate points must be distinct in bresenham algorithm"
434424
}
@@ -548,7 +538,6 @@ abstract class DefaultContext :
548538
key: Point,
549539
nodeCell: NodeCell
550540
) {
551-
requireEditorNotNull(key) { "Cell key cannot be null in putCell" }
552541
// Validate coordinates are within grid bounds
553542
if (key.x < 0 || key.y < 0 || key.x >= railwayNetGrid.getCols() || key.y >= railwayNetGrid.getRows()) {
554543
throw ContextCreationException(
@@ -560,7 +549,6 @@ abstract class DefaultContext :
560549

561550
// vedlejsi Nody (sousedni bunky)
562551
for (s1: Segment in nodeCell.joins()) {
563-
requireEditorNotNull(s1) { "Segment s1 cannot be null for nodeCell $nodeCell" }
564552
val p = s1.transform(key)
565553
// Skip neighbor if it's outside grid bounds (boundary cells)
566554
if (p.x < 0 || p.y < 0 || p.x >= railwayNetGrid.getCols() || p.y >= railwayNetGrid.getRows()) {
@@ -619,7 +607,6 @@ abstract class DefaultContext :
619607
* Remove a track line from the railway network
620608
*/
621609
override fun removeLine(line: TrackBlock) {
622-
requireEditorNotNull(line) { "TrackBlock line cannot be null in removeLine" }
623610
extendedUnorientedGraph.remove(line)
624611
getGrid().keySet().removeAll(linesKeys.remove(line) ?: emptySet())
625612
changeSupport.firePropertyChange(
@@ -656,7 +643,6 @@ abstract class DefaultContext :
656643
): Segment? {
657644
// If track is not null, use it; otherwise use secondEndTrack
658645
if (track != null) return getSegment(separator, track)
659-
requireValidArgument(separator != null) { "PathSeparator cannot be null" }
660646
requireValidArgument(secondEndTrack != null) { "secondEndTrack cannot be null for separator $separator" }
661647
requireValidArgument(separator is OrientedPathSeparator) {
662648
"PathSeparator must be OrientedPathSeparator, got ${separator.javaClass.simpleName}"
@@ -673,9 +659,6 @@ abstract class DefaultContext :
673659
separator: PathSeparator,
674660
track: Track
675661
): Segment? {
676-
if (separator == null || track == null) {
677-
throw IllegalArgumentException("separator or track is null")
678-
}
679662
return if (track is TrackSection) {
680663
@Suppress("UNCHECKED_CAST")
681664
val section = track as TrackSection
@@ -696,9 +679,6 @@ abstract class DefaultContext :
696679
separator: PathSeparator,
697680
section: TrackSection
698681
): Segment? {
699-
if (section == null) {
700-
throw IllegalArgumentException("section is null")
701-
}
702682
val trackBlock = section.getTrackBlock()
703683
if (trackBlock.isInnerElement(separator)) {
704684
return trackBlock.getJoin(separator, section)
@@ -749,7 +729,6 @@ abstract class DefaultContext :
749729
nodeCell: NodeCell,
750730
current: TrackBlock?
751731
): TrackBlock? {
752-
if (nodeCell == null) throw IllegalArgumentException("node is null")
753732
val location = getLocation(nodeCell)
754733
val segment = getSegment(location, current)
755734
val followingSegment = nodeCell.getFollowingSegment(segment)
@@ -766,7 +745,6 @@ abstract class DefaultContext :
766745
separator: PathSeparator,
767746
current: TrackSection?
768747
): TrackSection? {
769-
if (separator == null) throw IllegalArgumentException("separator is null")
770748
var trackBlock: TrackBlock? = null
771749
if (current != null) {
772750
trackBlock = current.getTrackBlock()
@@ -862,12 +840,10 @@ abstract class DefaultContext :
862840
next: Track?,
863841
previous: Track?
864842
): Boolean {
865-
requireSimulation(separator != null) { "Separator cannot be null in isSeparatorInDirection" }
866843
val segment = getSegment(separator, next, previous)
867844
if (segment == null && separator is InOut) return true
868845
requireSimulation(segment != null) { "Segment cannot be null for separator $separator" }
869846
val direction = separator.direction()
870-
requireSimulation(direction != null) { "Direction cannot be null for oriented separator" }
871847
val inDirection = segment === direction
872848
logger.debug {
873849
"isSeparatorInDirection: separator $separator, segment=$segment, direction=$direction, result=$inDirection"
@@ -882,9 +858,6 @@ abstract class DefaultContext :
882858
sep: PathSeparator,
883859
nxt: TrackSection
884860
): Path? {
885-
if (sep == null || nxt == null) {
886-
throw IllegalArgumentException("wrong arguments for aPath finding")
887-
}
888861
logger.debug { "pathToNextSemaphore: searching path from $sep via track section" }
889862
var separator = sep
890863
var previous: TrackSection? = null
@@ -940,7 +913,7 @@ abstract class DefaultContext :
940913
* Add report types to be reported
941914
*/
942915
override fun addReportTypes(vararg types: ReportType) {
943-
if (types == null || types.isEmpty()) {
916+
if (types.isEmpty()) {
944917
allowedReportTypes.clear()
945918
} else {
946919
allowedReportTypes.addAll(types.asList())
@@ -956,9 +929,8 @@ abstract class DefaultContext :
956929
* Remove report types from reporting
957930
*/
958931
override fun removeReportTypes(vararg types: ReportType) {
959-
if (types == null || types.isEmpty()) return
932+
if (types.isEmpty()) return
960933
for (t in types) {
961-
if (t == null) continue
962934
allowedReportTypes.remove(t)
963935
}
964936
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,6 @@ class DefaultRailWayNetGrid(
142142
*/
143143
@Synchronized
144144
fun remove(key: Point) {
145-
requireValidState(key != null) { "Key cannot be null" }
146145
val removed: Cell? = getCells().remove(key)
147146
val remove2: Point? = getReverseTable().remove(removed)
148147
requireValidState(key == remove2) {

src/main/kotlin/cz/vutbr/fit/interlockSim/gui/RailwayNetGridCanvas.kt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ import cz.vutbr.fit.interlockSim.context.EditingContext
1414
import cz.vutbr.fit.interlockSim.context.EditingContextFactory
1515
import cz.vutbr.fit.interlockSim.context.SimulationContext
1616
import cz.vutbr.fit.interlockSim.exceptions.requireEditor
17-
import cz.vutbr.fit.interlockSim.exceptions.requireEditorNotNull
18-
import cz.vutbr.fit.interlockSim.exceptions.requireValidState
1917
import cz.vutbr.fit.interlockSim.gui.gridcanvas.CellRenderer
2018
import cz.vutbr.fit.interlockSim.gui.gridcanvas.EditorCellRenderer
2119
import cz.vutbr.fit.interlockSim.gui.gridcanvas.GridCanvasEditingPopupMenu
@@ -234,7 +232,6 @@ class RailwayNetGridCanvas :
234232

235233
// Update context and recalculate display
236234
private fun changeContext(cont: Context) {
237-
requireEditorNotNull(cont) { "Context cannot be null" }
238235
if (context != null) {
239236
context!!.removePropertyChangeListener(this)
240237
}
@@ -263,7 +260,6 @@ class RailwayNetGridCanvas :
263260
for (entry in grid) {
264261
val key = entry.key
265262
val cell = entry.value
266-
requireValidState(key != null && cell != null) { "Grid entry has null key or value: ($key, $cell)" }
267263

268264
val x = key.x * CELL_WIDTH
269265
val y = key.y * CELL_HEIGHT

src/main/kotlin/cz/vutbr/fit/interlockSim/gui/StatusBar.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@ class StatusBar :
3535
val source = e.source
3636
requireValidState(source is StatusProducer) { "Source must be a StatusProducer" }
3737
val status = (source as StatusProducer).getStatus(e)
38-
if (status != null) {
39-
text = status
40-
}
38+
text = status
4139
}
4240
}
4341

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,7 @@ class NodeCellAction(
6666
g.color = Color.WHITE
6767
g.fillRect(0, 0, iconSize, iconSize)
6868
g.color = Color.BLACK
69-
if (cell != null) {
70-
editorCellRenderer.draw(g, cell)
71-
}
69+
editorCellRenderer.draw(g, cell)
7270
return ImageIcon(img)
7371
}
7472

src/main/kotlin/cz/vutbr/fit/interlockSim/gui/gridcanvas/CellRenderer.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@ abstract class CellRenderer(
113113
g: Graphics2D,
114114
cell: Cell
115115
) {
116-
requireValidState(cell != null) { "Cell cannot be null" }
117116

118117
try {
119118
// Use reflection to find and invoke the specific draw method for this cell type

src/main/kotlin/cz/vutbr/fit/interlockSim/gui/gridcanvas/GridCanvasPopupMenu.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
*/
1010
package cz.vutbr.fit.interlockSim.gui.gridcanvas
1111

12-
import cz.vutbr.fit.interlockSim.exceptions.requireValidState
1312
import cz.vutbr.fit.interlockSim.gui.RailwayNetGridCanvas
1413
import cz.vutbr.fit.interlockSim.objects.cells.Cell
1514
import cz.vutbr.fit.interlockSim.objects.cells.NodeCell
@@ -57,7 +56,6 @@ abstract class GridCanvasPopupMenu : JPopupMenu() {
5756
key: cz.vutbr.fit.interlockSim.util.Point?,
5857
cell: Cell?
5958
) {
60-
requireValidState(canvas != null && e != null) { "Canvas and event cannot be null" }
6159
if (key == null || cell == null) return
6260
this.canvas = canvas
6361
reorganizeMenu(key, cell)

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ interface Cell {
8181
* @return neighbour Point
8282
*/
8383
fun transform(from: Point): Point {
84-
requireValidState(from != null) { "Point cannot be null" }
8584
val tr = Point(from.x + dx, from.y + dy)
8685
requireValidState(from != tr) { "Transformed point must be different from original: segment=$this" }
8786
return tr

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ open class RailSemaphore(
9999
open fun setSignal(signal: Signal) {
100100
logger.debug {
101101
if (this.signal != signal) {
102-
"Semaphore ${if (getName() != null) getName() else this.hashCode()} " +
102+
"Semaphore ${getName()} " +
103103
"signal change: ${this.signal} -> $signal at t=${jDisco.Process.time()}"
104104
} else {
105105
""

src/main/kotlin/cz/vutbr/fit/interlockSim/objects/paths/AbstractPath.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import cz.vutbr.fit.interlockSim.context.SimulationContext
1313
import cz.vutbr.fit.interlockSim.context.SimulationContext.ReportType
1414
import cz.vutbr.fit.interlockSim.exceptions.requireSimulation
1515
import cz.vutbr.fit.interlockSim.exceptions.requireSimulationNotNull
16-
import cz.vutbr.fit.interlockSim.exceptions.requireValidArgument
1716
import cz.vutbr.fit.interlockSim.objects.cells.InOut
1817
import cz.vutbr.fit.interlockSim.objects.cells.RailSemaphore
1918
import cz.vutbr.fit.interlockSim.objects.cells.RailSwitch
@@ -191,7 +190,6 @@ abstract class AbstractPath protected constructor(
191190
previous: Track?,
192191
next: Track
193192
): Boolean {
194-
requireValidArgument(methodName != null && next != null) { "Method name and next track must not be null" }
195193
val from = context.getSegment(separator, previous, next)
196194
val to = context.getSegment(separator, next, previous)
197195
requireSimulation(!conflict(from, to)) { "Segment conflict: from=$from, to=$to" }

0 commit comments

Comments
 (0)