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 final public static String getForesterLibraryInformation() {
100 return "forester " + ForesterConstants.FORESTER_VERSION + " (" + ForesterConstants.FORESTER_DATE + ")";
103 public static boolean seqIsLikelyToBeAa( final String s ) {
104 final String seq = s.toLowerCase();
105 if ( ( seq.indexOf( 'r' ) > -1 ) || ( seq.indexOf( 'd' ) > -1 ) || ( seq.indexOf( 'e' ) > -1 )
106 || ( seq.indexOf( 'q' ) > -1 ) || ( seq.indexOf( 'h' ) > -1 ) || ( seq.indexOf( 'k' ) > -1 )
107 || ( seq.indexOf( 'w' ) > -1 ) || ( seq.indexOf( 's' ) > -1 ) || ( seq.indexOf( 'm' ) > -1 )
108 || ( seq.indexOf( 'p' ) > -1 ) || ( seq.indexOf( 'v' ) > -1 ) ) {
115 * This calculates a color. If value is equal to min the returned color is
116 * minColor, if value is equal to max the returned color is maxColor,
117 * otherwise a color 'proportional' to value is returned.
131 final public static Color calcColor( double value,
134 final Color minColor,
135 final Color maxColor ) {
142 final double x = ForesterUtil.calculateColorFactor( value, max, min );
143 final int red = ForesterUtil.calculateColorComponent( minColor.getRed(), maxColor.getRed(), x );
144 final int green = ForesterUtil.calculateColorComponent( minColor.getGreen(), maxColor.getGreen(), x );
145 final int blue = ForesterUtil.calculateColorComponent( minColor.getBlue(), maxColor.getBlue(), x );
146 return new Color( red, green, blue );
150 * This calculates a color. If value is equal to min the returned color is
151 * minColor, if value is equal to max the returned color is maxColor, if
152 * value is equal to mean the returned color is meanColor, otherwise a color
153 * 'proportional' to value is returned -- either between min-mean or
163 * the mean/median value
172 final public static Color calcColor( double value,
176 final Color minColor,
177 final Color maxColor,
178 final Color meanColor ) {
185 if ( value < mean ) {
186 final double x = ForesterUtil.calculateColorFactor( value, mean, min );
187 final int red = ForesterUtil.calculateColorComponent( minColor.getRed(), meanColor.getRed(), x );
188 final int green = ForesterUtil.calculateColorComponent( minColor.getGreen(), meanColor.getGreen(), x );
189 final int blue = ForesterUtil.calculateColorComponent( minColor.getBlue(), meanColor.getBlue(), x );
190 return new Color( red, green, blue );
192 else if ( value > mean ) {
193 final double x = ForesterUtil.calculateColorFactor( value, max, mean );
194 final int red = ForesterUtil.calculateColorComponent( meanColor.getRed(), maxColor.getRed(), x );
195 final int green = ForesterUtil.calculateColorComponent( meanColor.getGreen(), maxColor.getGreen(), x );
196 final int blue = ForesterUtil.calculateColorComponent( meanColor.getBlue(), maxColor.getBlue(), x );
197 return new Color( red, green, blue );
205 * Helper method for calcColor methods.
207 * @param smallercolor_component_x
208 * color component the smaller color
209 * @param largercolor_component_x
210 * color component the larger color
213 * @return an int representing a color component
215 final private static int calculateColorComponent( final double smallercolor_component_x,
216 final double largercolor_component_x,
218 return ( int ) ( smallercolor_component_x + ( ( x * ( largercolor_component_x - smallercolor_component_x ) ) / 255.0 ) );
222 * Helper method for calcColor methods.
231 * @return a normalized value between larger and smaller
233 final private static double calculateColorFactor( final double value, final double larger, final double smaller ) {
234 return ( 255.0 * ( value - smaller ) ) / ( larger - smaller );
237 final public static String collapseWhiteSpace( final String s ) {
238 return s.replaceAll( "[\\s]+", " " );
241 final public static void collection2file( final File file, final Collection<?> data, final String separator )
243 final Writer writer = new BufferedWriter( new FileWriter( file ) );
244 collection2writer( writer, data, separator );
248 final public static void collection2writer( final Writer writer, final Collection<?> data, final String separator )
250 boolean first = true;
251 for( final Object object : data ) {
253 writer.write( separator );
258 writer.write( object.toString() );
262 final public static String colorToHex( final Color color ) {
263 final String rgb = Integer.toHexString( color.getRGB() );
264 return rgb.substring( 2, rgb.length() );
267 synchronized public static void copyFile( final File in, final File out ) throws IOException {
268 final FileInputStream in_s = new FileInputStream( in );
269 final FileOutputStream out_s = new FileOutputStream( out );
271 final byte[] buf = new byte[ 1024 ];
273 while ( ( i = in_s.read( buf ) ) != -1 ) {
274 out_s.write( buf, 0, i );
277 catch ( final IOException e ) {
281 if ( in_s != null ) {
284 if ( out_s != null ) {
290 final public static int countChars( final String str, final char c ) {
292 for( int i = 0; i < str.length(); ++i ) {
293 if ( str.charAt( i ) == c ) {
300 final public static BufferedWriter createBufferedWriter( final File file ) throws IOException {
301 if ( file.exists() ) {
302 throw new IOException( "[" + file + "] already exists" );
304 return new BufferedWriter( new FileWriter( file ) );
307 final public static EasyWriter createEasyWriter( final File file ) throws IOException {
308 return new EasyWriter( createBufferedWriter( file ) );
311 final public static BufferedWriter createEasyWriter( final String name ) throws IOException {
312 return createEasyWriter( createFileForWriting( name ) );
315 final public static BufferedWriter createBufferedWriter( final String name ) throws IOException {
316 return new BufferedWriter( new FileWriter( createFileForWriting( name ) ) );
319 final public static File createFileForWriting( final String name ) throws IOException {
320 final File file = new File( name );
321 if ( file.exists() ) {
322 throw new IOException( "[" + name + "] already exists" );
327 public static void fatalError( final String prg_name, final String message ) {
328 System.err.println();
329 System.err.println( "[" + prg_name + "] > " + message );
330 System.err.println();
334 public static String[] file2array( final File file ) throws IOException {
335 final List<String> list = file2list( file );
336 final String[] ary = new String[ list.size() ];
338 for( final String s : list ) {
344 final public static List<String> file2list( final File file ) throws IOException {
345 final List<String> list = new ArrayList<String>();
346 final BufferedReader in = new BufferedReader( new FileReader( file ) );
348 while ( ( str = in.readLine() ) != null ) {
350 if ( ( str.length() > 0 ) && !str.startsWith( "#" ) ) {
351 for( final String s : splitString( str ) ) {
360 final public static SortedSet<String> file2set( final File file ) throws IOException {
361 final SortedSet<String> set = new TreeSet<String>();
362 final BufferedReader in = new BufferedReader( new FileReader( file ) );
364 while ( ( str = in.readLine() ) != null ) {
366 if ( ( str.length() > 0 ) && !str.startsWith( "#" ) ) {
367 for( final String s : splitString( str ) ) {
376 final public static String getCurrentDateTime() {
377 final DateFormat format = new SimpleDateFormat( "yyyy/MM/dd HH:mm:ss" );
378 return format.format( new Date() );
381 final public static String getFileSeparator() {
382 return ForesterUtil.FILE_SEPARATOR;
385 final public static String getFirstLine( final Object source ) throws FileNotFoundException, IOException {
386 BufferedReader reader = null;
387 if ( source instanceof File ) {
388 final File f = ( File ) source;
390 throw new IOException( "[" + f.getAbsolutePath() + "] does not exist" );
392 else if ( !f.isFile() ) {
393 throw new IOException( "[" + f.getAbsolutePath() + "] is not a file" );
395 else if ( !f.canRead() ) {
396 throw new IOException( "[" + f.getAbsolutePath() + "] is not a readable" );
398 reader = new BufferedReader( new FileReader( f ) );
400 else if ( source instanceof InputStream ) {
401 reader = new BufferedReader( new InputStreamReader( ( InputStream ) source ) );
403 else if ( source instanceof String ) {
404 reader = new BufferedReader( new StringReader( ( String ) source ) );
406 else if ( source instanceof StringBuffer ) {
407 reader = new BufferedReader( new StringReader( source.toString() ) );
409 else if ( source instanceof URL ) {
410 reader = new BufferedReader( new InputStreamReader( ( ( URL ) source ).openStream() ) );
413 throw new IllegalArgumentException( "dont know how to read [" + source.getClass() + "]" );
416 while ( ( line = reader.readLine() ) != null ) {
418 if ( !ForesterUtil.isEmpty( line ) ) {
419 if ( reader != null ) {
425 if ( reader != null ) {
431 final public static String getLineSeparator() {
432 return ForesterUtil.LINE_SEPARATOR;
435 final public static void increaseCountingMap( final Map<String, Integer> counting_map, final String item_name ) {
436 if ( !counting_map.containsKey( item_name ) ) {
437 counting_map.put( item_name, 1 );
440 counting_map.put( item_name, counting_map.get( item_name ) + 1 );
444 final public static boolean isContainsParanthesesableNhCharacter( final String nh ) {
445 return PARANTHESESABLE_NH_CHARS_PATTERN.matcher( nh ).find();
448 final public static boolean isEmpty( final List<?> l ) {
449 if ( ( l == null ) || l.isEmpty() ) {
452 for( final Object o : l ) {
460 final public static boolean isEmpty( final Set<?> s ) {
461 if ( ( s == null ) || s.isEmpty() ) {
464 for( final Object o : s ) {
472 final public static boolean isEmpty( final String s ) {
473 return ( ( s == null ) || ( s.length() < 1 ) );
476 final public static boolean isEqual( final double a, final double b ) {
477 return ( ( Math.abs( a - b ) ) < ZERO_DIFF );
480 final public static boolean isEven( final int n ) {
485 * This determines whether String[] a and String[] b have at least one
486 * String in common (intersect). Returns false if at least one String[] is
490 * a String[] b a String[]
491 * @return true if both a and b or not empty or null and contain at least
492 * one element in common false otherwise
494 final public static boolean isIntersecting( final String[] a, final String[] b ) {
495 if ( ( a == null ) || ( b == null ) ) {
498 if ( ( a.length < 1 ) || ( b.length < 1 ) ) {
501 for( int i = 0; i < a.length; ++i ) {
502 final String ai = a[ i ];
503 for( int j = 0; j < b.length; ++j ) {
504 if ( ( ai != null ) && ( b[ j ] != null ) && ai.equals( b[ j ] ) ) {
512 final public static double isLargerOrEqualToZero( final double d ) {
521 final public static boolean isNull( final BigDecimal s ) {
522 return ( ( s == null ) || ( s.compareTo( NULL_BD ) == 0 ) );
525 final public static String isReadableFile( final File f ) {
527 return "file [" + f + "] does not exist";
529 if ( f.isDirectory() ) {
530 return "[" + f + "] is a directory";
533 return "[" + f + "] is not a file";
535 if ( !f.canRead() ) {
536 return "file [" + f + "] is not readable";
538 if ( f.length() < 1 ) {
539 return "file [" + f + "] is empty";
544 final public static String isReadableFile( final String s ) {
545 return isReadableFile( new File( s ) );
548 final public static String isWritableFile( final File f ) {
549 if ( f.isDirectory() ) {
550 return "[" + f + "] is a directory";
553 return "[" + f + "] already exists";
559 * Helper for method "stringToColor".
561 * (Last modified: 12/20/03)
563 final public static int limitRangeForColor( int i ) {
573 final public static SortedMap<Object, Integer> listToSortedCountsMap( final List list ) {
574 final SortedMap<Object, Integer> map = new TreeMap<Object, Integer>();
575 for( final Object key : list ) {
576 if ( !map.containsKey( key ) ) {
580 map.put( key, map.get( key ) + 1 );
586 final public static void map2file( final File file,
587 final Map<?, ?> data,
588 final String entry_separator,
589 final String data_separator ) throws IOException {
590 final Writer writer = new BufferedWriter( new FileWriter( file ) );
591 map2writer( writer, data, entry_separator, data_separator );
595 final public static void map2writer( final Writer writer,
596 final Map<?, ?> data,
597 final String entry_separator,
598 final String data_separator ) throws IOException {
599 boolean first = true;
600 for( final Entry<?, ?> entry : data.entrySet() ) {
602 writer.write( data_separator );
607 writer.write( entry.getKey().toString() );
608 writer.write( entry_separator );
609 writer.write( entry.getValue().toString() );
613 final public static StringBuffer mapToStringBuffer( final Map map, final String key_value_separator ) {
614 final StringBuffer sb = new StringBuffer();
615 for( final Iterator iter = map.keySet().iterator(); iter.hasNext(); ) {
616 final Object key = iter.next();
617 sb.append( key.toString() );
618 sb.append( key_value_separator );
619 sb.append( map.get( key ).toString() );
620 sb.append( ForesterUtil.getLineSeparator() );
625 final public static String normalizeString( final String s,
627 final boolean left_pad,
628 final char pad_char ) {
629 if ( s.length() > length ) {
630 return s.substring( 0, length );
633 final StringBuffer pad = new StringBuffer( length - s.length() );
634 for( int i = 0; i < ( length - s.length() ); ++i ) {
635 pad.append( pad_char );
646 final public static BufferedReader obtainReader( final Object source ) throws IOException, FileNotFoundException {
647 BufferedReader reader = null;
648 if ( source instanceof File ) {
649 final File f = ( File ) source;
651 throw new IOException( "\"" + f.getAbsolutePath() + "\" does not exist" );
653 else if ( !f.isFile() ) {
654 throw new IOException( "\"" + f.getAbsolutePath() + "\" is not a file" );
656 else if ( !f.canRead() ) {
657 throw new IOException( "\"" + f.getAbsolutePath() + "\" is not a readable" );
659 reader = new BufferedReader( new FileReader( f ) );
661 else if ( source instanceof InputStream ) {
662 reader = new BufferedReader( new InputStreamReader( ( InputStream ) source ) );
664 else if ( source instanceof String ) {
665 reader = new BufferedReader( new StringReader( ( String ) source ) );
667 else if ( source instanceof StringBuffer ) {
668 reader = new BufferedReader( new StringReader( source.toString() ) );
671 throw new IllegalArgumentException( "attempt to parse object of type [" + source.getClass()
672 + "] (can only parse objects of type File, InputStream, String, or StringBuffer)" );
677 final public static StringBuffer pad( final double number, final int size, final char pad, final boolean left_pad ) {
678 return pad( new StringBuffer( number + "" ), size, pad, left_pad );
681 final public static StringBuffer pad( final String string, final int size, final char pad, final boolean left_pad ) {
682 return pad( new StringBuffer( string ), size, pad, left_pad );
685 final public static StringBuffer pad( final StringBuffer string,
688 final boolean left_pad ) {
689 final StringBuffer padding = new StringBuffer();
690 final int s = size - string.length();
692 return new StringBuffer( string.substring( 0, size ) );
694 for( int i = 0; i < s; ++i ) {
695 padding.append( pad );
698 return padding.append( string );
701 return string.append( padding );
705 final public static double parseDouble( final String str ) throws ParseException {
706 if ( ForesterUtil.isEmpty( str ) ) {
709 return Double.parseDouble( str );
712 final public static int parseInt( final String str ) throws ParseException {
713 if ( ForesterUtil.isEmpty( str ) ) {
716 return Integer.parseInt( str );
719 final public static void printArray( final Object[] a ) {
720 for( int i = 0; i < a.length; ++i ) {
721 System.out.println( "[" + i + "]=" + a[ i ] );
725 final public static void printCountingMap( final Map<String, Integer> counting_map ) {
726 for( final String key : counting_map.keySet() ) {
727 System.out.println( key + ": " + counting_map.get( key ) );
731 final public static void printErrorMessage( final String prg_name, final String message ) {
732 System.out.println( "[" + prg_name + "] > error: " + message );
735 final public static void printProgramInformation( final String prg_name, final String prg_version, final String date ) {
736 final int l = prg_name.length() + prg_version.length() + date.length() + 4;
737 System.out.println();
738 System.out.println( prg_name + " " + prg_version + " (" + date + ")" );
739 for( int i = 0; i < l; ++i ) {
740 System.out.print( "_" );
742 System.out.println();
745 final public static void printProgramInformation( final String prg_name,
747 final String prg_version,
751 final String based_on ) {
752 String my_prg_name = new String( prg_name );
753 if ( !ForesterUtil.isEmpty( desc ) ) {
754 my_prg_name += ( " - " + desc );
756 final int l = my_prg_name.length() + prg_version.length() + date.length() + 4;
757 System.out.println();
758 System.out.println( my_prg_name + " " + prg_version + " (" + date + ")" );
759 for( int i = 0; i < l; ++i ) {
760 System.out.print( "_" );
762 System.out.println();
763 System.out.println();
764 System.out.println( "WWW : " + www );
765 System.out.println( "Contact : " + email );
766 if ( !ForesterUtil.isEmpty( based_on ) ) {
767 System.out.println( "Based on: " + based_on );
769 if ( !ForesterUtil.isEmpty( ForesterUtil.JAVA_VERSION ) && !ForesterUtil.isEmpty( ForesterUtil.JAVA_VENDOR ) ) {
770 System.out.println();
771 System.out.println( "[running on Java " + ForesterUtil.JAVA_VERSION + " " + ForesterUtil.JAVA_VENDOR + "]" );
773 System.out.println();
776 final public static void printProgramInformation( final String prg_name,
777 final String prg_version,
781 printProgramInformation( prg_name, null, prg_version, date, email, www, null );
784 final public static void printWarningMessage( final String prg_name, final String message ) {
785 System.out.println( "[" + prg_name + "] > warning: " + message );
788 final public static void programMessage( final String prg_name, final String message ) {
789 System.out.println( "[" + prg_name + "] > " + message );
792 final public static String removeSuffix( final String file_name ) {
793 final int i = file_name.lastIndexOf( '.' );
795 return file_name.substring( 0, i );
801 * Removes all white space from String s.
803 * @return String s with white space removed
805 final public static String removeWhiteSpace( String s ) {
807 for( i = 0; i <= s.length() - 1; i++ ) {
808 if ( ( s.charAt( i ) == ' ' ) || ( s.charAt( i ) == '\t' ) || ( s.charAt( i ) == '\n' )
809 || ( s.charAt( i ) == '\r' ) ) {
810 s = s.substring( 0, i ) + s.substring( i + 1 );
817 final public static String replaceIllegalNhCharacters( final String nh ) {
821 return nh.trim().replaceAll( "[\\[\\]:]+", "_" );
824 final public static String replaceIllegalNhxCharacters( final String nhx ) {
828 return nhx.trim().replaceAll( "[\\[\\](),:;\\s]+", "_" );
831 final public static double round( final double value, final int decimal_place ) {
832 BigDecimal bd = new BigDecimal( value );
833 bd = bd.setScale( decimal_place, BigDecimal.ROUND_HALF_UP );
834 return bd.doubleValue();
838 * Rounds d to an int.
840 final public static int roundToInt( final double d ) {
841 return ( int ) ( d + 0.5 );
844 final public static int roundToInt( final float f ) {
845 return ( int ) ( f + 0.5f );
848 final public static short roundToShort( final double d ) {
849 return ( short ) ( d + 0.5 );
852 final public static String sanitizeString( final String s ) {
861 final private static String[] splitString( final String str ) {
862 final String regex = "[\\s;,]+";
863 return str.split( regex );
866 final public static String stringArrayToString( final String[] a ) {
867 return stringArrayToString( a, ", " );
870 final public static String stringArrayToString( final String[] a, final String separator ) {
871 final StringBuilder sb = new StringBuilder();
872 if ( ( a != null ) && ( a.length > 0 ) ) {
873 for( int i = 0; i < a.length - 1; ++i ) {
874 sb.append( a[ i ] + separator );
876 sb.append( a[ a.length - 1 ] );
878 return sb.toString();
881 final public static String[] stringListToArray( final List<String> list ) {
882 if ( list != null ) {
883 final String[] str = new String[ list.size() ];
885 for( final String l : list ) {
893 final public static String stringListToString( final List<String> l, final String separator ) {
894 final StringBuilder sb = new StringBuilder();
895 if ( ( l != null ) && ( l.size() > 0 ) ) {
896 for( int i = 0; i < l.size() - 1; ++i ) {
897 sb.append( l.get( i ) + separator );
899 sb.append( l.get( l.size() - 1 ) );
901 return sb.toString();
904 final public static String[] stringSetToArray( final Set<String> strings ) {
905 final String[] str_array = new String[ strings.size() ];
907 for( final String e : strings ) {
908 str_array[ i++ ] = e;
913 final public static void unexpectedFatalError( final String prg_name, final Exception e ) {
914 System.err.println();
915 System.err.println( "[" + prg_name
916 + "] > unexpected error (Should not have occured! Please contact program author(s).)" );
917 e.printStackTrace( System.err );
918 System.err.println();
922 final public static void unexpectedFatalError( final String prg_name, final String message ) {
923 System.err.println();
924 System.err.println( "[" + prg_name
925 + "] > unexpected error. Should not have occured! Please contact program author(s)." );
926 System.err.println( message );
927 System.err.println();
931 final public static void unexpectedFatalError( final String prg_name, final String message, final Exception e ) {
932 System.err.println();
933 System.err.println( "[" + prg_name
934 + "] > unexpected error. Should not have occured! Please contact program author(s)." );
935 System.err.println( message );
936 e.printStackTrace( System.err );
937 System.err.println();
941 public final static String wordWrap( final String str, final int width ) {
942 final StringBuilder sb = new StringBuilder( str );
946 while ( i < sb.length() ) {
947 if ( sb.charAt( i ) == ' ' ) {
950 if ( sb.charAt( i ) == '\n' ) {
954 if ( i > start + width - 1 ) {
956 sb.setCharAt( ls, '\n' );
961 sb.insert( i, '\n' );
967 return sb.toString();