package it.mice.voila2.acceleogenerator.core;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class VoilaFileWalks {
	
	/** Method to find all voilaProject given a root directory
	 * @author Simone Punzo <simone.punzo@micegroup.it>
	 * @param dir
	 * @param fileExtension
	 * @return
	 * @throws IOException
	 */
	 public static List<Path> findByFileExtension(String dir, String fileExtension)
	            throws IOException {

		 	Path path = Paths.get(dir);
	        if (!Files.isDirectory(path)) {
	            throw new IllegalArgumentException("Path must be a directory!");
	        }

	        List<Path> result;
	        try (Stream<Path> walk = Files.walk(path)) {
	            result = walk
	                    .filter(Files::isRegularFile)   // is a file
	                    .filter(p -> p.getFileName().toString().endsWith(fileExtension))
	                    .collect(Collectors.toList());
	        }
	        return result;

	    }

}
