2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
5 // Copyright (C) 2008-2009 Christian M. Zmasek
6 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 // Contact: phylosoft @ gmail . com
24 // WWW: www.phylosoft.org/forester
26 package org.forester.util;
28 import java.awt.Color;
29 import java.io.BufferedReader;
30 import java.io.BufferedWriter;
32 import java.io.FileInputStream;
33 import java.io.FileNotFoundException;
34 import java.io.FileOutputStream;
35 import java.io.FileReader;
36 import java.io.FileWriter;
37 import java.io.IOException;
38 import java.io.InputStream;
39 import java.io.InputStreamReader;
40 import java.io.StringReader;
41 import java.io.Writer;
42 import java.math.BigDecimal;
44 import java.text.DateFormat;
45 import java.text.DecimalFormat;
46 import java.text.DecimalFormatSymbols;
47 import java.text.NumberFormat;
48 import java.text.ParseException;
49 import java.text.SimpleDateFormat;
50 import java.util.ArrayList;
51 import java.util.Collection;
52 import java.util.Date;
53 import java.util.Iterator;
54 import java.util.List;
56 import java.util.Map.Entry;
58 import java.util.SortedMap;
59 import java.util.SortedSet;
60 import java.util.TreeMap;
61 import java.util.TreeSet;
62 import java.util.regex.Pattern;
64 public final class ForesterUtil {
66 public final static String FILE_SEPARATOR = System.getProperty( "file.separator" );
67 public final static String LINE_SEPARATOR = System.getProperty( "line.separator" );
68 public final static String JAVA_VENDOR = System.getProperty( "java.vendor" );
69 public final static String JAVA_VERSION = System.getProperty( "java.version" );
70 public final static String OS_ARCH = System.getProperty( "os.arch" );
71 public final static String OS_NAME = System.getProperty( "os.name" );
72 public final static String OS_VERSION = System.getProperty( "os.version" );
73 public final static Pattern PARANTHESESABLE_NH_CHARS_PATTERN = Pattern.compile( "[(),;\\s]" );
74 public final static double ZERO_DIFF = 1.0E-9;
75 public static final BigDecimal NULL_BD = new BigDecimal( 0 );
76 public static final NumberFormat FORMATTER_9;
77 public static final NumberFormat FORMATTER_6;
78 public static final NumberFormat FORMATTER_06;
79 public static final NumberFormat FORMATTER_3;
81 final DecimalFormatSymbols dfs = new DecimalFormatSymbols();
82 dfs.setDecimalSeparator( '.' );
83 // dfs.setGroupingSeparator( ( char ) 0 );
84 FORMATTER_9 = new DecimalFormat( "#.#########", dfs );
85 FORMATTER_6 = new DecimalFormat( "#.######", dfs );
86 FORMATTER_06 = new DecimalFormat( "0.######", dfs );
87 FORMATTER_3 = new DecimalFormat( "#.###", dfs );
90 private ForesterUtil() {
93 final public static void appendSeparatorIfNotEmpty( final StringBuffer sb, final char separator ) {
94 if ( sb.length() > 0 ) {
95 sb.append( separator );
99 public static boolean seqIsLikelyToBeAa( final String s ) {
100 final String seq = s.toLowerCase();
101 if ( ( seq.indexOf( 'r' ) > -1 ) || ( seq.indexOf( 'd' ) > -1 ) || ( seq.indexOf( 'e' ) > -1 )
102 || ( seq.indexOf( 'q' ) > -1 ) || ( seq.indexOf( 'h' ) > -1 ) || ( seq.indexOf( 'k' ) > -1 )
103 || ( seq.indexOf( 'w' ) > -1 ) || ( seq.indexOf( 's' ) > -1 ) || ( seq.indexOf( 'm' ) > -1 )
104 || ( seq.indexOf( 'p' ) > -1 ) || ( seq.indexOf( 'v' ) > -1 ) ) {
111 * This calculates a color. If value is equal to min the returned color is
112 * minColor, if value is equal to max the returned color is maxColor,
113 * otherwise a color 'proportional' to value is returned.
127 final public static Color calcColor( double value,
130 final Color minColor,
131 final Color maxColor ) {
138 final double x = ForesterUtil.calculateColorFactor( value, max, min );
139 final int red = ForesterUtil.calculateColorComponent( minColor.getRed(), maxColor.getRed(), x );
140 final int green = ForesterUtil.calculateColorComponent( minColor.getGreen(), maxColor.getGreen(), x );
141 final int blue = ForesterUtil.calculateColorComponent( minColor.getBlue(), maxColor.getBlue(), x );
142 return new Color( red, green, blue );
146 * This calculates a color. If value is equal to min the returned color is
147 * minColor, if value is equal to max the returned color is maxColor, if
148 * value is equal to mean the returned color is meanColor, otherwise a color
149 * 'proportional' to value is returned -- either between min-mean or
159 * the mean/median value
168 final public static Color calcColor( double value,
172 final Color minColor,
173 final Color maxColor,
174 final Color meanColor ) {
181 if ( value < mean ) {
182 final double x = ForesterUtil.calculateColorFactor( value, mean, min );
183 final int red = ForesterUtil.calculateColorComponent( minColor.getRed(), meanColor.getRed(), x );
184 final int green = ForesterUtil.calculateColorComponent( minColor.getGreen(), meanColor.getGreen(), x );
185 final int blue = ForesterUtil.calculateColorComponent( minColor.getBlue(), meanColor.getBlue(), x );
186 return new Color( red, green, blue );
188 else if ( value > mean ) {
189 final double x = ForesterUtil.calculateColorFactor( value, max, mean );
190 final int red = ForesterUtil.calculateColorComponent( meanColor.getRed(), maxColor.getRed(), x );
191 final int green = ForesterUtil.calculateColorComponent( meanColor.getGreen(), maxColor.getGreen(), x );
192 final int blue = ForesterUtil.calculateColorComponent( meanColor.getBlue(), maxColor.getBlue(), x );
193 return new Color( red, green, blue );
201 * Helper method for calcColor methods.
203 * @param smallercolor_component_x
204 * color component the smaller color
205 * @param largercolor_component_x
206 * color component the larger color
209 * @return an int representing a color component
211 final private static int calculateColorComponent( final double smallercolor_component_x,
212 final double largercolor_component_x,
214 return ( int ) ( smallercolor_component_x + ( ( x * ( largercolor_component_x - smallercolor_component_x ) ) / 255.0 ) );
218 * Helper method for calcColor methods.
227 * @return a normalized value between larger and smaller
229 final private static double calculateColorFactor( final double value, final double larger, final double smaller ) {
230 return ( 255.0 * ( value - smaller ) ) / ( larger - smaller );
233 final public static String collapseWhiteSpace( final String s ) {
234 return s.replaceAll( "[\\s]+", " " );
237 final public static void collection2file( final File file, final Collection<?> data, final String separator )
239 final Writer writer = new BufferedWriter( new FileWriter( file ) );
240 collection2writer( writer, data, separator );
244 final public static void collection2writer( final Writer writer, final Collection<?> data, final String separator )
246 boolean first = true;
247 for( final Object object : data ) {
249 writer.write( separator );
254 writer.write( object.toString() );
258 final public static String colorToHex( final Color color ) {
259 final String rgb = Integer.toHexString( color.getRGB() );
260 return rgb.substring( 2, rgb.length() );
263 synchronized public static void copyFile( final File in, final File out ) throws IOException {
264 final FileInputStream in_s = new FileInputStream( in );
265 final FileOutputStream out_s = new FileOutputStream( out );
267 final byte[] buf = new byte[ 1024 ];
269 while ( ( i = in_s.read( buf ) ) != -1 ) {
270 out_s.write( buf, 0, i );
273 catch ( final IOException e ) {
277 if ( in_s != null ) {
280 if ( out_s != null ) {
286 final public static int countChars( final String str, final char c ) {
288 for( int i = 0; i < str.length(); ++i ) {
289 if ( str.charAt( i ) == c ) {
296 final public static BufferedWriter createBufferedWriter( final File file ) throws IOException {
297 if ( file.exists() ) {
298 throw new IOException( "[" + file + "] already exists" );
300 return new BufferedWriter( new FileWriter( file ) );
303 final public static BufferedWriter createBufferedWriter( final String name ) throws IOException {
304 return new BufferedWriter( new FileWriter( createFileForWriting( name ) ) );
307 final public static File createFileForWriting( final String name ) throws IOException {
308 final File file = new File( name );
309 if ( file.exists() ) {
310 throw new IOException( "[" + name + "] already exists" );
315 public static void fatalError( final String prg_name, final String message ) {
316 System.err.println();
317 System.err.println( "[" + prg_name + "] > " + message );
318 System.err.println();
322 public static String[] file2array( final File file ) throws IOException {
323 final List<String> list = file2list( file );
324 final String[] ary = new String[ list.size() ];
326 for( final String s : list ) {
332 final public static List<String> file2list( final File file ) throws IOException {
333 final List<String> list = new ArrayList<String>();
334 final BufferedReader in = new BufferedReader( new FileReader( file ) );
336 while ( ( str = in.readLine() ) != null ) {
338 if ( ( str.length() > 0 ) && !str.startsWith( "#" ) ) {
339 for( final String s : splitString( str ) ) {
348 final public static SortedSet<String> file2set( final File file ) throws IOException {
349 final SortedSet<String> set = new TreeSet<String>();
350 final BufferedReader in = new BufferedReader( new FileReader( file ) );
352 while ( ( str = in.readLine() ) != null ) {
354 if ( ( str.length() > 0 ) && !str.startsWith( "#" ) ) {
355 for( final String s : splitString( str ) ) {
364 final public static String getCurrentDateTime() {
365 final DateFormat format = new SimpleDateFormat( "yyyy/MM/dd HH:mm:ss" );
366 return format.format( new Date() );
369 final public static String getFileSeparator() {
370 return ForesterUtil.FILE_SEPARATOR;
373 final public static String getFirstLine( final Object source ) throws FileNotFoundException, IOException {
374 BufferedReader reader = null;
375 if ( source instanceof File ) {
376 final File f = ( File ) source;
378 throw new IOException( "[" + f.getAbsolutePath() + "] does not exist" );
380 else if ( !f.isFile() ) {
381 throw new IOException( "[" + f.getAbsolutePath() + "] is not a file" );
383 else if ( !f.canRead() ) {
384 throw new IOException( "[" + f.getAbsolutePath() + "] is not a readable" );
386 reader = new BufferedReader( new FileReader( f ) );
388 else if ( source instanceof InputStream ) {
389 reader = new BufferedReader( new InputStreamReader( ( InputStream ) source ) );
391 else if ( source instanceof String ) {
392 reader = new BufferedReader( new StringReader( ( String ) source ) );
394 else if ( source instanceof StringBuffer ) {
395 reader = new BufferedReader( new StringReader( source.toString() ) );
397 else if ( source instanceof URL ) {
398 reader = new BufferedReader( new InputStreamReader( ( ( URL ) source ).openStream() ) );
401 throw new IllegalArgumentException( "dont know how to read [" + source.getClass() + "]" );
404 while ( ( line = reader.readLine() ) != null ) {
406 if ( !ForesterUtil.isEmpty( line ) ) {
407 if ( reader != null ) {
413 if ( reader != null ) {
419 final public static String getLineSeparator() {
420 return ForesterUtil.LINE_SEPARATOR;
423 final public static void increaseCountingMap( final Map<String, Integer> counting_map, final String item_name ) {
424 if ( !counting_map.containsKey( item_name ) ) {
425 counting_map.put( item_name, 1 );
428 counting_map.put( item_name, counting_map.get( item_name ) + 1 );
432 final public static boolean isContainsParanthesesableNhCharacter( final String nh ) {
433 return PARANTHESESABLE_NH_CHARS_PATTERN.matcher( nh ).find();
436 final public static boolean isEmpty( final List<?> l ) {
437 if ( ( l == null ) || l.isEmpty() ) {
440 for( final Object o : l ) {
448 final public static boolean isEmpty( final Set<?> s ) {
449 if ( ( s == null ) || s.isEmpty() ) {
452 for( final Object o : s ) {
460 final public static boolean isEmpty( final String s ) {
461 return ( ( s == null ) || ( s.length() < 1 ) );
464 final public static boolean isEqual( final double a, final double b ) {
465 return ( ( Math.abs( a - b ) ) < ZERO_DIFF );
468 final public static boolean isEven( final int n ) {
473 * This determines whether String[] a and String[] b have at least one
474 * String in common (intersect). Returns false if at least one String[] is
478 * a String[] b a String[]
479 * @return true if both a and b or not empty or null and contain at least
480 * one element in common false otherwise
482 final public static boolean isIntersecting( final String[] a, final String[] b ) {
483 if ( ( a == null ) || ( b == null ) ) {
486 if ( ( a.length < 1 ) || ( b.length < 1 ) ) {
489 for( int i = 0; i < a.length; ++i ) {
490 final String ai = a[ i ];
491 for( int j = 0; j < b.length; ++j ) {
492 if ( ( ai != null ) && ( b[ j ] != null ) && ai.equals( b[ j ] ) ) {
500 final public static double isLargerOrEqualToZero( final double d ) {
509 final public static boolean isNull( final BigDecimal s ) {
510 return ( ( s == null ) || ( s.compareTo( NULL_BD ) == 0 ) );
513 final public static String isReadableFile( final File f ) {
515 return "file [" + f + "] does not exist";
517 if ( f.isDirectory() ) {
518 return "[" + f + "] is a directory";
521 return "[" + f + "] is not a file";
523 if ( !f.canRead() ) {
524 return "file [" + f + "] is not readable";
526 if ( f.length() < 1 ) {
527 return "file [" + f + "] is empty";
532 final public static String isReadableFile( final String s ) {
533 return isReadableFile( new File( s ) );
536 final public static String isWritableFile( final File f ) {
537 if ( f.isDirectory() ) {
538 return "[" + f + "] is a directory";
541 return "[" + f + "] already exists";
547 * Helper for method "stringToColor".
549 * (Last modified: 12/20/03)
551 final public static int limitRangeForColor( int i ) {
561 final public static SortedMap<Object, Integer> listToSortedCountsMap( final List list ) {
562 final SortedMap<Object, Integer> map = new TreeMap<Object, Integer>();
563 for( final Object key : list ) {
564 if ( !map.containsKey( key ) ) {
568 map.put( key, map.get( key ) + 1 );
574 final public static void map2file( final File file,
575 final Map<?, ?> data,
576 final String entry_separator,
577 final String data_separator ) throws IOException {
578 final Writer writer = new BufferedWriter( new FileWriter( file ) );
579 map2writer( writer, data, entry_separator, data_separator );
583 final public static void map2writer( final Writer writer,
584 final Map<?, ?> data,
585 final String entry_separator,
586 final String data_separator ) throws IOException {
587 boolean first = true;
588 for( final Entry<?, ?> entry : data.entrySet() ) {
590 writer.write( data_separator );
595 writer.write( entry.getKey().toString() );
596 writer.write( entry_separator );
597 writer.write( entry.getValue().toString() );
601 final public static StringBuffer mapToStringBuffer( final Map map, final String key_value_separator ) {
602 final StringBuffer sb = new StringBuffer();
603 for( final Iterator iter = map.keySet().iterator(); iter.hasNext(); ) {
604 final Object key = iter.next();
605 sb.append( key.toString() );
606 sb.append( key_value_separator );
607 sb.append( map.get( key ).toString() );
608 sb.append( ForesterUtil.getLineSeparator() );
613 final public static String normalizeString( final String s,
615 final boolean left_pad,
616 final char pad_char ) {
617 if ( s.length() > length ) {
618 return s.substring( 0, length );
621 final StringBuffer pad = new StringBuffer( length - s.length() );
622 for( int i = 0; i < ( length - s.length() ); ++i ) {
623 pad.append( pad_char );
634 final public static BufferedReader obtainReader( final Object source ) throws IOException, FileNotFoundException {
635 BufferedReader reader = null;
636 if ( source instanceof File ) {
637 final File f = ( File ) source;
639 throw new IOException( "\"" + f.getAbsolutePath() + "\" does not exist" );
641 else if ( !f.isFile() ) {
642 throw new IOException( "\"" + f.getAbsolutePath() + "\" is not a file" );
644 else if ( !f.canRead() ) {
645 throw new IOException( "\"" + f.getAbsolutePath() + "\" is not a readable" );
647 reader = new BufferedReader( new FileReader( f ) );
649 else if ( source instanceof InputStream ) {
650 reader = new BufferedReader( new InputStreamReader( ( InputStream ) source ) );
652 else if ( source instanceof String ) {
653 reader = new BufferedReader( new StringReader( ( String ) source ) );
655 else if ( source instanceof StringBuffer ) {
656 reader = new BufferedReader( new StringReader( source.toString() ) );
659 throw new IllegalArgumentException( "attempt to parse object of type [" + source.getClass()
660 + "] (can only parse objects of type File, InputStream, String, or StringBuffer)" );
665 final public static StringBuffer pad( final double number, final int size, final char pad, final boolean left_pad ) {
666 return pad( new StringBuffer( number + "" ), size, pad, left_pad );
669 final public static StringBuffer pad( final String string, final int size, final char pad, final boolean left_pad ) {
670 return pad( new StringBuffer( string ), size, pad, left_pad );
673 final public static StringBuffer pad( final StringBuffer string,
676 final boolean left_pad ) {
677 final StringBuffer padding = new StringBuffer();
678 final int s = size - string.length();
680 return new StringBuffer( string.substring( 0, size ) );
682 for( int i = 0; i < s; ++i ) {
683 padding.append( pad );
686 return padding.append( string );
689 return string.append( padding );
693 final public static double parseDouble( final String str ) throws ParseException {
694 if ( ForesterUtil.isEmpty( str ) ) {
697 return Double.parseDouble( str );
700 final public static int parseInt( final String str ) throws ParseException {
701 if ( ForesterUtil.isEmpty( str ) ) {
704 return Integer.parseInt( str );
707 final public static void printArray( final Object[] a ) {
708 for( int i = 0; i < a.length; ++i ) {
709 System.out.println( "[" + i + "]=" + a[ i ] );
713 final public static void printCountingMap( final Map<String, Integer> counting_map ) {
714 for( final String key : counting_map.keySet() ) {
715 System.out.println( key + ": " + counting_map.get( key ) );
719 final public static void printErrorMessage( final String prg_name, final String message ) {
720 System.out.println( "[" + prg_name + "] > error: " + message );
723 final public static void printProgramInformation( final String prg_name, final String prg_version, final String date ) {
724 final int l = prg_name.length() + prg_version.length() + date.length() + 4;
725 System.out.println();
726 System.out.println( prg_name + " " + prg_version + " (" + date + ")" );
727 for( int i = 0; i < l; ++i ) {
728 System.out.print( "_" );
730 System.out.println();
733 final public static void printProgramInformation( final String prg_name,
735 final String prg_version,
739 String my_prg_name = new String( prg_name );
740 if ( !ForesterUtil.isEmpty( desc ) ) {
741 my_prg_name += ( " - " + desc );
743 final int l = my_prg_name.length() + prg_version.length() + date.length() + 4;
744 System.out.println();
745 System.out.println( my_prg_name + " " + prg_version + " (" + date + ")" );
746 for( int i = 0; i < l; ++i ) {
747 System.out.print( "_" );
749 System.out.println();
750 System.out.println();
751 System.out.println( "WWW : " + www );
752 System.out.println( "Contact: " + email );
753 if ( !ForesterUtil.isEmpty( ForesterUtil.JAVA_VERSION ) && !ForesterUtil.isEmpty( ForesterUtil.JAVA_VENDOR ) ) {
754 System.out.println();
755 System.out.println( "[running on Java " + ForesterUtil.JAVA_VERSION + " " + ForesterUtil.JAVA_VENDOR + "]" );
757 System.out.println();
760 final public static void printProgramInformation( final String prg_name,
761 final String prg_version,
765 printProgramInformation( prg_name, null, prg_version, date, email, www );
768 final public static void printWarningMessage( final String prg_name, final String message ) {
769 System.out.println( "[" + prg_name + "] > warning: " + message );
772 final public static void programMessage( final String prg_name, final String message ) {
773 System.out.println( "[" + prg_name + "] > " + message );
776 final public static String removeSuffix( final String file_name ) {
777 final int i = file_name.lastIndexOf( '.' );
779 return file_name.substring( 0, i );
785 * Removes all white space from String s.
787 * @return String s with white space removed
789 final public static String removeWhiteSpace( String s ) {
791 for( i = 0; i <= s.length() - 1; i++ ) {
792 if ( ( s.charAt( i ) == ' ' ) || ( s.charAt( i ) == '\t' ) || ( s.charAt( i ) == '\n' )
793 || ( s.charAt( i ) == '\r' ) ) {
794 s = s.substring( 0, i ) + s.substring( i + 1 );
801 final public static String replaceIllegalNhCharacters( final String nh ) {
805 return nh.trim().replaceAll( "[\\[\\]:]+", "_" );
808 final public static String replaceIllegalNhxCharacters( final String nhx ) {
812 return nhx.trim().replaceAll( "[\\[\\](),:;\\s]+", "_" );
815 final public static double round( final double value, final int decimal_place ) {
816 BigDecimal bd = new BigDecimal( value );
817 bd = bd.setScale( decimal_place, BigDecimal.ROUND_HALF_UP );
818 return bd.doubleValue();
822 * Rounds d to an int.
824 final public static int roundToInt( final double d ) {
825 return ( int ) ( d + 0.5 );
828 final public static int roundToInt( final float f ) {
829 return ( int ) ( f + 0.5f );
832 final public static short roundToShort( final double d ) {
833 return ( short ) ( d + 0.5 );
836 final public static String sanitizeString( final String s ) {
845 final private static String[] splitString( final String str ) {
846 final String regex = "[\\s;,]+";
847 return str.split( regex );
850 final public static String stringArrayToString( final String[] a ) {
851 return stringArrayToString( a, ", " );
854 final public static String stringArrayToString( final String[] a, final String separator ) {
855 final StringBuilder sb = new StringBuilder();
856 if ( ( a != null ) && ( a.length > 0 ) ) {
857 for( int i = 0; i < a.length - 1; ++i ) {
858 sb.append( a[ i ] + separator );
860 sb.append( a[ a.length - 1 ] );
862 return sb.toString();
865 final public static String[] stringListToArray( final List<String> list ) {
866 if ( list != null ) {
867 final String[] str = new String[ list.size() ];
869 for( final String l : list ) {
877 final public static String stringListToString( final List<String> l, final String separator ) {
878 final StringBuilder sb = new StringBuilder();
879 if ( ( l != null ) && ( l.size() > 0 ) ) {
880 for( int i = 0; i < l.size() - 1; ++i ) {
881 sb.append( l.get( i ) + separator );
883 sb.append( l.get( l.size() - 1 ) );
885 return sb.toString();
888 final public static String[] stringSetToArray( final Set<String> strings ) {
889 final String[] str_array = new String[ strings.size() ];
891 for( final String e : strings ) {
892 str_array[ i++ ] = e;
897 final public static void unexpectedFatalError( final String prg_name, final Exception e ) {
898 System.err.println();
899 System.err.println( "[" + prg_name
900 + "] > unexpected error (Should not have occured! Please contact program author(s).)" );
901 e.printStackTrace( System.err );
902 System.err.println();
906 final public static void unexpectedFatalError( final String prg_name, final String message ) {
907 System.err.println();
908 System.err.println( "[" + prg_name
909 + "] > unexpected error. Should not have occured! Please contact program author(s)." );
910 System.err.println( message );
911 System.err.println();
915 final public static void unexpectedFatalError( final String prg_name, final String message, final Exception e ) {
916 System.err.println();
917 System.err.println( "[" + prg_name
918 + "] > unexpected error. Should not have occured! Please contact program author(s)." );
919 System.err.println( message );
920 e.printStackTrace( System.err );
921 System.err.println();
925 public final static String wordWrap( final String str, final int width ) {
926 final StringBuilder sb = new StringBuilder( str );
930 while ( i < sb.length() ) {
931 if ( sb.charAt( i ) == ' ' ) {
934 if ( sb.charAt( i ) == '\n' ) {
938 if ( i > start + width - 1 ) {
940 sb.setCharAt( ls, '\n' );
945 sb.insert( i, '\n' );
951 return sb.toString();