@@ -28,6 +28,12 @@ import cz.vutbr.fit.interlockSim.sim.InOutWorker
2828import cz.vutbr.fit.interlockSim.sim.LoopProcess
2929import cz.vutbr.fit.interlockSim.sim.ShuntingLoop
3030import cz.vutbr.fit.interlockSim.exceptions.SimulationException
31+ import cz.vutbr.fit.interlockSim.exceptions.requireEditor
32+ import cz.vutbr.fit.interlockSim.exceptions.requireEditorNotNull
33+ import cz.vutbr.fit.interlockSim.exceptions.requireSimulation
34+ import cz.vutbr.fit.interlockSim.exceptions.requireSimulationNotNull
35+ import cz.vutbr.fit.interlockSim.exceptions.requireValidArgument
36+ import cz.vutbr.fit.interlockSim.exceptions.requireValidState
3137import cz.vutbr.fit.interlockSim.objects.cells.anti
3238import cz.vutbr.fit.interlockSim.objects.cells.conflict
3339import cz.vutbr.fit.interlockSim.objects.cells.segmentFor
@@ -211,11 +217,11 @@ abstract class DefaultContext :
211217 val nodecell2: NodeCell = Util .assertNodeCell(getGrid().get(key2)!! )
212218
213219 for (s1: Segment in nodecell1.joins()) {
214- assert (s1 != null ) { nodecell1 }
220+ requireEditorNotNull (s1) { " Segment s1 cannot be null for nodecell $ nodecell1" }
215221 val p1 = s1.transform(key1)
216222 if (used(p1)) continue
217223 for (s2: Segment in nodecell2.joins()) {
218- assert (s2 != null ) { nodecell2 }
224+ requireEditorNotNull (s2) { " Segment s2 cannot be null for nodecell $ nodecell2" }
219225 if (s1 == s2) continue // stejne segmenty se nepropoji
220226 val p2 = s2.transform(key2)
221227 if (used(p2)) continue
@@ -250,7 +256,7 @@ abstract class DefaultContext :
250256 key2 : Point ,
251257 trackBlock : TrackBlock
252258 ): Boolean {
253- assert (key1 != null && key2 != null )
259+ requireEditor (key1 != null && key2 != null ) { " Keys cannot be null in hardJoin " }
254260 if (key1.distance(key2) > SQRT2 ) {
255261 val blockEndFrom = s1.transform(key1)
256262 val blockEndTo = s2.transform(key2)
@@ -309,7 +315,9 @@ abstract class DefaultContext :
309315 @Suppress(" UNCHECKED_CAST" )
310316 val mapKeys: Set <Point > = mapToAdd.keySet() as Set <Point >
311317 linesKeys[trackBlock] = mapKeys
312- assert (! extendedUnorientedGraph.contains(key1, key2))
318+ requireValidState(! extendedUnorientedGraph.contains(key1, key2)) {
319+ " Graph already contains edge between ($key1 , $key2 )"
320+ }
313321 extendedUnorientedGraph.put(key1, s1, key2, s2, trackBlock)
314322 return mapToAdd
315323 }
@@ -325,7 +333,9 @@ abstract class DefaultContext :
325333 key2 : Point ,
326334 trackBlock : TrackBlock
327335 ): MutableMap <Point , TrackBlockPart >? {
328- assert (bresenham != null && bresenham.isNotEmpty())
336+ requireValidArgument(bresenham != null && bresenham.isNotEmpty()) {
337+ " Bresenham path list cannot be null or empty"
338+ }
329339 val map: MutableMap <Point , TrackBlockPart > = LinkedHashMap ()
330340 var from = key1
331341 var middle = bresenham[0 ]
@@ -358,7 +368,7 @@ abstract class DefaultContext :
358368 to : Point ,
359369 block : TrackBlock
360370 ): TrackBlockPart ? {
361- assert (block != null )
371+ requireEditorNotNull (block) { " TrackBlock cannot be null in createPart " }
362372 if (used(middle)) return null
363373 if (from == to || from == middle || middle == to) return null
364374
@@ -390,8 +400,12 @@ abstract class DefaultContext :
390400 p2 : Point ,
391401 points : MutableList <Point >
392402 ): Boolean {
393- assert (key1 != null && key2 != null && p1 != null && p2 != null )
394- assert (key1 != p1 && key2 != p2 && key1 != p2 && key2 != p1)
403+ requireValidArgument(key1 != null && key2 != null && p1 != null && p2 != null ) {
404+ " All points must be non-null in bresenham algorithm"
405+ }
406+ requireValidArgument(key1 != p1 && key2 != p2 && key1 != p2 && key2 != p1) {
407+ " Keys and intermediate points must be distinct in bresenham algorithm"
408+ }
395409
396410 // Make mutable copies since we need to modify them for the algorithm
397411 var p1Mut = p1
@@ -508,7 +522,7 @@ abstract class DefaultContext :
508522 key : Point ,
509523 nodeCell : NodeCell
510524 ) {
511- assert (key != null )
525+ requireEditorNotNull (key) { " Cell key cannot be null in putCell " }
512526 // Validate coordinates are within grid bounds
513527 if (key.x < 0 || key.y < 0 || key.x >= railwayNetGrid.getCols() || key.y >= railwayNetGrid.getRows()) {
514528 throw ContextCreationException (
@@ -520,7 +534,7 @@ abstract class DefaultContext :
520534
521535 // vedlejsi Nody (sousedni bunky)
522536 for (s1: Segment in nodeCell.joins()) {
523- assert (s1 != null ) { nodeCell }
537+ requireEditorNotNull (s1) { " Segment s1 cannot be null for nodeCell $nodeCell " }
524538 val p = s1.transform(key)
525539 // Skip neighbor if it's outside grid bounds (boundary cells)
526540 if (p.x < 0 || p.y < 0 || p.x >= railwayNetGrid.getCols() || p.y >= railwayNetGrid.getRows()) {
@@ -533,7 +547,9 @@ abstract class DefaultContext :
533547 // vzit proti-segment
534548 val s2 = anti(s1)
535549 if (nodeCell2.joins().contains(s2)) {
536- assert (s2.transform(p) == key)
550+ requireValidState(s2.transform(p) == key) {
551+ " Segment transformation inconsistency: s2.transform($p ) != $key "
552+ }
537553 extendedUnorientedGraph.putIfNotExists(
538554 key,
539555 s1,
@@ -577,7 +593,7 @@ abstract class DefaultContext :
577593 * Remove a track line from the railway network
578594 */
579595 override fun removeLine (line : TrackBlock ) {
580- assert (line != null )
596+ requireEditorNotNull (line) { " TrackBlock line cannot be null in removeLine " }
581597 extendedUnorientedGraph.remove(line)
582598 getGrid().keySet().removeAll(linesKeys.remove(line) ? : emptySet())
583599 changeSupport.firePropertyChange(
@@ -614,9 +630,11 @@ abstract class DefaultContext :
614630 ): Segment ? {
615631 // If track is not null, use it; otherwise use secondEndTrack
616632 if (track != null ) return getSegment(separator, track)
617- assert (separator != null )
618- assert (secondEndTrack != null ) { separator }
619- assert (separator is OrientedPathSeparator ) // Util
633+ requireValidArgument(separator != null ) { " PathSeparator cannot be null" }
634+ requireValidArgument(secondEndTrack != null ) { " secondEndTrack cannot be null for separator $separator " }
635+ requireValidArgument(separator is OrientedPathSeparator ) {
636+ " PathSeparator must be OrientedPathSeparator, got ${separator.javaClass.simpleName} "
637+ }
620638 val segment = getSegment(separator, secondEndTrack!! )
621639 // Match Java 1:1: return null when segment doesn't exist
622640 return separator.getFollowingSegment(segment)
@@ -682,7 +700,9 @@ abstract class DefaultContext :
682700 current : TrackBlock ?
683701 ): Segment ? {
684702 if (current != null ) {
685- assert (getGraph().get(location).contains(current)) { current }
703+ requireValidState(getGraph().get(location).contains(current)) {
704+ " Current track block $current not found in graph at location $location "
705+ }
686706 }
687707 return if (current == null ) null else getGraph().extensionalObject(location, current)
688708 }
@@ -692,7 +712,7 @@ abstract class DefaultContext :
692712 */
693713 private fun getLocation (node : NodeCell ): Point {
694714 val location = getRailWayNetGrid().getLocation(node)
695- assert (location != null ) { this }
715+ requireValidState (location != null ) { " Location not found for nodeCell $node in grid " }
696716 return location!!
697717 }
698718
@@ -724,7 +744,7 @@ abstract class DefaultContext :
724744 var trackBlock: TrackBlock ? = null
725745 if (current != null ) {
726746 trackBlock = current.getTrackBlock()
727- assert (trackBlock != null )
747+ requireValidState (trackBlock != null ) { " TrackBlock cannot be null for current track section " }
728748 val nextTrackSection = trackBlock?.getNextTrackSection(separator, current)
729749 if (nextTrackSection != null ) {
730750 if (logger.isTraceEnabled()) {
@@ -785,7 +805,7 @@ abstract class DefaultContext :
785805 * Stop the simulation
786806 */
787807 override fun stop () {
788- assert (mainProcess != null )
808+ requireSimulationNotNull (mainProcess) { " Main process must be initialized before stopping simulation " }
789809 for (worker in workers.values) {
790810 worker.terminate()
791811 }
@@ -819,12 +839,12 @@ abstract class DefaultContext :
819839 next : Track ? ,
820840 previous : Track ?
821841 ): Boolean {
822- assert (separator != null )
842+ requireSimulation (separator != null ) { " Separator cannot be null in isSeparatorInDirection " }
823843 val segment = getSegment(separator, next, previous)
824844 if (segment == null && separator is InOut ) return true
825- assert (segment != null ) { separator }
845+ requireSimulation (segment != null ) { " Segment cannot be null for separator $separator " }
826846 val direction = separator.direction()
827- assert (direction != null )
847+ requireSimulation (direction != null ) { " Direction cannot be null for oriented separator " }
828848 val inDirection = segment == = direction
829849 if (logger.isDebugEnabled()) {
830850 logger.debug(
0 commit comments