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.math.BigDecimal;
43 import java.text.DateFormat;
44 import java.text.DecimalFormat;
45 import java.text.DecimalFormatSymbols;
46 import java.text.NumberFormat;
47 import java.text.ParseException;
48 import java.text.SimpleDateFormat;
49 import java.util.ArrayList;
50 import java.util.Date;
51 import java.util.Iterator;
52 import java.util.List;
55 import java.util.SortedMap;
56 import java.util.SortedSet;
57 import java.util.TreeMap;
58 import java.util.TreeSet;
59 import java.util.regex.Pattern;
61 public final class ForesterUtil {
63 public final static String FILE_SEPARATOR = System.getProperty( "file.separator" );
64 public final static String LINE_SEPARATOR = System.getProperty( "line.separator" );
65 public final static String JAVA_VENDOR = System.getProperty( "java.vendor" );
66 public final static String JAVA_VERSION = System.getProperty( "java.version" );
67 public final static String OS_ARCH = System.getProperty( "os.arch" );
68 public final static String OS_NAME = System.getProperty( "os.name" );
69 public final static String OS_VERSION = System.getProperty( "os.version" );
70 public final static Pattern PARANTHESESABLE_NH_CHARS_PATTERN = Pattern.compile( "[(),;\\s]" );
71 public final static double ZERO_DIFF = 1.0E-9;
72 public static final BigDecimal NULL_BD = new BigDecimal( 0 );
73 public static final NumberFormat FORMATTER_9;
74 public static final NumberFormat FORMATTER_6;
75 public static final NumberFormat FORMATTER_06;
76 public static final NumberFormat FORMATTER_3;
78 final DecimalFormatSymbols dfs = new DecimalFormatSymbols();
79 dfs.setDecimalSeparator( '.' );
80 // dfs.setGroupingSeparator( ( char ) 0 );
81 FORMATTER_9 = new DecimalFormat( "#.#########", dfs );
82 FORMATTER_6 = new DecimalFormat( "#.######", dfs );
83 FORMATTER_06 = new DecimalFormat( "0.######", dfs );
84 FORMATTER_3 = new DecimalFormat( "#.###", dfs );
87 private ForesterUtil() {
90 final public static void appendSeparatorIfNotEmpty( final StringBuffer sb, final char separator ) {
91 if ( sb.length() > 0 ) {
92 sb.append( separator );
97 * This calculates a color. If value is equal to min the returned color is
98 * minColor, if value is equal to max the returned color is maxColor,
99 * otherwise a color 'proportional' to value is returned.
113 final public static Color calcColor( double value,
116 final Color minColor,
117 final Color maxColor ) {
124 final double x = ForesterUtil.calculateColorFactor( value, max, min );
125 final int red = ForesterUtil.calculateColorComponent( minColor.getRed(), maxColor.getRed(), x );
126 final int green = ForesterUtil.calculateColorComponent( minColor.getGreen(), maxColor.getGreen(), x );
127 final int blue = ForesterUtil.calculateColorComponent( minColor.getBlue(), maxColor.getBlue(), x );
128 return new Color( red, green, blue );
132 * This calculates a color. If value is equal to min the returned color is
133 * minColor, if value is equal to max the returned color is maxColor, if
134 * value is equal to mean the returned color is meanColor, otherwise a color
135 * 'proportional' to value is returned -- either between min-mean or
145 * the mean/median value
154 final public static Color calcColor( double value,
158 final Color minColor,
159 final Color maxColor,
160 final Color meanColor ) {
167 if ( value < mean ) {
168 final double x = ForesterUtil.calculateColorFactor( value, mean, min );
169 final int red = ForesterUtil.calculateColorComponent( minColor.getRed(), meanColor.getRed(), x );
170 final int green = ForesterUtil.calculateColorComponent( minColor.getGreen(), meanColor.getGreen(), x );
171 final int blue = ForesterUtil.calculateColorComponent( minColor.getBlue(), meanColor.getBlue(), x );
172 return new Color( red, green, blue );
174 else if ( value > mean ) {
175 final double x = ForesterUtil.calculateColorFactor( value, max, mean );
176 final int red = ForesterUtil.calculateColorComponent( meanColor.getRed(), maxColor.getRed(), x );
177 final int green = ForesterUtil.calculateColorComponent( meanColor.getGreen(), maxColor.getGreen(), x );
178 final int blue = ForesterUtil.calculateColorComponent( meanColor.getBlue(), maxColor.getBlue(), x );
179 return new Color( red, green, blue );
187 * Helper method for calcColor methods.
189 * @param smallercolor_component_x
190 * color component the smaller color
191 * @param largercolor_component_x
192 * color component the larger color
195 * @return an int representing a color component
197 final private static int calculateColorComponent( final double smallercolor_component_x,
198 final double largercolor_component_x,
200 return ( int ) ( smallercolor_component_x + ( ( x * ( largercolor_component_x - smallercolor_component_x ) ) / 255.0 ) );
204 * Helper method for calcColor methods.
213 * @return a normalized value between larger and smaller
215 final private static double calculateColorFactor( final double value, final double larger, final double smaller ) {
216 return ( 255.0 * ( value - smaller ) ) / ( larger - smaller );
219 final public static String collapseWhiteSpace( final String s ) {
220 return s.replaceAll( "[\\s]+", " " );
223 final public static String colorToHex( final Color color ) {
224 final String rgb = Integer.toHexString( color.getRGB() );
225 return rgb.substring( 2, rgb.length() );
228 synchronized public static void copyFile( final File in, final File out ) throws IOException {
229 final FileInputStream in_s = new FileInputStream( in );
230 final FileOutputStream out_s = new FileOutputStream( out );
232 final byte[] buf = new byte[ 1024 ];
234 while ( ( i = in_s.read( buf ) ) != -1 ) {
235 out_s.write( buf, 0, i );
238 catch ( final IOException e ) {
242 if ( in_s != null ) {
245 if ( out_s != null ) {
251 final public static int countChars( final String str, final char c ) {
253 for( int i = 0; i < str.length(); ++i ) {
254 if ( str.charAt( i ) == c ) {
261 final public static BufferedWriter createBufferedWriter( final File file ) throws IOException {
262 if ( file.exists() ) {
263 throw new IOException( "[" + file + "] already exists" );
265 return new BufferedWriter( new FileWriter( file ) );
268 final public static BufferedWriter createBufferedWriter( final String name ) throws IOException {
269 return new BufferedWriter( new FileWriter( createFileForWriting( name ) ) );
272 final public static File createFileForWriting( final String name ) throws IOException {
273 final File file = new File( name );
274 if ( file.exists() ) {
275 throw new IOException( "[" + name + "] already exists" );
280 public static void fatalError( final String prg_name, final String message ) {
281 System.err.println();
282 System.err.println( "[" + prg_name + "] > " + message );
283 System.err.println();
287 public static String[] file2array( final File file ) throws IOException {
288 final List<String> list = file2list( file );
289 final String[] ary = new String[ list.size() ];
291 for( final String s : list ) {
297 final public static List<String> file2list( final File file ) throws IOException {
298 final List<String> list = new ArrayList<String>();
299 final BufferedReader in = new BufferedReader( new FileReader( file ) );
301 while ( ( str = in.readLine() ) != null ) {
303 if ( ( str.length() > 0 ) && !str.startsWith( "#" ) ) {
304 for( final String s : splitString( str ) ) {
313 final public static SortedSet<String> file2set( final File file ) throws IOException {
314 final SortedSet<String> set = new TreeSet<String>();
315 final BufferedReader in = new BufferedReader( new FileReader( file ) );
317 while ( ( str = in.readLine() ) != null ) {
319 if ( ( str.length() > 0 ) && !str.startsWith( "#" ) ) {
320 for( final String s : splitString( str ) ) {
329 final public static String getCurrentDateTime() {
330 final DateFormat format = new SimpleDateFormat( "yyyy/MM/dd HH:mm:ss" );
331 return format.format( new Date() );
334 final public static String getFileSeparator() {
335 return ForesterUtil.FILE_SEPARATOR;
338 final public static String getFirstLine( final Object source ) throws FileNotFoundException, IOException {
339 BufferedReader reader = null;
340 if ( source instanceof File ) {
341 final File f = ( File ) source;
343 throw new IOException( "[" + f.getAbsolutePath() + "] does not exist" );
345 else if ( !f.isFile() ) {
346 throw new IOException( "[" + f.getAbsolutePath() + "] is not a file" );
348 else if ( !f.canRead() ) {
349 throw new IOException( "[" + f.getAbsolutePath() + "] is not a readable" );
351 reader = new BufferedReader( new FileReader( f ) );
353 else if ( source instanceof InputStream ) {
354 reader = new BufferedReader( new InputStreamReader( ( InputStream ) source ) );
356 else if ( source instanceof String ) {
357 reader = new BufferedReader( new StringReader( ( String ) source ) );
359 else if ( source instanceof StringBuffer ) {
360 reader = new BufferedReader( new StringReader( source.toString() ) );
362 else if ( source instanceof URL ) {
363 reader = new BufferedReader( new InputStreamReader( ( ( URL ) source ).openStream() ) );
366 throw new IllegalArgumentException( "dont know how to read [" + source.getClass() + "]" );
369 while ( ( line = reader.readLine() ) != null ) {
371 if ( !ForesterUtil.isEmpty( line ) ) {
372 if ( reader != null ) {
378 if ( reader != null ) {
384 final public static String getLineSeparator() {
385 return ForesterUtil.LINE_SEPARATOR;
388 final public static void increaseCountingMap( final Map<String, Integer> counting_map, final String item_name ) {
389 if ( !counting_map.containsKey( item_name ) ) {
390 counting_map.put( item_name, 1 );
393 counting_map.put( item_name, counting_map.get( item_name ) + 1 );
397 final public static boolean isContainsParanthesesableNhCharacter( final String nh ) {
398 return PARANTHESESABLE_NH_CHARS_PATTERN.matcher( nh ).find();
401 final public static boolean isEmpty( final List<?> l ) {
402 if ( ( l == null ) || l.isEmpty() ) {
405 for( final Object o : l ) {
413 final public static boolean isEmpty( final Set<?> s ) {
414 if ( ( s == null ) || s.isEmpty() ) {
417 for( final Object o : s ) {
425 final public static boolean isEmpty( final String s ) {
426 return ( ( s == null ) || ( s.length() < 1 ) );
429 final public static boolean isEqual( final double a, final double b ) {
430 return ( ( Math.abs( a - b ) ) < ZERO_DIFF );
433 final public static boolean isEven( final int n ) {
438 * This determines whether String[] a and String[] b have at least one
439 * String in common (intersect). Returns false if at least one String[] is
443 * a String[] b a String[]
444 * @return true if both a and b or not empty or null and contain at least
445 * one element in common false otherwise
447 final public static boolean isIntersecting( final String[] a, final String[] b ) {
448 if ( ( a == null ) || ( b == null ) ) {
451 if ( ( a.length < 1 ) || ( b.length < 1 ) ) {
454 for( int i = 0; i < a.length; ++i ) {
455 final String ai = a[ i ];
456 for( int j = 0; j < b.length; ++j ) {
457 if ( ( ai != null ) && ( b[ j ] != null ) && ai.equals( b[ j ] ) ) {
465 final public static double isLargerOrEqualToZero( final double d ) {
474 final public static boolean isNull( final BigDecimal s ) {
475 return ( ( s == null ) || ( s.compareTo( NULL_BD ) == 0 ) );
478 final public static String isReadableFile( final File f ) {
480 return "file [" + f + "] does not exist";
482 if ( f.isDirectory() ) {
483 return "[" + f + "] is a directory";
486 return "[" + f + "] is not a file";
488 if ( !f.canRead() ) {
489 return "file [" + f + "] is not readable";
491 if ( f.length() < 1 ) {
492 return "file [" + f + "] is empty";
497 final public static String isReadableFile( final String s ) {
498 return isReadableFile( new File( s ) );
501 final public static String isWritableFile( final File f ) {
502 if ( f.isDirectory() ) {
503 return "[" + f + "] is a directory";
506 return "[" + f + "] already exists";
512 * Helper for method "stringToColor".
514 * (Last modified: 12/20/03)
516 final public static int limitRangeForColor( int i ) {
526 final public static SortedMap<Object, Integer> listToSortedCountsMap( final List list ) {
527 final SortedMap<Object, Integer> map = new TreeMap<Object, Integer>();
528 for( final Object key : list ) {
529 if ( !map.containsKey( key ) ) {
533 map.put( key, map.get( key ) + 1 );
539 final public static StringBuffer mapToStringBuffer( final Map map, final String key_value_separator ) {
540 final StringBuffer sb = new StringBuffer();
541 for( final Iterator iter = map.keySet().iterator(); iter.hasNext(); ) {
542 final Object key = iter.next();
543 sb.append( key.toString() );
544 sb.append( key_value_separator );
545 sb.append( map.get( key ).toString() );
546 sb.append( ForesterUtil.getLineSeparator() );
551 final public static String normalizeString( final String s,
553 final boolean left_pad,
554 final char pad_char ) {
555 if ( s.length() > length ) {
556 return s.substring( 0, length );
559 final StringBuffer pad = new StringBuffer( length - s.length() );
560 for( int i = 0; i < ( length - s.length() ); ++i ) {
561 pad.append( pad_char );
572 final public static BufferedReader obtainReader( final Object source ) throws IOException, FileNotFoundException {
573 BufferedReader reader = null;
574 if ( source instanceof File ) {
575 final File f = ( File ) source;
577 throw new IOException( "\"" + f.getAbsolutePath() + "\" does not exist" );
579 else if ( !f.isFile() ) {
580 throw new IOException( "\"" + f.getAbsolutePath() + "\" is not a file" );
582 else if ( !f.canRead() ) {
583 throw new IOException( "\"" + f.getAbsolutePath() + "\" is not a readable" );
585 reader = new BufferedReader( new FileReader( f ) );
587 else if ( source instanceof InputStream ) {
588 reader = new BufferedReader( new InputStreamReader( ( InputStream ) source ) );
590 else if ( source instanceof String ) {
591 reader = new BufferedReader( new StringReader( ( String ) source ) );
593 else if ( source instanceof StringBuffer ) {
594 reader = new BufferedReader( new StringReader( source.toString() ) );
597 throw new IllegalArgumentException( "attempt to parse object of type [" + source.getClass()
598 + "] (can only parse objects of type File, InputStream, String, or StringBuffer)" );
603 final public static StringBuffer pad( final double number, final int size, final char pad, final boolean left_pad ) {
604 return pad( new StringBuffer( number + "" ), size, pad, left_pad );
607 final public static StringBuffer pad( final String string, final int size, final char pad, final boolean left_pad ) {
608 return pad( new StringBuffer( string ), size, pad, left_pad );
611 final public static StringBuffer pad( final StringBuffer string,
614 final boolean left_pad ) {
615 final StringBuffer padding = new StringBuffer();
616 final int s = size - string.length();
618 return new StringBuffer( string.substring( 0, size ) );
620 for( int i = 0; i < s; ++i ) {
621 padding.append( pad );
624 return padding.append( string );
627 return string.append( padding );
631 final public static double parseDouble( final String str ) throws ParseException {
632 if ( ForesterUtil.isEmpty( str ) ) {
635 return Double.parseDouble( str );
638 final public static int parseInt( final String str ) throws ParseException {
639 if ( ForesterUtil.isEmpty( str ) ) {
642 return Integer.parseInt( str );
645 final public static void printArray( final Object[] a ) {
646 for( int i = 0; i < a.length; ++i ) {
647 System.out.println( "[" + i + "]=" + a[ i ] );
651 final public static void printCountingMap( final Map<String, Integer> counting_map ) {
652 for( final String key : counting_map.keySet() ) {
653 System.out.println( key + ": " + counting_map.get( key ) );
657 final public static void printErrorMessage( final String prg_name, final String message ) {
658 System.out.println( "[" + prg_name + "] > error: " + message );
661 final public static void printProgramInformation( final String prg_name, final String prg_version, final String date ) {
662 final int l = prg_name.length() + prg_version.length() + date.length() + 4;
663 System.out.println();
664 System.out.println( prg_name + " " + prg_version + " (" + date + ")" );
665 for( int i = 0; i < l; ++i ) {
666 System.out.print( "_" );
668 System.out.println();
671 final public static void printProgramInformation( final String prg_name,
672 final String prg_version,
676 final int l = prg_name.length() + prg_version.length() + date.length() + 4;
677 System.out.println();
678 System.out.println( prg_name + " " + prg_version + " (" + date + ")" );
679 for( int i = 0; i < l; ++i ) {
680 System.out.print( "_" );
682 System.out.println();
683 System.out.println();
684 System.out.println( "WWW : " + www );
685 System.out.println( "Contact: " + email );
686 if ( !ForesterUtil.isEmpty( ForesterUtil.JAVA_VERSION ) && !ForesterUtil.isEmpty( ForesterUtil.JAVA_VENDOR ) ) {
687 System.out.println();
688 System.out.println( "[running on Java " + ForesterUtil.JAVA_VERSION + " " + ForesterUtil.JAVA_VENDOR + "]" );
690 System.out.println();
693 final public static void printWarningMessage( final String prg_name, final String message ) {
694 System.out.println( "[" + prg_name + "] > warning: " + message );
697 final public static void programMessage( final String prg_name, final String message ) {
698 System.out.println( "[" + prg_name + "] > " + message );
701 final public static String removeSuffix( final String file_name ) {
702 final int i = file_name.lastIndexOf( '.' );
704 return file_name.substring( 0, i );
710 * Removes all white space from String s.
712 * @return String s with white space removed
714 final public static String removeWhiteSpace( String s ) {
716 for( i = 0; i <= s.length() - 1; i++ ) {
717 if ( ( s.charAt( i ) == ' ' ) || ( s.charAt( i ) == '\t' ) || ( s.charAt( i ) == '\n' )
718 || ( s.charAt( i ) == '\r' ) ) {
719 s = s.substring( 0, i ) + s.substring( i + 1 );
726 final public static String replaceIllegalNhCharacters( final String nh ) {
730 return nh.trim().replaceAll( "[\\[\\]:]+", "_" );
733 final public static String replaceIllegalNhxCharacters( final String nhx ) {
737 return nhx.trim().replaceAll( "[\\[\\](),:;\\s]+", "_" );
740 final public static double round( final double value, final int decimal_place ) {
741 BigDecimal bd = new BigDecimal( value );
742 bd = bd.setScale( decimal_place, BigDecimal.ROUND_HALF_UP );
743 return bd.doubleValue();
747 * Rounds d to an int.
749 final public static int roundToInt( final double d ) {
750 return ( int ) ( d + 0.5 );
753 final public static int roundToInt( final float f ) {
754 return ( int ) ( f + 0.5f );
757 final public static short roundToShort( final double d ) {
758 return ( short ) ( d + 0.5 );
761 final public static String sanitizeString( final String s ) {
770 final private static String[] splitString( final String str ) {
771 final String regex = "[\\s;,]+";
772 return str.split( regex );
775 final public static String stringArrayToString( final String[] a, final String separator ) {
776 final StringBuilder sb = new StringBuilder();
777 if ( ( a != null ) && ( a.length > 0 ) ) {
778 for( int i = 0; i < a.length - 1; ++i ) {
779 sb.append( a[ i ] + separator );
781 sb.append( a[ a.length - 1 ] );
783 return sb.toString();
786 final public static String stringListToString( final List<String> l, final String separator ) {
787 final StringBuilder sb = new StringBuilder();
788 if ( ( l != null ) && ( l.size() > 0 ) ) {
789 for( int i = 0; i < l.size() - 1; ++i ) {
790 sb.append( l.get( i ) + separator );
792 sb.append( l.get( l.size() - 1 ) );
794 return sb.toString();
797 final public static String stringArrayToString( final String[] a ) {
798 return stringArrayToString( a, ", " );
801 final public static String[] stringSetToArray( final Set<String> strings ) {
802 final String[] str_array = new String[ strings.size() ];
804 for( final String e : strings ) {
805 str_array[ i++ ] = e;
810 final public static String[] stringListToArray( final List<String> list ) {
811 if ( list != null ) {
812 final String[] str = new String[ list.size() ];
814 for( final String l : list ) {
822 final public static void unexpectedFatalError( final String prg_name, final Exception e ) {
823 System.err.println();
824 System.err.println( "[" + prg_name
825 + "] > unexpected error (Should not have occured! Please contact program author(s).)" );
826 e.printStackTrace( System.err );
827 System.err.println();
831 final public static void unexpectedFatalError( final String prg_name, final String message ) {
832 System.err.println();
833 System.err.println( "[" + prg_name
834 + "] > unexpected error. Should not have occured! Please contact program author(s)." );
835 System.err.println( message );
836 System.err.println();
840 final public static void unexpectedFatalError( final String prg_name, final String message, final Exception e ) {
841 System.err.println();
842 System.err.println( "[" + prg_name
843 + "] > unexpected error. Should not have occured! Please contact program author(s)." );
844 System.err.println( message );
845 e.printStackTrace( System.err );
846 System.err.println();
850 public final static String wordWrap( final String str, final int width ) {
851 final StringBuilder sb = new StringBuilder( str );
855 while ( i < sb.length() ) {
856 if ( sb.charAt( i ) == ' ' ) {
859 if ( sb.charAt( i ) == '\n' ) {
863 if ( i > start + width - 1 ) {
865 sb.setCharAt( ls, '\n' );
870 sb.insert( i, '\n' );
876 return sb.toString();