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 isWindowns() {
100 return ForesterUtil.OS_NAME.toLowerCase().indexOf( "win" ) > -1;
103 final public static String getForesterLibraryInformation() {
104 return "forester " + ForesterConstants.FORESTER_VERSION + " (" + ForesterConstants.FORESTER_DATE + ")";
107 public static boolean seqIsLikelyToBeAa( final String s ) {
108 final String seq = s.toLowerCase();
109 if ( ( seq.indexOf( 'r' ) > -1 ) || ( seq.indexOf( 'd' ) > -1 ) || ( seq.indexOf( 'e' ) > -1 )
110 || ( seq.indexOf( 'q' ) > -1 ) || ( seq.indexOf( 'h' ) > -1 ) || ( seq.indexOf( 'k' ) > -1 )
111 || ( seq.indexOf( 'w' ) > -1 ) || ( seq.indexOf( 's' ) > -1 ) || ( seq.indexOf( 'm' ) > -1 )
112 || ( seq.indexOf( 'p' ) > -1 ) || ( seq.indexOf( 'v' ) > -1 ) ) {
119 * This calculates a color. If value is equal to min the returned color is
120 * minColor, if value is equal to max the returned color is maxColor,
121 * otherwise a color 'proportional' to value is returned.
135 final public static Color calcColor( double value,
138 final Color minColor,
139 final Color maxColor ) {
146 final double x = ForesterUtil.calculateColorFactor( value, max, min );
147 final int red = ForesterUtil.calculateColorComponent( minColor.getRed(), maxColor.getRed(), x );
148 final int green = ForesterUtil.calculateColorComponent( minColor.getGreen(), maxColor.getGreen(), x );
149 final int blue = ForesterUtil.calculateColorComponent( minColor.getBlue(), maxColor.getBlue(), x );
150 return new Color( red, green, blue );
154 * This calculates a color. If value is equal to min the returned color is
155 * minColor, if value is equal to max the returned color is maxColor, if
156 * value is equal to mean the returned color is meanColor, otherwise a color
157 * 'proportional' to value is returned -- either between min-mean or
167 * the mean/median value
176 final public static Color calcColor( double value,
180 final Color minColor,
181 final Color maxColor,
182 final Color meanColor ) {
189 if ( value < mean ) {
190 final double x = ForesterUtil.calculateColorFactor( value, mean, min );
191 final int red = ForesterUtil.calculateColorComponent( minColor.getRed(), meanColor.getRed(), x );
192 final int green = ForesterUtil.calculateColorComponent( minColor.getGreen(), meanColor.getGreen(), x );
193 final int blue = ForesterUtil.calculateColorComponent( minColor.getBlue(), meanColor.getBlue(), x );
194 return new Color( red, green, blue );
196 else if ( value > mean ) {
197 final double x = ForesterUtil.calculateColorFactor( value, max, mean );
198 final int red = ForesterUtil.calculateColorComponent( meanColor.getRed(), maxColor.getRed(), x );
199 final int green = ForesterUtil.calculateColorComponent( meanColor.getGreen(), maxColor.getGreen(), x );
200 final int blue = ForesterUtil.calculateColorComponent( meanColor.getBlue(), maxColor.getBlue(), x );
201 return new Color( red, green, blue );
209 * Helper method for calcColor methods.
211 * @param smallercolor_component_x
212 * color component the smaller color
213 * @param largercolor_component_x
214 * color component the larger color
217 * @return an int representing a color component
219 final private static int calculateColorComponent( final double smallercolor_component_x,
220 final double largercolor_component_x,
222 return ( int ) ( smallercolor_component_x + ( ( x * ( largercolor_component_x - smallercolor_component_x ) ) / 255.0 ) );
226 * Helper method for calcColor methods.
235 * @return a normalized value between larger and smaller
237 final private static double calculateColorFactor( final double value, final double larger, final double smaller ) {
238 return ( 255.0 * ( value - smaller ) ) / ( larger - smaller );
241 final public static String collapseWhiteSpace( final String s ) {
242 return s.replaceAll( "[\\s]+", " " );
245 final public static void collection2file( final File file, final Collection<?> data, final String separator )
247 final Writer writer = new BufferedWriter( new FileWriter( file ) );
248 collection2writer( writer, data, separator );
252 final public static void collection2writer( final Writer writer, final Collection<?> data, final String separator )
254 boolean first = true;
255 for( final Object object : data ) {
257 writer.write( separator );
262 writer.write( object.toString() );
266 final public static String colorToHex( final Color color ) {
267 final String rgb = Integer.toHexString( color.getRGB() );
268 return rgb.substring( 2, rgb.length() );
271 synchronized public static void copyFile( final File in, final File out ) throws IOException {
272 final FileInputStream in_s = new FileInputStream( in );
273 final FileOutputStream out_s = new FileOutputStream( out );
275 final byte[] buf = new byte[ 1024 ];
277 while ( ( i = in_s.read( buf ) ) != -1 ) {
278 out_s.write( buf, 0, i );
281 catch ( final IOException e ) {
285 if ( in_s != null ) {
288 if ( out_s != null ) {
294 final public static int countChars( final String str, final char c ) {
296 for( int i = 0; i < str.length(); ++i ) {
297 if ( str.charAt( i ) == c ) {
304 final public static BufferedWriter createBufferedWriter( final File file ) throws IOException {
305 if ( file.exists() ) {
306 throw new IOException( "[" + file + "] already exists" );
308 return new BufferedWriter( new FileWriter( file ) );
311 final public static EasyWriter createEasyWriter( final File file ) throws IOException {
312 return new EasyWriter( createBufferedWriter( file ) );
315 final public static BufferedWriter createEasyWriter( final String name ) throws IOException {
316 return createEasyWriter( createFileForWriting( name ) );
319 final public static BufferedWriter createBufferedWriter( final String name ) throws IOException {
320 return new BufferedWriter( new FileWriter( createFileForWriting( name ) ) );
323 final public static File createFileForWriting( final String name ) throws IOException {
324 final File file = new File( name );
325 if ( file.exists() ) {
326 throw new IOException( "[" + name + "] already exists" );
331 public static void fatalError( final String prg_name, final String message ) {
332 System.err.println();
333 System.err.println( "[" + prg_name + "] > " + message );
334 System.err.println();
338 public static void fatalErrorIfFileNotReadable( final String prg_name, final File file ) {
339 final String error = isReadableFile( file );
340 if ( !isEmpty( error ) ) {
341 System.err.println();
342 System.err.println( "[" + prg_name + "] > " + error );
343 System.err.println();
348 public static String[] file2array( final File file ) throws IOException {
349 final List<String> list = file2list( file );
350 final String[] ary = new String[ list.size() ];
352 for( final String s : list ) {
358 final public static List<String> file2list( final File file ) throws IOException {
359 final List<String> list = new ArrayList<String>();
360 final BufferedReader in = new BufferedReader( new FileReader( file ) );
362 while ( ( str = in.readLine() ) != null ) {
364 if ( ( str.length() > 0 ) && !str.startsWith( "#" ) ) {
365 for( final String s : splitString( str ) ) {
374 final public static SortedSet<String> file2set( final File file ) throws IOException {
375 final SortedSet<String> set = new TreeSet<String>();
376 final BufferedReader in = new BufferedReader( new FileReader( file ) );
378 while ( ( str = in.readLine() ) != null ) {
380 if ( ( str.length() > 0 ) && !str.startsWith( "#" ) ) {
381 for( final String s : splitString( str ) ) {
390 final public static String getCurrentDateTime() {
391 final DateFormat format = new SimpleDateFormat( "yyyy/MM/dd HH:mm:ss" );
392 return format.format( new Date() );
395 final public static String getFileSeparator() {
396 return ForesterUtil.FILE_SEPARATOR;
399 final public static String getFirstLine( final Object source ) throws FileNotFoundException, IOException {
400 BufferedReader reader = null;
401 if ( source instanceof File ) {
402 final File f = ( File ) source;
404 throw new IOException( "[" + f.getAbsolutePath() + "] does not exist" );
406 else if ( !f.isFile() ) {
407 throw new IOException( "[" + f.getAbsolutePath() + "] is not a file" );
409 else if ( !f.canRead() ) {
410 throw new IOException( "[" + f.getAbsolutePath() + "] is not a readable" );
412 reader = new BufferedReader( new FileReader( f ) );
414 else if ( source instanceof InputStream ) {
415 reader = new BufferedReader( new InputStreamReader( ( InputStream ) source ) );
417 else if ( source instanceof String ) {
418 reader = new BufferedReader( new StringReader( ( String ) source ) );
420 else if ( source instanceof StringBuffer ) {
421 reader = new BufferedReader( new StringReader( source.toString() ) );
423 else if ( source instanceof URL ) {
424 reader = new BufferedReader( new InputStreamReader( ( ( URL ) source ).openStream() ) );
427 throw new IllegalArgumentException( "dont know how to read [" + source.getClass() + "]" );
430 while ( ( line = reader.readLine() ) != null ) {
432 if ( !ForesterUtil.isEmpty( line ) ) {
433 if ( reader != null ) {
439 if ( reader != null ) {
445 final public static String getLineSeparator() {
446 return ForesterUtil.LINE_SEPARATOR;
449 final public static void increaseCountingMap( final Map<String, Integer> counting_map, final String item_name ) {
450 if ( !counting_map.containsKey( item_name ) ) {
451 counting_map.put( item_name, 1 );
454 counting_map.put( item_name, counting_map.get( item_name ) + 1 );
458 final public static boolean isContainsParanthesesableNhCharacter( final String nh ) {
459 return PARANTHESESABLE_NH_CHARS_PATTERN.matcher( nh ).find();
462 final public static boolean isEmpty( final List<?> l ) {
463 if ( ( l == null ) || l.isEmpty() ) {
466 for( final Object o : l ) {
474 final public static boolean isEmpty( final Set<?> s ) {
475 if ( ( s == null ) || s.isEmpty() ) {
478 for( final Object o : s ) {
486 final public static boolean isEmpty( final String s ) {
487 return ( ( s == null ) || ( s.length() < 1 ) );
490 final public static boolean isEqual( final double a, final double b ) {
491 return ( ( Math.abs( a - b ) ) < ZERO_DIFF );
494 final public static boolean isEven( final int n ) {
495 return ( n % 2 ) == 0;
499 * This determines whether String[] a and String[] b have at least one
500 * String in common (intersect). Returns false if at least one String[] is
504 * a String[] b a String[]
505 * @return true if both a and b or not empty or null and contain at least
506 * one element in common false otherwise
508 final public static boolean isIntersecting( final String[] a, final String[] b ) {
509 if ( ( a == null ) || ( b == null ) ) {
512 if ( ( a.length < 1 ) || ( b.length < 1 ) ) {
515 for( final String ai : a ) {
516 for( final String element : b ) {
517 if ( ( ai != null ) && ( element != null ) && ai.equals( element ) ) {
525 final public static double isLargerOrEqualToZero( final double d ) {
534 final public static boolean isNull( final BigDecimal s ) {
535 return ( ( s == null ) || ( s.compareTo( NULL_BD ) == 0 ) );
538 final public static String isReadableFile( final File f ) {
540 return "file [" + f + "] does not exist";
542 if ( f.isDirectory() ) {
543 return "[" + f + "] is a directory";
546 return "[" + f + "] is not a file";
548 if ( !f.canRead() ) {
549 return "file [" + f + "] is not readable";
551 if ( f.length() < 1 ) {
552 return "file [" + f + "] is empty";
557 final public static String isReadableFile( final String s ) {
558 return isReadableFile( new File( s ) );
561 final public static String isWritableFile( final File f ) {
562 if ( f.isDirectory() ) {
563 return "[" + f + "] is a directory";
566 return "[" + f + "] already exists";
572 * Helper for method "stringToColor".
574 * (Last modified: 12/20/03)
576 final public static int limitRangeForColor( int i ) {
586 final public static SortedMap<Object, Integer> listToSortedCountsMap( final List list ) {
587 final SortedMap<Object, Integer> map = new TreeMap<Object, Integer>();
588 for( final Object key : list ) {
589 if ( !map.containsKey( key ) ) {
593 map.put( key, map.get( key ) + 1 );
599 final public static void map2file( final File file,
600 final Map<?, ?> data,
601 final String entry_separator,
602 final String data_separator ) throws IOException {
603 final Writer writer = new BufferedWriter( new FileWriter( file ) );
604 map2writer( writer, data, entry_separator, data_separator );
608 final public static void map2writer( final Writer writer,
609 final Map<?, ?> data,
610 final String entry_separator,
611 final String data_separator ) throws IOException {
612 boolean first = true;
613 for( final Entry<?, ?> entry : data.entrySet() ) {
615 writer.write( data_separator );
620 writer.write( entry.getKey().toString() );
621 writer.write( entry_separator );
622 writer.write( entry.getValue().toString() );
626 final public static StringBuffer mapToStringBuffer( final Map map, final String key_value_separator ) {
627 final StringBuffer sb = new StringBuffer();
628 for( final Iterator iter = map.keySet().iterator(); iter.hasNext(); ) {
629 final Object key = iter.next();
630 sb.append( key.toString() );
631 sb.append( key_value_separator );
632 sb.append( map.get( key ).toString() );
633 sb.append( ForesterUtil.getLineSeparator() );
638 final public static String normalizeString( final String s,
640 final boolean left_pad,
641 final char pad_char ) {
642 if ( s.length() > length ) {
643 return s.substring( 0, length );
646 final StringBuffer pad = new StringBuffer( length - s.length() );
647 for( int i = 0; i < ( length - s.length() ); ++i ) {
648 pad.append( pad_char );
659 final public static BufferedReader obtainReader( final Object source ) throws IOException, FileNotFoundException {
660 BufferedReader reader = null;
661 if ( source instanceof File ) {
662 final File f = ( File ) source;
664 throw new IOException( "\"" + f.getAbsolutePath() + "\" does not exist" );
666 else if ( !f.isFile() ) {
667 throw new IOException( "\"" + f.getAbsolutePath() + "\" is not a file" );
669 else if ( !f.canRead() ) {
670 throw new IOException( "\"" + f.getAbsolutePath() + "\" is not a readable" );
672 reader = new BufferedReader( new FileReader( f ) );
674 else if ( source instanceof InputStream ) {
675 reader = new BufferedReader( new InputStreamReader( ( InputStream ) source ) );
677 else if ( source instanceof String ) {
678 reader = new BufferedReader( new StringReader( ( String ) source ) );
680 else if ( source instanceof StringBuffer ) {
681 reader = new BufferedReader( new StringReader( source.toString() ) );
684 throw new IllegalArgumentException( "attempt to parse object of type [" + source.getClass()
685 + "] (can only parse objects of type File, InputStream, String, or StringBuffer)" );
690 final public static StringBuffer pad( final double number, final int size, final char pad, final boolean left_pad ) {
691 return pad( new StringBuffer( number + "" ), size, pad, left_pad );
694 final public static StringBuffer pad( final String string, final int size, final char pad, final boolean left_pad ) {
695 return pad( new StringBuffer( string ), size, pad, left_pad );
698 final public static StringBuffer pad( final StringBuffer string,
701 final boolean left_pad ) {
702 final StringBuffer padding = new StringBuffer();
703 final int s = size - string.length();
705 return new StringBuffer( string.substring( 0, size ) );
707 for( int i = 0; i < s; ++i ) {
708 padding.append( pad );
711 return padding.append( string );
714 return string.append( padding );
718 final public static double parseDouble( final String str ) throws ParseException {
719 if ( ForesterUtil.isEmpty( str ) ) {
722 return Double.parseDouble( str );
725 final public static int parseInt( final String str ) throws ParseException {
726 if ( ForesterUtil.isEmpty( str ) ) {
729 return Integer.parseInt( str );
732 final public static void printArray( final Object[] a ) {
733 for( int i = 0; i < a.length; ++i ) {
734 System.out.println( "[" + i + "]=" + a[ i ] );
738 final public static void printCountingMap( final Map<String, Integer> counting_map ) {
739 for( final String key : counting_map.keySet() ) {
740 System.out.println( key + ": " + counting_map.get( key ) );
744 final public static void printErrorMessage( final String prg_name, final String message ) {
745 System.out.println( "[" + prg_name + "] > error: " + message );
748 final public static void printProgramInformation( final String prg_name, final String prg_version, final String date ) {
749 final int l = prg_name.length() + prg_version.length() + date.length() + 4;
750 System.out.println();
751 System.out.println( prg_name + " " + prg_version + " (" + date + ")" );
752 for( int i = 0; i < l; ++i ) {
753 System.out.print( "_" );
755 System.out.println();
758 final public static void printProgramInformation( final String prg_name,
760 final String prg_version,
764 final String based_on ) {
765 String my_prg_name = new String( prg_name );
766 if ( !ForesterUtil.isEmpty( desc ) ) {
767 my_prg_name += ( " - " + desc );
769 final int l = my_prg_name.length() + prg_version.length() + date.length() + 4;
770 System.out.println();
771 System.out.println( my_prg_name + " " + prg_version + " (" + date + ")" );
772 for( int i = 0; i < l; ++i ) {
773 System.out.print( "_" );
775 System.out.println();
776 System.out.println();
777 System.out.println( "WWW : " + www );
778 System.out.println( "Contact : " + email );
779 if ( !ForesterUtil.isEmpty( based_on ) ) {
780 System.out.println( "Based on: " + based_on );
782 if ( !ForesterUtil.isEmpty( ForesterUtil.JAVA_VERSION ) && !ForesterUtil.isEmpty( ForesterUtil.JAVA_VENDOR ) ) {
783 System.out.println();
784 System.out.println( "[running on Java " + ForesterUtil.JAVA_VERSION + " " + ForesterUtil.JAVA_VENDOR + "]" );
786 System.out.println();
789 final public static void printProgramInformation( final String prg_name,
790 final String prg_version,
794 printProgramInformation( prg_name, null, prg_version, date, email, www, null );
797 final public static void printWarningMessage( final String prg_name, final String message ) {
798 System.out.println( "[" + prg_name + "] > warning: " + message );
801 final public static void programMessage( final String prg_name, final String message ) {
802 System.out.println( "[" + prg_name + "] > " + message );
805 final public static String removeSuffix( final String file_name ) {
806 final int i = file_name.lastIndexOf( '.' );
808 return file_name.substring( 0, i );
814 * Removes all white space from String s.
816 * @return String s with white space removed
818 final public static String removeWhiteSpace( String s ) {
820 for( i = 0; i <= ( s.length() - 1 ); i++ ) {
821 if ( ( s.charAt( i ) == ' ' ) || ( s.charAt( i ) == '\t' ) || ( s.charAt( i ) == '\n' )
822 || ( s.charAt( i ) == '\r' ) ) {
823 s = s.substring( 0, i ) + s.substring( i + 1 );
830 final public static String replaceIllegalNhCharacters( final String nh ) {
834 return nh.trim().replaceAll( "[\\[\\]:]+", "_" );
837 final public static String replaceIllegalNhxCharacters( final String nhx ) {
841 return nhx.trim().replaceAll( "[\\[\\](),:;\\s]+", "_" );
844 final public static double round( final double value, final int decimal_place ) {
845 BigDecimal bd = new BigDecimal( value );
846 bd = bd.setScale( decimal_place, BigDecimal.ROUND_HALF_UP );
847 return bd.doubleValue();
851 * Rounds d to an int.
853 final public static int roundToInt( final double d ) {
854 return ( int ) ( d + 0.5 );
857 final public static int roundToInt( final float f ) {
858 return ( int ) ( f + 0.5f );
861 final public static short roundToShort( final double d ) {
862 return ( short ) ( d + 0.5 );
865 final public static String sanitizeString( final String s ) {
874 final private static String[] splitString( final String str ) {
875 final String regex = "[\\s;,]+";
876 return str.split( regex );
879 final public static String stringArrayToString( final String[] a ) {
880 return stringArrayToString( a, ", " );
883 final public static String stringArrayToString( final String[] a, final String separator ) {
884 final StringBuilder sb = new StringBuilder();
885 if ( ( a != null ) && ( a.length > 0 ) ) {
886 for( int i = 0; i < ( a.length - 1 ); ++i ) {
887 sb.append( a[ i ] + separator );
889 sb.append( a[ a.length - 1 ] );
891 return sb.toString();
894 final public static String[] stringListToArray( final List<String> list ) {
895 if ( list != null ) {
896 final String[] str = new String[ list.size() ];
898 for( final String l : list ) {
906 final public static String stringListToString( final List<String> l, final String separator ) {
907 final StringBuilder sb = new StringBuilder();
908 if ( ( l != null ) && ( l.size() > 0 ) ) {
909 for( int i = 0; i < ( l.size() - 1 ); ++i ) {
910 sb.append( l.get( i ) + separator );
912 sb.append( l.get( l.size() - 1 ) );
914 return sb.toString();
917 final public static String[] stringSetToArray( final Set<String> strings ) {
918 final String[] str_array = new String[ strings.size() ];
920 for( final String e : strings ) {
921 str_array[ i++ ] = e;
926 final public static void unexpectedFatalError( final String prg_name, final Exception e ) {
927 System.err.println();
928 System.err.println( "[" + prg_name
929 + "] > unexpected error (Should not have occured! Please contact program author(s).)" );
930 e.printStackTrace( System.err );
931 System.err.println();
935 final public static void unexpectedFatalError( final String prg_name, final String message ) {
936 System.err.println();
937 System.err.println( "[" + prg_name
938 + "] > unexpected error. Should not have occured! Please contact program author(s)." );
939 System.err.println( message );
940 System.err.println();
944 final public static void unexpectedFatalError( final String prg_name, final String message, final Exception e ) {
945 System.err.println();
946 System.err.println( "[" + prg_name
947 + "] > unexpected error. Should not have occured! Please contact program author(s)." );
948 System.err.println( message );
949 e.printStackTrace( System.err );
950 System.err.println();
954 public final static String wordWrap( final String str, final int width ) {
955 final StringBuilder sb = new StringBuilder( str );
959 while ( i < sb.length() ) {
960 if ( sb.charAt( i ) == ' ' ) {
963 if ( sb.charAt( i ) == '\n' ) {
967 if ( i > ( ( start + width ) - 1 ) ) {
969 sb.setCharAt( ls, '\n' );
974 sb.insert( i, '\n' );
980 return sb.toString();