Skip to content

Instantly share code, notes, and snippets.

@SanowerTamjit
Last active March 19, 2019 08:39
Show Gist options
  • Save SanowerTamjit/3f33598f3db32dd92871b552f2ffaf82 to your computer and use it in GitHub Desktop.
Save SanowerTamjit/3f33598f3db32dd92871b552f2ffaf82 to your computer and use it in GitHub Desktop.
Random file paths to file tree structure using java
import java.util.*;
public class FilePathsToJsonTree {
public static void main(final String... args) {
List<String> filePaths = new ArrayList<>();
filePaths.add("root/a/1.txt");
filePaths.add("root/");
filePaths.add("root/a/");
filePaths.add("root/a/b/");
filePaths.add("root/e/d/4.txt");
filePaths.add("root/a/3.txt");
filePaths.add("root/b/c/");
filePaths.add("root/b/c/5.5xt");
filePaths.add("root/e/d/");
final dirNode directoryRootNode = createDirectoryTree(filePaths);
System.out.println(directoryRootNode);
}
public static dirNode createDirectoryTree(final List<String> list) {
dirNode treeRootNode = null;
for (final String rawPath : list) {
final String path = rawPath.startsWith("/") ? rawPath.substring(1) : rawPath;
final String[] pathElements = path.split("/");
dirNode movingNode = null;
for (final String pathElement : pathElements) {
String fileExtension = getFileExtension(pathElement);
boolean isDir = false;
movingNode = new dirNode(movingNode, pathElement, fileExtension);
}
if (treeRootNode == null) {
treeRootNode = movingNode.getRoot();
} else {
treeRootNode.merge(movingNode.getRoot());
}
}
return treeRootNode;
}
public static String getFileExtension(String fileName) {
int lastIndexOf = fileName.lastIndexOf(".");
if (lastIndexOf == -1) {
return "";
}
return fileName.substring(lastIndexOf);
}
private static class dirNode {
private final Set<dirNode> childFiles = new LinkedHashSet<>();
private final String value;
private final String fileExt;
private final dirNode parent;
public dirNode(final dirNode parent, final String value, final String fileExt) {
this.parent = parent;
if (this.parent != null) {
this.parent.childFiles.add(this);
}
this.value = value;
this.fileExt = fileExt;
}
public Set<dirNode> getchildFiles() {
return this.childFiles;
}
public String getValue() {
return this.value;
}
public String getFileExt() {
return this.fileExt;
}
public dirNode getParent() {
return this.parent;
}
public int getChildCount() {
int childCount = this.isChild() ? 1 : 0;
for (final dirNode child : this.childFiles) {
childCount += child.getChildCount();
}
return childCount;
}
public boolean isChild() {
return this.childFiles.isEmpty();
}
public dirNode getRoot() {
return this.parent == null ? this : this.parent.getRoot();
}
public void merge(final dirNode that) {
if (!this.value.equals(that.value)) {
return;
} else if (this.childFiles.isEmpty()) {
this.childFiles.addAll(that.childFiles);
return;
}
final dirNode[] thischildFiles = this.childFiles
.toArray(new dirNode[this.childFiles.size()]);
for (final dirNode thisChild : thischildFiles) {
for (final dirNode thatChild : that.childFiles) {
if (thisChild.value.equals(thatChild.value)) {
thisChild.merge(thatChild);
} else if (!this.childFiles.contains(thatChild)) {
this.childFiles.add(thatChild);
}
}
}
}
@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
final dirNode that = (dirNode) o;
return Objects.equals(this.value, that.value)
&& Objects.equals(this.parent, that.parent);
}
@Override
public int hashCode() {
return Objects.hash(this.value, this.parent);
}
@Override
public String toString() {
return "{" +
"\"name\":\"" + this.value + '\"' +
", \"ext\":\"" + this.fileExt + '\"' +
", \"childFiles\":" + this.childFiles +
'}';
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment