private int bestSurroundingSet ( int index , int length , int ... valid ) { int option1 = set [ index - 1 ] ; if ( index + 1 < length ) { // we have two options to check int option2 = set [ index + 1 ] ; if ( contains ( valid , option1 ) && contains ( valid , option2 ) ) { return Math . min ( option1 , option2 ) ; } else if ( contains ( valid , option1 ) ) { return option1 ; } else if ( contains ( valid , option2 ) ) { return option2 ; } else { return valid [ 0 ] ; } } else { // we only have one option to check if ( contains ( valid , option1 ) ) { return option1 ; } else { return valid [ 0 ] ; } } }
Guesses the best set to use at the specified index by looking at the surrounding sets . In general characters in lower - numbered sets are more common so we choose them if we can . If no good surrounding sets can be found the default value returned is the first value from the valid set .