Skip to content

Commit 376324c

Browse files
committed
Refactor - remove @JvmStatic and 2JvmField
Move static utility functions from companion objects to package-level top-level functions across cell and path classes, following Kotlin idioms and improving code organization. This eliminates unnecessary companion object nesting for stateless utility functions. Changes: - Cell: Extract segmentFor(), conflict(), anti(), d2r(), r2d() to package level - AbstractCell: Extract arr2set() to package level - RailSemaphore: Extract forSpeed(), getConstantInstance() to package level - RailSwitch: Extract helper functions and constants to package level - Update all call sites to use top-level functions instead of companion references - Maintain full backward compatibility with existing functionality All 698 tests pass. No functional changes, pure refactoring.
1 parent f97862c commit 376324c

13 files changed

Lines changed: 255 additions & 257 deletions

File tree

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
package cz.vutbr.fit.interlockSim.context
1111

1212
import cz.vutbr.fit.interlockSim.context.SimulationContext.ReportType
13-
import cz.vutbr.fit.interlockSim.objects.cells.Cell
1413
import cz.vutbr.fit.interlockSim.objects.cells.Cell.Segment
1514
import cz.vutbr.fit.interlockSim.objects.cells.InOut
1615
import cz.vutbr.fit.interlockSim.objects.cells.NodeCell
@@ -29,6 +28,9 @@ import cz.vutbr.fit.interlockSim.sim.InOutWorker
2928
import cz.vutbr.fit.interlockSim.sim.LoopProcess
3029
import cz.vutbr.fit.interlockSim.sim.ShuntingLoop
3130
import cz.vutbr.fit.interlockSim.exceptions.SimulationException
31+
import cz.vutbr.fit.interlockSim.objects.cells.anti
32+
import cz.vutbr.fit.interlockSim.objects.cells.conflict
33+
import cz.vutbr.fit.interlockSim.objects.cells.segmentFor
3234
import cz.vutbr.fit.interlockSim.util.ExtendedUnorientedGraph
3335
import cz.vutbr.fit.interlockSim.util.HashMapGraph
3436
import cz.vutbr.fit.interlockSim.util.Point
@@ -369,10 +371,10 @@ abstract class DefaultContext :
369371
return null
370372
}
371373

372-
val s1 = Cell.Segment.segmentFor(ux, uy)
373-
val s2 = Cell.Segment.segmentFor(vx, vy)
374+
val s1 = segmentFor(ux, uy)
375+
val s2 = segmentFor(vx, vy)
374376
// vyhodit nehezky dvojice segmentu
375-
if (s1 == null || s2 == null || Cell.Segment.conflict(s1, s2)) {
377+
if (s1 == null || s2 == null || conflict(s1, s2)) {
376378
return null
377379
}
378380
return TrackBlockPart(block, arrayOf(s1, s2))
@@ -390,7 +392,7 @@ abstract class DefaultContext :
390392
): Boolean {
391393
assert(key1 != null && key2 != null && p1 != null && p2 != null)
392394
assert(key1 != p1 && key2 != p2 && key1 != p2 && key2 != p1)
393-
395+
394396
// Make mutable copies since we need to modify them for the algorithm
395397
var p1Mut = p1
396398
var p2Mut = p2
@@ -529,7 +531,7 @@ abstract class DefaultContext :
529531
val nodeCell2 = cell2
530532

531533
// vzit proti-segment
532-
val s2 = Segment.anti(s1)
534+
val s2 = anti(s1)
533535
if (nodeCell2.joins().contains(s2)) {
534536
assert(s2.transform(p) == key)
535537
extendedUnorientedGraph.putIfNotExists(

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import cz.vutbr.fit.interlockSim.objects.cells.InOut
1717
import cz.vutbr.fit.interlockSim.objects.cells.NodeCell
1818
import cz.vutbr.fit.interlockSim.objects.cells.RailSemaphore
1919
import cz.vutbr.fit.interlockSim.objects.cells.RailSwitch
20+
import cz.vutbr.fit.interlockSim.objects.cells.SUPPORTED_SIMPLE_SPATIAL_TYPES
2021
import java.awt.Dimension
2122
import java.awt.event.ActionEvent
2223
import javax.swing.AbstractAction
@@ -68,7 +69,7 @@ class ToolBar : JToolBar() {
6869
addSeparator()
6970

7071
// Add switch buttons for all supported spatial types
71-
val spatialTypes = RailSwitch.SUPPORTED_SIMPLE_SPATIAL_TYPES
72+
val spatialTypes = SUPPORTED_SIMPLE_SPATIAL_TYPES
7273
for (spatialType in spatialTypes) {
7374
for (switchType in RailSwitch.Type.values()) {
7475
addButton(RailSwitch::class.java, spatialType, switchType)

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,9 @@ abstract class AbstractCell : Cell {
3434
return iterator.next()
3535
}
3636

37-
companion object {
38-
@JvmStatic
39-
protected fun arr2set(segments: Array<Cell.Segment>): Set<Cell.Segment> {
40-
assert(segments.isNotEmpty())
41-
return EnumSet.of(segments[0], *segments) as Set<Cell.Segment>
42-
}
43-
}
37+
}
38+
39+
fun arr2set(segments: Array<Cell.Segment>): Set<Cell.Segment> {
40+
assert(segments.isNotEmpty())
41+
return EnumSet.of(segments[0], *segments) as Set<Cell.Segment>
4442
}

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

Lines changed: 80 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -87,92 +87,6 @@ interface Cell {
8787
return tr
8888
}
8989

90-
companion object {
91-
/**
92-
* conversion
93-
* @param d
94-
* @return converted number
95-
*/
96-
@JvmStatic
97-
fun d2r(d: Int): Float = (d + 1) * 0.5f
98-
99-
/**
100-
* conversion
101-
* @param r
102-
* @return converted number
103-
*/
104-
@JvmStatic
105-
fun r2d(r: Float): Int = (2 * r - 1).toInt()
106-
107-
/**
108-
* This is from d-coordinates conversion
109-
* @param dx
110-
* @param dy
111-
* @return segment
112-
*/
113-
@JvmStatic
114-
fun segmentFor(
115-
dx: Int,
116-
dy: Int
117-
): Segment? =
118-
if (dx < 0) {
119-
if (dy < 0) {
120-
B
121-
} else if (dy > 0) {
122-
D
123-
} else {
124-
A
125-
}
126-
} else if (dx == 0) {
127-
if (dy < 0) {
128-
C
129-
} else if (dy > 0) {
130-
H
131-
} else {
132-
null
133-
}
134-
} else if (dx > 0) {
135-
if (dy < 0) {
136-
E
137-
} else if (dy > 0) {
138-
G
139-
} else {
140-
F
141-
}
142-
} else {
143-
null
144-
}
145-
146-
/**
147-
* if segments can consist in regular cell
148-
* @param a
149-
* @param b
150-
* @return if segments is good pair
151-
*/
152-
@JvmStatic
153-
fun conflict(
154-
a: Segment?,
155-
b: Segment?
156-
): Boolean {
157-
if (a == b) return true
158-
if (a == null || b == null) return false
159-
val dx = a.dx - b.dx
160-
val dy = a.dy - b.dy
161-
if (Math.sqrt((dx * dx + dy * dy).toDouble()) <= 0.5) return true
162-
return false
163-
}
164-
165-
/**
166-
* @param segment
167-
* @return segment in reverse direction
168-
*/
169-
@JvmStatic
170-
fun anti(segment: Segment): Segment {
171-
val anti = segmentFor(-segment.dx, -segment.dy)
172-
assert(anti != null)
173-
return anti!!
174-
}
175-
}
17690
}
17791

17892
/**
@@ -214,3 +128,83 @@ interface Cell {
214128
*/
215129
fun joins(): Set<Segment>
216130
}
131+
132+
/**
133+
* conversion
134+
* @param d
135+
* @return converted number
136+
*/
137+
fun d2r(d: Int): Float = (d + 1) * 0.5f
138+
139+
/**
140+
* conversion
141+
* @param r
142+
* @return converted number
143+
*/
144+
fun r2d(r: Float): Int = (2 * r - 1).toInt()
145+
146+
/**
147+
* This is from d-coordinates conversion
148+
* @param dx
149+
* @param dy
150+
* @return segment
151+
*/
152+
fun segmentFor(
153+
dx: Int,
154+
dy: Int
155+
): Cell.Segment? =
156+
if (dx < 0) {
157+
if (dy < 0) {
158+
Cell.Segment.B
159+
} else if (dy > 0) {
160+
Cell.Segment.D
161+
} else {
162+
Cell.Segment.A
163+
}
164+
} else if (dx == 0) {
165+
if (dy < 0) {
166+
Cell.Segment.C
167+
} else if (dy > 0) {
168+
Cell.Segment.H
169+
} else {
170+
null
171+
}
172+
} else if (dx > 0) {
173+
if (dy < 0) {
174+
Cell.Segment.E
175+
} else if (dy > 0) {
176+
Cell.Segment.G
177+
} else {
178+
Cell.Segment.F
179+
}
180+
} else {
181+
null
182+
}
183+
184+
/**
185+
* if segments can consist in regular cell
186+
* @param a
187+
* @param b
188+
* @return if segments is good pair
189+
*/
190+
fun conflict(
191+
a: Cell.Segment?,
192+
b: Cell.Segment?
193+
): Boolean {
194+
if (a == b) return true
195+
if (a == null || b == null) return false
196+
val dx = a.dx - b.dx
197+
val dy = a.dy - b.dy
198+
if (Math.sqrt((dx * dx + dy * dy).toDouble()) <= 0.5) return true
199+
return false
200+
}
201+
202+
/**
203+
* @param segment
204+
* @return segment in reverse direction
205+
*/
206+
fun anti(segment: Cell.Segment): Cell.Segment {
207+
val anti = segmentFor(-segment.dx, -segment.dy)
208+
assert(anti != null)
209+
return anti!!
210+
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ class InOut(
3232
init {
3333
this.name = name
3434
this.inSemaphore = RailSemaphore(!orientation, spatialType)
35-
requireSimulation(inSemaphore.direction() == Cell.Segment.anti(direction())) {
35+
requireSimulation(inSemaphore.direction() == anti(direction())) {
3636
"In semaphore direction must be anti-parallel to InOut direction"
3737
}
38-
this.outSemaphore = RailSemaphore.getConstantInstance(orientation, spatialType, Signal.FREE)
38+
this.outSemaphore = getConstantInstance(orientation, spatialType, Signal.FREE)
3939
setName(name)
4040
}
4141

@@ -87,7 +87,7 @@ class InOut(
8787
allowedSpeed: Double
8888
) {
8989
val sem = getSemaphoreForWithExeption(from, to)
90-
sem.setSignal(Signal.forSpeed(allowedSpeed))
90+
sem.setSignal(forSpeed(allowedSpeed))
9191
}
9292

9393
override fun cancelPathSetup(

0 commit comments

Comments
 (0)