private static ShrinkWrapPath relativizeCommonRoot ( final ShrinkWrapPath thisOriginal , final Path thisCurrent , final Path otherOriginal , Path otherCurrent , final int backupCount ) { // Preconditions assert thisOriginal != null ; assert thisCurrent != null ; assert otherOriginal != null ; assert otherCurrent != null ; assert backupCount >= 0 ; // Do we yet have a common root? if ( ! otherCurrent . startsWith ( thisCurrent ) ) { // Back up until we do final Path otherParent = otherCurrent . getParent ( ) ; final ShrinkWrapPath thisParent = ( ShrinkWrapPath ) thisCurrent . getParent ( ) ; if ( otherParent != null && thisParent != null ) { return relativizeCommonRoot ( thisOriginal , thisParent , otherOriginal , otherParent , backupCount + 1 ) ; } else { throw new IllegalArgumentException ( "No common components" ) ; } } // Common root. Now relativize that. final List < String > thisTokens = tokenize ( thisOriginal ) ; final List < String > otherTokens = tokenize ( ( ShrinkWrapPath ) otherOriginal ) ; final int numOtherTokens = otherTokens . size ( ) ; final int numToTake = otherTokens . size ( ) - thisTokens . size ( ) ; final StringBuilder sb = new StringBuilder ( ) ; for ( int i = 0 ; i < backupCount ; i ++ ) { sb . append ( DIR_BACK ) ; sb . append ( ArchivePath . SEPARATOR ) ; } final int startCounter = numOtherTokens - numToTake - backupCount ; final int stopCounter = numOtherTokens - 1 ; if ( log . isLoggable ( Level . FINEST ) ) { log . finest ( "Backup: " + backupCount ) ; log . finest ( "This tokens: " + thisTokens ) ; log . finest ( "Other tokens: " + otherTokens ) ; log . finest ( "Differential: " + numToTake ) ; log . finest ( "Start: " + startCounter ) ; log . finest ( "Stop: " + stopCounter ) ; } for ( int i = startCounter ; i <= stopCounter ; i ++ ) { if ( i > startCounter ) { sb . append ( ArchivePath . SEPARATOR ) ; } sb . append ( otherTokens . get ( i ) ) ; } return new ShrinkWrapPath ( sb . toString ( ) , thisOriginal . fileSystem ) ; }
Relativizes the paths recursively