001    package edu.nrao.sss.util;
002    
003    import java.io.File;
004    import java.io.FileOutputStream;
005    import java.io.IOException;
006    import java.util.ArrayList;
007    import java.util.Collection;
008    import java.util.StringTokenizer;
009    
010    public class SchedulingUtil {
011    
012      private SchedulingUtil(){
013                    
014            }
015            
016            public static float formatPercent( double value, int digitCount, int decimalCount ){
017                    float normalized = (float)(((int)( value * Math.pow(10, digitCount) + 0.5))/Math.pow(10, decimalCount));
018                    return normalized;
019            }
020            
021            public static float nearestHalf( double value, boolean roundUp ){
022                    int valueInt = (int)value;
023                    double decimal = value - valueInt;
024                    int decInt = (int)( decimal * 10);
025                    int fiveCount = decInt / 5;
026                    int extraFives = 0;
027                    if ( roundUp ){
028                            extraFives = 1;
029                    }
030                    float valueDec = ( fiveCount + extraFives ) * 0.5f;
031                    float nearestHalf = valueInt + valueDec;
032                    return nearestHalf;
033            }
034            
035            public static void writeDebugFile( String xmlStr ){
036                    try {
037                            File f = File.createTempFile( "scheduler", "xml");
038                            FileOutputStream fos = new FileOutputStream( f );
039                            fos.write( xmlStr.getBytes());
040                            fos.flush();
041                            fos.close();
042                            System.out.println( "Wrote file "+f.getAbsolutePath());
043                    }
044                    catch( IOException ioe ){
045                            System.out.println( "Could not write file "+ioe.getMessage());
046                    }
047            }
048            
049            
050            
051            
052              //Note:  StringUtil contains a variation of this, but here we are removing
053            //white space before making the string.
054              public static String fromCollection(Collection<?> source, String separator) {
055                StringBuilder buff = new StringBuilder();
056                StringUtil sUtil = StringUtil.getInstance();
057                for (Object value : source){
058                    if ( value != null ){
059                            String valueStr = sUtil.normalizeString( value.toString() );
060                            buff.append(valueStr).append(separator);
061                    }
062                }
063                
064                //Remove the final separator
065                int buffSize = buff.length();
066                if (buffSize > 0){
067                  int separatorSize = separator.length();
068                  buff.delete(buffSize-separatorSize, buffSize);
069                }
070                
071                return buff.toString();
072              }
073              
074              //Note:  StringUtil contains a variation of this, but here we are removing
075              //white space
076              public static Collection<String> toCollection(String source, String separator, 
077                                                     Collection<String> destination){
078                if (destination == null)
079                  destination = new ArrayList<String>();
080                StringUtil stringUtil = StringUtil.getInstance();
081                StringTokenizer splitter = new StringTokenizer(source, separator);
082                while (splitter.hasMoreTokens()){
083                    
084                    String nextToken = stringUtil.normalizeString( splitter.nextToken()); 
085                    destination.add( nextToken );
086                }
087                return destination;
088              }
089    
090    }