Coverage Report - au.csiro.netcdf.util.Util
 
Classes in this File Line Coverage Branch Coverage Complexity
Util
86%
52/60
73%
28/38
4.625
 
 1  
 /**
 2  
  * Copyright 2010, CSIRO Australia.
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *         http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 
 17  
 package au.csiro.netcdf.util;
 18  
 
 19  
 import java.io.File;
 20  
 import java.io.FileFilter;
 21  
 import java.io.FileInputStream;
 22  
 import java.io.IOException;
 23  
 import java.util.ArrayList;
 24  
 import java.util.Arrays;
 25  
 import java.util.Collections;
 26  
 import java.util.LinkedList;
 27  
 import java.util.List;
 28  
 import java.util.regex.Matcher;
 29  
 import java.util.regex.Pattern;
 30  
 
 31  
 import org.apache.commons.io.filefilter.WildcardFileFilter;
 32  
 
 33  
 /**
 34  
  * Util - utility class with various helpful tools...
 35  
  * 
 36  
  * Copyright 2005, CSIRO Australia All rights reserved.
 37  
  * 
 38  
  * @author James Dempsey on 12/10/2005
 39  
  * @version $Revision: 78 $ $Date: 2010-07-24 16:23:13 +1000 (Sat, 24 Jul 2010) $
 40  
  */
 41  0
 public class Util
 42  
 {
 43  
     /**
 44  
      * Check if a string contains a valid number
 45  
      * 
 46  
      * @param number The numeric string to be checked
 47  
      * @return True if the number is valid
 48  
      */
 49  
     public static boolean validateNumber(String number)
 50  
     {
 51  
         try
 52  
         {
 53  86
             Integer.parseInt(number);
 54  78
             return true;
 55  
         }
 56  8
         catch (NumberFormatException e)
 57  
         {
 58  8
             return false;
 59  
         }
 60  
     }
 61  
     
 62  
     /**
 63  
      * Check if a file exists for a given file pathname.
 64  
      * 
 65  
      * @param filePath
 66  
      *            a pathname string.
 67  
      * @return True if the file exists.
 68  
      */
 69  
     public static boolean fileExists(String filePath)
 70  
     {
 71  174
         if(filePath == null || filePath.isEmpty())
 72  
         {
 73  0
             return false;
 74  
         }
 75  174
         File file = new File(filePath);
 76  174
         return file.exists();
 77  
     }
 78  
     
 79  
     /**
 80  
      * Counts the number of lines in a file.
 81  
      * 
 82  
      * @param filePath
 83  
      *            the name of the file to open.
 84  
      * @return The number of lines in the file
 85  
      */
 86  
     public static int fastFileLineCounter(String filePath)
 87  
     {
 88  10
         if (filePath == null || filePath.isEmpty())
 89  
         {
 90  0
             throw new IllegalArgumentException("missing filename!");
 91  
         }
 92  
         try
 93  
         {
 94  10
             FileInputStream fis = new FileInputStream(filePath);
 95  10
             final int bufferSize = 2048;
 96  10
             byte buf[] = new byte[bufferSize];
 97  10
             int cnt = 0;
 98  
             int n;
 99  28
             while ((n = fis.read(buf)) != -1)
 100  
             {
 101  296
                 for (int i = 0; i < n; i++)
 102  
                 {
 103  288
                     if (buf[i] == '\n')
 104  
                     {
 105  24
                         cnt++;
 106  
                     }
 107  
                 }
 108  
             }
 109  10
             fis.close();
 110  10
             return cnt;
 111  
         }
 112  0
         catch (IOException e)
 113  
         {
 114  0
             System.err.println(e);
 115  0
             return -1;
 116  
         }
 117  
     }
 118  
 
 119  
     /**
 120  
      * Creates a new <code>File</code> instance from a given pathname string, if the given pathname string exists.
 121  
      * 
 122  
      * @param outputFilenameArg
 123  
      *            a pathname string.
 124  
      * @return a {@link File} instance.
 125  
      * @throws IllegalArgumentException
 126  
      *             thrown if the file can not be found.
 127  
      */
 128  
     public static File getExistingFile(String outputFilenameArg) throws IllegalArgumentException
 129  
     {
 130  70
         if (!fileExists(outputFilenameArg))
 131  
         {
 132  6
             throw new IllegalArgumentException("The following file must exist: " + outputFilenameArg);
 133  
         }
 134  64
         return new File(outputFilenameArg);
 135  
     }
 136  
     
 137  
     /**
 138  
      * Method to get the file extension as a String for the supplied file name.
 139  
      * If there is no extension it will return <tt>null</tt>.
 140  
      * @param fileName - file name to get extension from
 141  
      * @return String - valid file extension or <tt>null</tt>.
 142  
      */
 143  
     public static String getFileExtension(String fileName)
 144  
     {
 145  68
         String fileExtension = null;
 146  
 
 147  68
         int dotPos = fileName.lastIndexOf(".");
 148  68
         if (dotPos > 0)
 149  
         {
 150  54
             fileExtension = fileName.substring(dotPos);
 151  
         }
 152  
 
 153  68
         return fileExtension;
 154  
     }
 155  
   
 156  
     /**
 157  
      * Splits a comma separated <code>String</code> into a <code>List</code> of tokens.
 158  
      * 
 159  
      * @param commaSeparatedString
 160  
      *            a comma separated <code>String</code>
 161  
      * @return a list of tokens, or an empty list if the <code>String</code> is <tt>null</tt> or empty.
 162  
      */
 163  
     public static List<String> tokeniseCommaSeparatedString(String commaSeparatedString)
 164  
     {
 165  52
         if (commaSeparatedString == null || commaSeparatedString.isEmpty())
 166  
         {
 167  4
             return new ArrayList<String>();
 168  
         }
 169  
                                                                       
 170  48
         String[] tokens = splitOnNonBackslashedCharacter(commaSeparatedString, ","); // split on all ',' characters that
 171  
                                                                                      // are not delimited by a backslash
 172  48
         return Arrays.asList(tokens);
 173  
     }
 174  
     
 175  
     /**
 176  
      * Splits the given string around matches of the given non-backslashed regular expression.
 177  
      * 
 178  
      * @param text
 179  
      *            the string to be split.
 180  
      * @param regex
 181  
      *            the delimiting regular expression.
 182  
      * @return the array of strings computed by splitting this string around matches of the given non-backslashed
 183  
      *         regular expression.
 184  
      */
 185  
     public static String[] splitOnNonBackslashedCharacter(String text, String regex)
 186  
     {
 187  98
         if (text == null)
 188  
         {
 189  0
             text = "";
 190  
         }
 191  
 
 192  98
         int last_match = 0;
 193  98
         List<String> splitted = new LinkedList<String>();
 194  
 
 195  
         // matches any regex character that does not have a backslash in front of it.
 196  98
         Pattern pattern = Pattern.compile("[^\\\\]" + regex);
 197  98
         Matcher m = pattern.matcher(text);
 198  
 
 199  256
         while (m.find())
 200  
         {
 201  60
             splitted.add(text.substring(last_match, m.start() + 1)); // +1 is used to include the character before regex
 202  
                                                                      // which would otherwise be stripped. 
 203  60
             last_match = m.end();
 204  
         }
 205  
 
 206  98
         splitted.add(text.substring(last_match));
 207  98
         return splitted.toArray(new String[splitted.size()]);
 208  
     }
 209  
 
 210  
     /**
 211  
      * Retrieve a list of the absolute path names of the files in a folder that match a pattern. The pattern uses the
 212  
      * characters '?' and '*' to represent a single or multiple wildcard characters. This is the same as often found on
 213  
      * Dos/Unix command lines. 
 214  
      * 
 215  
      * @param foldername
 216  
      *            The name of the folder to be searched or the single file if no pattern
 217  
      * @param pattern
 218  
      *            The pattern to be matched against. The wildcard characters '?' and '*' are supported.
 219  
      * @return A list of the absolute names of the matching files
 220  
      */
 221  
     public static List<String> getListOfTargetFiles(String foldername, String pattern)
 222  
     {
 223  34
         String cleanPat = pattern.replaceAll("\\\\\\*", "*").replaceAll("\\\\\\?", "?");
 224  34
         List<String> fileNameList = new ArrayList<String>();
 225  34
         if (pattern == null || pattern.length() == 0)
 226  
         {
 227  30
             fileNameList.add(foldername);
 228  30
             return fileNameList;
 229  
         }
 230  
         
 231  4
         File dir = new File(foldername);
 232  4
         if (!dir.exists() || !dir.isDirectory() || !dir.canRead())
 233  
         {
 234  0
             return Collections.emptyList();
 235  
         }
 236  
 
 237  4
         FileFilter patternFilter = new WildcardFileFilter(cleanPat);
 238  4
         File[] files = dir.listFiles(patternFilter);
 239  20
         for (int i = 0; i < files.length; i++)
 240  
         {
 241  16
             fileNameList.add(files[i].getAbsolutePath());
 242  
         }
 243  
 
 244  4
         return fileNameList;
 245  
     }
 246  
 }