@ SuppressWarnings ( { "checkstyle:cyclomaticcomplexity" , "checkstyle:npathcomplexity" , "checkstyle:nestedifdepth" } ) public RoadPath addAndGetPath ( RoadPath end ) { RoadPath selectedPath = null ; if ( end != null && ! end . isEmpty ( ) ) { RoadConnection first = end . getFirstPoint ( ) ; RoadConnection last = end . getLastPoint ( ) ; assert first != null ; assert last != null ; first = first . getWrappedRoadConnection ( ) ; last = last . getWrappedRoadConnection ( ) ; assert first != null ; assert last != null ; if ( this . paths . isEmpty ( ) ) { this . paths . add ( end ) ; selectedPath = end ; } else { RoadPath connectToFirst = null ; RoadPath connectToLast = null ; final Iterator < RoadPath > pathIterator = this . paths . iterator ( ) ; RoadPath path ; while ( ( connectToFirst == null || connectToLast == null ) && pathIterator . hasNext ( ) ) { path = pathIterator . next ( ) ; if ( connectToFirst == null ) { if ( first . equals ( path . getFirstPoint ( ) ) ) { connectToFirst = path ; } else if ( first . equals ( path . getLastPoint ( ) ) ) { connectToFirst = path ; } } if ( connectToLast == null ) { if ( last . equals ( path . getFirstPoint ( ) ) ) { connectToLast = path ; } else if ( last . equals ( path . getLastPoint ( ) ) ) { connectToLast = path ; } } } if ( connectToFirst != null && connectToLast != null && ! connectToLast . equals ( connectToFirst ) ) { // a) Select the biggest path as reference // b) Remove the nonreference path that is connected to the new path // c) Add the components of the new path into the reference path // d) Reinject the components of the nonreference path. // -- // a) final RoadPath reference ; final RoadPath nonreference ; if ( connectToFirst . size ( ) > connectToLast . size ( ) ) { reference = connectToFirst ; nonreference = connectToLast ; } else { reference = connectToLast ; nonreference = connectToFirst ; } // b) if ( this . paths . remove ( nonreference ) ) { // c) if ( ! RoadPath . addPathToPath ( reference , end ) ) { // Reinject the remove elements. this . paths . add ( nonreference ) ; } else { // d) if ( ! RoadPath . addPathToPath ( reference , nonreference ) ) { // Reinject the remove elements. this . paths . add ( nonreference ) ; } else { selectedPath = reference ; } } } } else if ( connectToFirst != null ) { if ( RoadPath . addPathToPath ( connectToFirst , end ) ) { selectedPath = connectToFirst ; } } else if ( connectToLast != null ) { if ( RoadPath . addPathToPath ( connectToLast , end ) ) { selectedPath = connectToLast ; } } else if ( this . paths . add ( end ) ) { selectedPath = end ; } } if ( selectedPath != null ) { this . segmentCount += end . size ( ) ; } } return selectedPath ; }
Add the given road path into this cluster of road paths .