private static String getHeaderTemplate ( int [ ] widths , String [ ] headers ) { return IntStream . range ( 0 , widths . length ) . mapToObj ( i -> { final int width = widths [ i ] ; final int length = headers [ i ] . length ( ) ; final int leading = ( width - length ) / 2 ; final int trailing = width - ( length + leading ) ; final StringBuilder text = new StringBuilder ( ) ; whitespace ( text , leading + 1 ) ; text . append ( "%" ) . append ( i + 1 ) . append ( "$s" ) ; whitespace ( text , trailing ) ; text . append ( " |" ) ; return text . toString ( ) ; } ) . reduce ( ( left , right ) -> left + " " + right ) . orElse ( "" ) ; }
Returns the header template given the widths specified