Changeset 10047


Ignore:
Timestamp:
Aug 10, 2017 1:07:54 PM (2 years ago)
Author:
Zimon Kuhs
Message:

#5432

  • Added methods to FileUtil.
    • toFile makes files from string paths.
    • filterOnExtensions extracts files with specified extensions from a collection.
Location:
trunk/Compiler/ModelicaFrontEnd/src/java/org/jmodelica/util
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/Compiler/ModelicaFrontEnd/src/java/org/jmodelica/util/collections/ListUtil.java

    r10034 r10047  
    2828        return list;
    2929    }
    30    
     30
    3131    /**
    3232     * Creates a list of strings from the string representation of elements.
  • trunk/Compiler/ModelicaFrontEnd/src/java/org/jmodelica/util/files/FileUtil.java

    r10034 r10047  
    125125
    126126    /**
     127     * Retrieves all the names of the files from the specified sources.
     128     *
     129     * @param sources
     130     *            The file sources.
     131     * @return
     132     *         a list of file names from the specified sources.
     133     * @see #allFileNames(boolean, File...)
     134     */
     135    public static Collection<String> allFileNames(Collection<File> sources) {
     136        return allFileNames(sources.toArray(new File[sources.size()]));
     137    }
     138
     139    /**
    127140     * Retrieves all the names of the files or directories from the specified
    128141     * sources.
     
    250263
    251264    /**
     265     * Filters a set of file on their extensions.
     266     *
     267     * @param files         the files to filter.
     268     * @param extensions    the extensions to filter on.
     269     * @return              a collection of files from {@code files} with a file extension present
     270     *                      in {@code extensions}.
     271     */
     272    public static Collection<File> filterOnExtensions(Collection<File> files, String... extensions) {
     273        Collection<File> filtered = new ArrayList<File>();
     274        for (File file : files) {
     275            for (String ext : extensions) {
     276                if (file.getName().endsWith(ext)) {
     277                    filtered.add(file);
     278                    break;
     279                }
     280            }
     281        }
     282        return filtered;
     283    }
     284   
     285    /**
    252286     * Quick method for specifying {@link StandardCopyOption#REPLACE_EXISTING}.
    253287     *
     
    262296        return replace ? StandardCopyOption.REPLACE_EXISTING : null;
    263297    }
     298
     299    /**
     300     * Creates a list of {@link File} objects from a list of string paths.
     301     *
     302     * @param paths the string paths.
     303     * @return      a list of {@link File} objects.
     304     */
     305    public static Collection<File> toFile(String... paths) {
     306        java.util.List<File> files = new ArrayList<File>();
     307        for (String name : paths) {
     308            files.add(new File(name));
     309        }
     310        return files;
     311    }
     312   
     313    /**
     314     * Creates a list of {@link File} objects from a list of string paths.
     315     *
     316     * @param paths the string paths.
     317     * @return      a list of {@link File} objects.
     318     */
     319    public static Collection<File> toFile(List<String> paths) {
     320        java.util.List<File> files = new ArrayList<File>();
     321        for (String name : paths) {
     322            files.add(new File(name));
     323        }
     324        return files;
     325    }
     326
    264327}
Note: See TracChangeset for help on using the changeset viewer.