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 import org.forester.phylogeny.PhylogenyNode;
65 import org.forester.phylogeny.data.Distribution;
66 import org.forester.phylogeny.data.Sequence;
67 import org.forester.phylogeny.data.Taxonomy;
69 public final class ForesterUtil {
71 public final static String FILE_SEPARATOR = System.getProperty( "file.separator" );
72 public final static String LINE_SEPARATOR = System.getProperty( "line.separator" );
73 public final static String JAVA_VENDOR = System.getProperty( "java.vendor" );
74 public final static String JAVA_VERSION = System.getProperty( "java.version" );
75 public final static String OS_ARCH = System.getProperty( "os.arch" );
76 public final static String OS_NAME = System.getProperty( "os.name" );
77 public final static String OS_VERSION = System.getProperty( "os.version" );
78 public final static Pattern PARANTHESESABLE_NH_CHARS_PATTERN = Pattern.compile( "[(),;\\s]" );
79 public final static double ZERO_DIFF = 1.0E-9;
80 public static final BigDecimal NULL_BD = new BigDecimal( 0 );
81 public static final NumberFormat FORMATTER_9;
82 public static final NumberFormat FORMATTER_6;
83 public static final NumberFormat FORMATTER_06;
84 public static final NumberFormat FORMATTER_3;
86 final DecimalFormatSymbols dfs = new DecimalFormatSymbols();
87 dfs.setDecimalSeparator( '.' );
88 // dfs.setGroupingSeparator( ( char ) 0 );
89 FORMATTER_9 = new DecimalFormat( "#.#########", dfs );
90 FORMATTER_6 = new DecimalFormat( "#.######", dfs );
91 FORMATTER_06 = new DecimalFormat( "0.######", dfs );
92 FORMATTER_3 = new DecimalFormat( "#.###", dfs );
95 private ForesterUtil() {
98 public static void ensurePresenceOfTaxonomy( final PhylogenyNode node ) {
99 if ( !node.getNodeData().isHasTaxonomy() ) {
100 node.getNodeData().setTaxonomy( new Taxonomy() );
104 public static void ensurePresenceOfSequence( final PhylogenyNode node ) {
105 if ( !node.getNodeData().isHasSequence() ) {
106 node.getNodeData().setSequence( new Sequence() );
110 final public static void ensurePresenceOfDistribution( final PhylogenyNode node ) {
111 if ( !node.getNodeData().isHasDistribution() ) {
112 node.getNodeData().setDistribution( new Distribution( "" ) );
116 final public static void ensurePresenceOfDate( final PhylogenyNode node ) {
117 if ( !node.getNodeData().isHasDate() ) {
118 node.getNodeData().setDate( new org.forester.phylogeny.data.Date() );
122 final public static void appendSeparatorIfNotEmpty( final StringBuffer sb, final char separator ) {
123 if ( sb.length() > 0 ) {
124 sb.append( separator );
128 public static boolean isWindowns() {
129 return ForesterUtil.OS_NAME.toLowerCase().indexOf( "win" ) > -1;
132 final public static String getForesterLibraryInformation() {
133 return "forester " + ForesterConstants.FORESTER_VERSION + " (" + ForesterConstants.FORESTER_DATE + ")";
136 public static boolean seqIsLikelyToBeAa( final String s ) {
137 final String seq = s.toLowerCase();
138 if ( ( seq.indexOf( 'r' ) > -1 ) || ( seq.indexOf( 'd' ) > -1 ) || ( seq.indexOf( 'e' ) > -1 )
139 || ( seq.indexOf( 'q' ) > -1 ) || ( seq.indexOf( 'h' ) > -1 ) || ( seq.indexOf( 'k' ) > -1 )
140 || ( seq.indexOf( 'w' ) > -1 ) || ( seq.indexOf( 's' ) > -1 ) || ( seq.indexOf( 'm' ) > -1 )
141 || ( seq.indexOf( 'p' ) > -1 ) || ( seq.indexOf( 'v' ) > -1 ) ) {
148 * This calculates a color. If value is equal to min the returned color is
149 * minColor, if value is equal to max the returned color is maxColor,
150 * otherwise a color 'proportional' to value is returned.
164 final public static Color calcColor( double value,
167 final Color minColor,
168 final Color maxColor ) {
175 final double x = ForesterUtil.calculateColorFactor( value, max, min );
176 final int red = ForesterUtil.calculateColorComponent( minColor.getRed(), maxColor.getRed(), x );
177 final int green = ForesterUtil.calculateColorComponent( minColor.getGreen(), maxColor.getGreen(), x );
178 final int blue = ForesterUtil.calculateColorComponent( minColor.getBlue(), maxColor.getBlue(), x );
179 return new Color( red, green, blue );
183 * This calculates a color. If value is equal to min the returned color is
184 * minColor, if value is equal to max the returned color is maxColor, if
185 * value is equal to mean the returned color is meanColor, otherwise a color
186 * 'proportional' to value is returned -- either between min-mean or
196 * the mean/median value
205 final public static Color calcColor( double value,
209 final Color minColor,
210 final Color maxColor,
211 final Color meanColor ) {
218 if ( value < mean ) {
219 final double x = ForesterUtil.calculateColorFactor( value, mean, min );
220 final int red = ForesterUtil.calculateColorComponent( minColor.getRed(), meanColor.getRed(), x );
221 final int green = ForesterUtil.calculateColorComponent( minColor.getGreen(), meanColor.getGreen(), x );
222 final int blue = ForesterUtil.calculateColorComponent( minColor.getBlue(), meanColor.getBlue(), x );
223 return new Color( red, green, blue );
225 else if ( value > mean ) {
226 final double x = ForesterUtil.calculateColorFactor( value, max, mean );
227 final int red = ForesterUtil.calculateColorComponent( meanColor.getRed(), maxColor.getRed(), x );
228 final int green = ForesterUtil.calculateColorComponent( meanColor.getGreen(), maxColor.getGreen(), x );
229 final int blue = ForesterUtil.calculateColorComponent( meanColor.getBlue(), maxColor.getBlue(), x );
230 return new Color( red, green, blue );
238 * Helper method for calcColor methods.
240 * @param smallercolor_component_x
241 * color component the smaller color
242 * @param largercolor_component_x
243 * color component the larger color
246 * @return an int representing a color component
248 final private static int calculateColorComponent( final double smallercolor_component_x,
249 final double largercolor_component_x,
251 return ( int ) ( smallercolor_component_x + ( ( x * ( largercolor_component_x - smallercolor_component_x ) ) / 255.0 ) );
255 * Helper method for calcColor methods.
264 * @return a normalized value between larger and smaller
266 final private static double calculateColorFactor( final double value, final double larger, final double smaller ) {
267 return ( 255.0 * ( value - smaller ) ) / ( larger - smaller );
270 final public static String collapseWhiteSpace( final String s ) {
271 return s.replaceAll( "[\\s]+", " " );
274 final public static void collection2file( final File file, final Collection<?> data, final String separator )
276 final Writer writer = new BufferedWriter( new FileWriter( file ) );
277 collection2writer( writer, data, separator );
281 final public static void collection2writer( final Writer writer, final Collection<?> data, final String separator )
283 boolean first = true;
284 for( final Object object : data ) {
286 writer.write( separator );
291 writer.write( object.toString() );
295 final public static String colorToHex( final Color color ) {
296 final String rgb = Integer.toHexString( color.getRGB() );
297 return rgb.substring( 2, rgb.length() );
300 synchronized public static void copyFile( final File in, final File out ) throws IOException {
301 final FileInputStream in_s = new FileInputStream( in );
302 final FileOutputStream out_s = new FileOutputStream( out );
304 final byte[] buf = new byte[ 1024 ];
306 while ( ( i = in_s.read( buf ) ) != -1 ) {
307 out_s.write( buf, 0, i );
310 catch ( final IOException e ) {
314 if ( in_s != null ) {
317 if ( out_s != null ) {
323 final public static int countChars( final String str, final char c ) {
325 for( int i = 0; i < str.length(); ++i ) {
326 if ( str.charAt( i ) == c ) {
333 final public static BufferedWriter createBufferedWriter( final File file ) throws IOException {
334 if ( file.exists() ) {
335 throw new IOException( "[" + file + "] already exists" );
337 return new BufferedWriter( new FileWriter( file ) );
340 final public static EasyWriter createEasyWriter( final File file ) throws IOException {
341 return new EasyWriter( createBufferedWriter( file ) );
344 final public static BufferedWriter createEasyWriter( final String name ) throws IOException {
345 return createEasyWriter( createFileForWriting( name ) );
348 final public static BufferedWriter createBufferedWriter( final String name ) throws IOException {
349 return new BufferedWriter( new FileWriter( createFileForWriting( name ) ) );
352 final public static File createFileForWriting( final String name ) throws IOException {
353 final File file = new File( name );
354 if ( file.exists() ) {
355 throw new IOException( "[" + name + "] already exists" );
360 public static void fatalError( final String prg_name, final String message ) {
361 System.err.println();
362 System.err.println( "[" + prg_name + "] > " + message );
363 System.err.println();
367 public static void fatalErrorIfFileNotReadable( final String prg_name, final File file ) {
368 final String error = isReadableFile( file );
369 if ( !isEmpty( error ) ) {
370 System.err.println();
371 System.err.println( "[" + prg_name + "] > " + error );
372 System.err.println();
377 public static String[] file2array( final File file ) throws IOException {
378 final List<String> list = file2list( file );
379 final String[] ary = new String[ list.size() ];
381 for( final String s : list ) {
387 final public static List<String> file2list( final File file ) throws IOException {
388 final List<String> list = new ArrayList<String>();
389 final BufferedReader in = new BufferedReader( new FileReader( file ) );
391 while ( ( str = in.readLine() ) != null ) {
393 if ( ( str.length() > 0 ) && !str.startsWith( "#" ) ) {
394 for( final String s : splitString( str ) ) {
403 final public static SortedSet<String> file2set( final File file ) throws IOException {
404 final SortedSet<String> set = new TreeSet<String>();
405 final BufferedReader in = new BufferedReader( new FileReader( file ) );
407 while ( ( str = in.readLine() ) != null ) {
409 if ( ( str.length() > 0 ) && !str.startsWith( "#" ) ) {
410 for( final String s : splitString( str ) ) {
419 final public static String getCurrentDateTime() {
420 final DateFormat format = new SimpleDateFormat( "yyyy/MM/dd HH:mm:ss" );
421 return format.format( new Date() );
424 final public static String getFileSeparator() {
425 return ForesterUtil.FILE_SEPARATOR;
428 final public static String getFirstLine( final Object source ) throws FileNotFoundException, IOException {
429 BufferedReader reader = null;
430 if ( source instanceof File ) {
431 final File f = ( File ) source;
433 throw new IOException( "[" + f.getAbsolutePath() + "] does not exist" );
435 else if ( !f.isFile() ) {
436 throw new IOException( "[" + f.getAbsolutePath() + "] is not a file" );
438 else if ( !f.canRead() ) {
439 throw new IOException( "[" + f.getAbsolutePath() + "] is not a readable" );
441 reader = new BufferedReader( new FileReader( f ) );
443 else if ( source instanceof InputStream ) {
444 reader = new BufferedReader( new InputStreamReader( ( InputStream ) source ) );
446 else if ( source instanceof String ) {
447 reader = new BufferedReader( new StringReader( ( String ) source ) );
449 else if ( source instanceof StringBuffer ) {
450 reader = new BufferedReader( new StringReader( source.toString() ) );
452 else if ( source instanceof URL ) {
453 reader = new BufferedReader( new InputStreamReader( ( ( URL ) source ).openStream() ) );
456 throw new IllegalArgumentException( "dont know how to read [" + source.getClass() + "]" );
459 while ( ( line = reader.readLine() ) != null ) {
461 if ( !ForesterUtil.isEmpty( line ) ) {
462 if ( reader != null ) {
468 if ( reader != null ) {
474 final public static String getLineSeparator() {
475 return ForesterUtil.LINE_SEPARATOR;
478 final public static void increaseCountingMap( final Map<String, Integer> counting_map, final String item_name ) {
479 if ( !counting_map.containsKey( item_name ) ) {
480 counting_map.put( item_name, 1 );
483 counting_map.put( item_name, counting_map.get( item_name ) + 1 );
487 final public static boolean isContainsParanthesesableNhCharacter( final String nh ) {
488 return PARANTHESESABLE_NH_CHARS_PATTERN.matcher( nh ).find();
491 final public static boolean isEmpty( final List<?> l ) {
492 if ( ( l == null ) || l.isEmpty() ) {
495 for( final Object o : l ) {
503 final public static boolean isEmpty( final Set<?> s ) {
504 if ( ( s == null ) || s.isEmpty() ) {
507 for( final Object o : s ) {
515 final public static boolean isEmpty( final String s ) {
516 return ( ( s == null ) || ( s.length() < 1 ) );
519 final public static boolean isEqual( final double a, final double b ) {
520 return ( ( Math.abs( a - b ) ) < ZERO_DIFF );
523 final public static boolean isEven( final int n ) {
524 return ( n % 2 ) == 0;
528 * This determines whether String[] a and String[] b have at least one
529 * String in common (intersect). Returns false if at least one String[] is
533 * a String[] b a String[]
534 * @return true if both a and b or not empty or null and contain at least
535 * one element in common false otherwise
537 final public static boolean isIntersecting( final String[] a, final String[] b ) {
538 if ( ( a == null ) || ( b == null ) ) {
541 if ( ( a.length < 1 ) || ( b.length < 1 ) ) {
544 for( final String ai : a ) {
545 for( final String element : b ) {
546 if ( ( ai != null ) && ( element != null ) && ai.equals( element ) ) {
554 final public static double isLargerOrEqualToZero( final double d ) {
563 final public static boolean isNull( final BigDecimal s ) {
564 return ( ( s == null ) || ( s.compareTo( NULL_BD ) == 0 ) );
567 final public static String isReadableFile( final File f ) {
569 return "file [" + f + "] does not exist";
571 if ( f.isDirectory() ) {
572 return "[" + f + "] is a directory";
575 return "[" + f + "] is not a file";
577 if ( !f.canRead() ) {
578 return "file [" + f + "] is not readable";
580 if ( f.length() < 1 ) {
581 return "file [" + f + "] is empty";
586 final public static String isReadableFile( final String s ) {
587 return isReadableFile( new File( s ) );
590 final public static String isWritableFile( final File f ) {
591 if ( f.isDirectory() ) {
592 return "[" + f + "] is a directory";
595 return "[" + f + "] already exists";
601 * Helper for method "stringToColor".
603 * (Last modified: 12/20/03)
605 final public static int limitRangeForColor( int i ) {
615 final public static SortedMap<Object, Integer> listToSortedCountsMap( final List list ) {
616 final SortedMap<Object, Integer> map = new TreeMap<Object, Integer>();
617 for( final Object key : list ) {
618 if ( !map.containsKey( key ) ) {
622 map.put( key, map.get( key ) + 1 );
628 final public static void map2file( final File file,
629 final Map<?, ?> data,
630 final String entry_separator,
631 final String data_separator ) throws IOException {
632 final Writer writer = new BufferedWriter( new FileWriter( file ) );
633 map2writer( writer, data, entry_separator, data_separator );
637 final public static void map2writer( final Writer writer,
638 final Map<?, ?> data,
639 final String entry_separator,
640 final String data_separator ) throws IOException {
641 boolean first = true;
642 for( final Entry<?, ?> entry : data.entrySet() ) {
644 writer.write( data_separator );
649 writer.write( entry.getKey().toString() );
650 writer.write( entry_separator );
651 writer.write( entry.getValue().toString() );
655 final public static StringBuffer mapToStringBuffer( final Map map, final String key_value_separator ) {
656 final StringBuffer sb = new StringBuffer();
657 for( final Iterator iter = map.keySet().iterator(); iter.hasNext(); ) {
658 final Object key = iter.next();
659 sb.append( key.toString() );
660 sb.append( key_value_separator );
661 sb.append( map.get( key ).toString() );
662 sb.append( ForesterUtil.getLineSeparator() );
667 final public static String normalizeString( final String s,
669 final boolean left_pad,
670 final char pad_char ) {
671 if ( s.length() > length ) {
672 return s.substring( 0, length );
675 final StringBuffer pad = new StringBuffer( length - s.length() );
676 for( int i = 0; i < ( length - s.length() ); ++i ) {
677 pad.append( pad_char );
688 final public static BufferedReader obtainReader( final Object source ) throws IOException, FileNotFoundException {
689 BufferedReader reader = null;
690 if ( source instanceof File ) {
691 final File f = ( File ) source;
693 throw new IOException( "\"" + f.getAbsolutePath() + "\" does not exist" );
695 else if ( !f.isFile() ) {
696 throw new IOException( "\"" + f.getAbsolutePath() + "\" is not a file" );
698 else if ( !f.canRead() ) {
699 throw new IOException( "\"" + f.getAbsolutePath() + "\" is not a readable" );
701 reader = new BufferedReader( new FileReader( f ) );
703 else if ( source instanceof InputStream ) {
704 reader = new BufferedReader( new InputStreamReader( ( InputStream ) source ) );
706 else if ( source instanceof String ) {
707 reader = new BufferedReader( new StringReader( ( String ) source ) );
709 else if ( source instanceof StringBuffer ) {
710 reader = new BufferedReader( new StringReader( source.toString() ) );
713 throw new IllegalArgumentException( "attempt to parse object of type [" + source.getClass()
714 + "] (can only parse objects of type File, InputStream, String, or StringBuffer)" );
719 final public static StringBuffer pad( final double number, final int size, final char pad, final boolean left_pad ) {
720 return pad( new StringBuffer( number + "" ), size, pad, left_pad );
723 final public static StringBuffer pad( final String string, final int size, final char pad, final boolean left_pad ) {
724 return pad( new StringBuffer( string ), size, pad, left_pad );
727 final public static StringBuffer pad( final StringBuffer string,
730 final boolean left_pad ) {
731 final StringBuffer padding = new StringBuffer();
732 final int s = size - string.length();
734 return new StringBuffer( string.substring( 0, size ) );
736 for( int i = 0; i < s; ++i ) {
737 padding.append( pad );
740 return padding.append( string );
743 return string.append( padding );
747 final public static double parseDouble( final String str ) throws ParseException {
748 if ( ForesterUtil.isEmpty( str ) ) {
751 return Double.parseDouble( str );
754 final public static int parseInt( final String str ) throws ParseException {
755 if ( ForesterUtil.isEmpty( str ) ) {
758 return Integer.parseInt( str );
761 final public static void printArray( final Object[] a ) {
762 for( int i = 0; i < a.length; ++i ) {
763 System.out.println( "[" + i + "]=" + a[ i ] );
767 final public static void printCountingMap( final Map<String, Integer> counting_map ) {
768 for( final String key : counting_map.keySet() ) {
769 System.out.println( key + ": " + counting_map.get( key ) );
773 final public static void printErrorMessage( final String prg_name, final String message ) {
774 System.err.println( "[" + prg_name + "] > error: " + message );
777 final public static void printProgramInformation( final String prg_name, final String prg_version, final String date ) {
778 final int l = prg_name.length() + prg_version.length() + date.length() + 4;
779 System.out.println();
780 System.out.println( prg_name + " " + prg_version + " (" + date + ")" );
781 for( int i = 0; i < l; ++i ) {
782 System.out.print( "_" );
784 System.out.println();
787 final public static void printProgramInformation( final String prg_name,
789 final String prg_version,
793 final String based_on ) {
794 String my_prg_name = new String( prg_name );
795 if ( !ForesterUtil.isEmpty( desc ) ) {
796 my_prg_name += ( " - " + desc );
798 final int l = my_prg_name.length() + prg_version.length() + date.length() + 4;
799 System.out.println();
800 System.out.println( my_prg_name + " " + prg_version + " (" + date + ")" );
801 for( int i = 0; i < l; ++i ) {
802 System.out.print( "_" );
804 System.out.println();
805 System.out.println();
806 System.out.println( "WWW : " + www );
807 System.out.println( "Contact : " + email );
808 if ( !ForesterUtil.isEmpty( based_on ) ) {
809 System.out.println( "Based on: " + based_on );
811 if ( !ForesterUtil.isEmpty( ForesterUtil.JAVA_VERSION ) && !ForesterUtil.isEmpty( ForesterUtil.JAVA_VENDOR ) ) {
812 System.out.println();
813 System.out.println( "[running on Java " + ForesterUtil.JAVA_VERSION + " " + ForesterUtil.JAVA_VENDOR + "]" );
815 System.out.println();
818 final public static void printProgramInformation( final String prg_name,
819 final String prg_version,
823 printProgramInformation( prg_name, null, prg_version, date, email, www, null );
826 final public static void printWarningMessage( final String prg_name, final String message ) {
827 System.out.println( "[" + prg_name + "] > warning: " + message );
830 final public static void programMessage( final String prg_name, final String message ) {
831 System.out.println( "[" + prg_name + "] > " + message );
834 final public static String removeSuffix( final String file_name ) {
835 final int i = file_name.lastIndexOf( '.' );
837 return file_name.substring( 0, i );
843 * Removes all white space from String s.
845 * @return String s with white space removed
847 final public static String removeWhiteSpace( String s ) {
849 for( i = 0; i <= ( s.length() - 1 ); i++ ) {
850 if ( ( s.charAt( i ) == ' ' ) || ( s.charAt( i ) == '\t' ) || ( s.charAt( i ) == '\n' )
851 || ( s.charAt( i ) == '\r' ) ) {
852 s = s.substring( 0, i ) + s.substring( i + 1 );
859 final public static String replaceIllegalNhCharacters( final String nh ) {
863 return nh.trim().replaceAll( "[\\[\\]:]+", "_" );
866 final public static String replaceIllegalNhxCharacters( final String nhx ) {
870 return nhx.trim().replaceAll( "[\\[\\](),:;\\s]+", "_" );
873 final public static double round( final double value, final int decimal_place ) {
874 BigDecimal bd = new BigDecimal( value );
875 bd = bd.setScale( decimal_place, BigDecimal.ROUND_HALF_UP );
876 return bd.doubleValue();
880 * Rounds d to an int.
882 final public static int roundToInt( final double d ) {
883 return ( int ) ( d + 0.5 );
886 final public static int roundToInt( final float f ) {
887 return ( int ) ( f + 0.5f );
890 final public static short roundToShort( final double d ) {
891 return ( short ) ( d + 0.5 );
894 final public static String sanitizeString( final String s ) {
903 final private static String[] splitString( final String str ) {
904 final String regex = "[\\s;,]+";
905 return str.split( regex );
908 final public static String stringArrayToString( final String[] a ) {
909 return stringArrayToString( a, ", " );
912 final public static String stringArrayToString( final String[] a, final String separator ) {
913 final StringBuilder sb = new StringBuilder();
914 if ( ( a != null ) && ( a.length > 0 ) ) {
915 for( int i = 0; i < ( a.length - 1 ); ++i ) {
916 sb.append( a[ i ] + separator );
918 sb.append( a[ a.length - 1 ] );
920 return sb.toString();
923 final public static String[] stringListToArray( final List<String> list ) {
924 if ( list != null ) {
925 final String[] str = new String[ list.size() ];
927 for( final String l : list ) {
935 final public static String stringListToString( final List<String> l, final String separator ) {
936 final StringBuilder sb = new StringBuilder();
937 if ( ( l != null ) && ( l.size() > 0 ) ) {
938 for( int i = 0; i < ( l.size() - 1 ); ++i ) {
939 sb.append( l.get( i ) + separator );
941 sb.append( l.get( l.size() - 1 ) );
943 return sb.toString();
946 final public static String[] stringSetToArray( final Set<String> strings ) {
947 final String[] str_array = new String[ strings.size() ];
949 for( final String e : strings ) {
950 str_array[ i++ ] = e;
955 final public static void unexpectedFatalError( final String prg_name, final Exception e ) {
956 System.err.println();
957 System.err.println( "[" + prg_name
958 + "] > unexpected error (Should not have occured! Please contact program author(s).)" );
959 e.printStackTrace( System.err );
960 System.err.println();
964 final public static void unexpectedFatalError( final String prg_name, final String message ) {
965 System.err.println();
966 System.err.println( "[" + prg_name
967 + "] > unexpected error. Should not have occured! Please contact program author(s)." );
968 System.err.println( message );
969 System.err.println();
973 final public static void unexpectedFatalError( final String prg_name, final String message, final Exception e ) {
974 System.err.println();
975 System.err.println( "[" + prg_name
976 + "] > unexpected error. Should not have occured! Please contact program author(s)." );
977 System.err.println( message );
978 e.printStackTrace( System.err );
979 System.err.println();
983 public final static String wordWrap( final String str, final int width ) {
984 final StringBuilder sb = new StringBuilder( str );
988 while ( i < sb.length() ) {
989 if ( sb.charAt( i ) == ' ' ) {
992 if ( sb.charAt( i ) == '\n' ) {
996 if ( i > ( ( start + width ) - 1 ) ) {
998 sb.setCharAt( ls, '\n' );
1003 sb.insert( i, '\n' );
1009 return sb.toString();