public String splitSpecialStateSwitch ( String specialStateTransition ) { Matcher transformedSpecialStateMatcher = TRANSORMED_SPECIAL_STATE_TRANSITION_METHOD . matcher ( specialStateTransition ) ; if ( ! transformedSpecialStateMatcher . find ( ) ) { return specialStateTransition ; } String specialStateSwitch = transformedSpecialStateMatcher . group ( 3 ) ; Matcher transformedCaseMatcher = TRANSFORMED_CASE_PATTERN . matcher ( specialStateSwitch ) ; StringBuffer switchReplacementBuffer = new StringBuffer ( ) ; StringBuffer extractedMethods = new StringBuffer ( ) ; List < String > extractedCasesList = new ArrayList < String > ( ) ; switchReplacementBuffer . append ( "$2\n" ) ; boolean methodExtracted = false ; boolean isFirst = true ; String from = "0" ; String to = "0" ; //Process individual case statements while ( transformedCaseMatcher . find ( ) ) { if ( isFirst ) { isFirst = false ; from = transformedCaseMatcher . group ( 2 ) ; } to = transformedCaseMatcher . group ( 2 ) ; extractedCasesList . add ( transformedCaseMatcher . group ( ) ) ; //If the list of not processed extracted cases exceeds the maximal allowed number //generate individual method for those cases if ( extractedCasesList . size ( ) >= casesPerSpecialStateSwitch ) { generateExtractedSwitch ( extractedCasesList , from , to , extractedMethods , switchReplacementBuffer ) ; extractedCasesList . clear ( ) ; isFirst = true ; methodExtracted = true ; } } //If no method was extracted return the input unchanged //or process rest of cases by generating method for them if ( ! methodExtracted ) { return specialStateTransition ; } else if ( ! extractedCasesList . isEmpty ( ) && methodExtracted ) { generateExtractedSwitch ( extractedCasesList , from , to , extractedMethods , switchReplacementBuffer ) ; } switchReplacementBuffer . append ( "$5" ) ; StringBuffer result = new StringBuffer ( ) ; transformedSpecialStateMatcher . appendReplacement ( result , switchReplacementBuffer . toString ( ) ) ; result . append ( extractedMethods ) ; transformedSpecialStateMatcher . appendTail ( result ) ; return result . toString ( ) ; }
Splits switch in specialStateTransition containing more than maxCasesPerSwitch cases into several methods each containing maximum of maxCasesPerSwitch cases or less .