package jetbrains.mps.ide.java.newparser;
/*Generated by MPS */
import jetbrains.mps.vfs.IFile;
import java.util.Set;
import jetbrains.mps.internal.collections.runtime.SetSequence;
import java.util.HashSet;
import jetbrains.mps.internal.collections.runtime.Sequence;
import jetbrains.mps.internal.collections.runtime.ListSequence;
import jetbrains.mps.vfs.IFileUtils;
import jetbrains.mps.internal.collections.runtime.IWhereFilter;
public class JavaConvertUtil {
public static Iterable<IFile> flattenDirs(Iterable<IFile> filesAndDirs) {
Set<IFile> result = SetSequence.fromSet(new HashSet<IFile>());
for (IFile entry : Sequence.fromIterable(onlyLeaves(filesAndDirs))) {
if (entry.isDirectory()) {
for (IFile file : ListSequence.fromList(IFileUtils.getAllFiles(entry))) {
if (accept(file)) {
SetSequence.fromSet(result).addElement(file);
}
}
} else if (accept(entry)) {
SetSequence.fromSet(result).addElement(entry);
}
}
return result;
}
private static boolean accept(IFile file) {
// called only for files, not dirs
return file.getName().endsWith(".java");
}
/**
* Throw away directories whose files are explicitly mentioned
* It allows to select a vertical range in the tree without worrying
* that too much will be included.
* E.g. we select dir 'parent' and its 2 files out of 10. Only the 2 files will be taken
*/
private static Iterable<IFile> onlyLeaves(Iterable<IFile> all) {
Set<IFile> dirs = SetSequence.fromSetWithValues(new HashSet<IFile>(), Sequence.fromIterable(all).where(new IWhereFilter<IFile>() {
public boolean accept(IFile it) {
return it.isDirectory();
}
}));
Set<IFile> excluded = SetSequence.fromSet(new HashSet<IFile>());
for (IFile item : Sequence.fromIterable(all)) {
IFile parent = item.getParent();
if (SetSequence.fromSet(dirs).contains(parent)) {
SetSequence.fromSet(excluded).addElement(parent);
}
}
return Sequence.fromIterable(all).subtract(SetSequence.fromSet(excluded));
}
}