new support classes added

This commit is contained in:
Daniel Collin 2015-10-20 06:56:14 +00:00
parent 9764f546da
commit 83e0196c50
21 changed files with 798 additions and 9 deletions

View file

@ -0,0 +1,84 @@
package com.coder.client.file;
import java.util.LinkedList;
import java.util.List;
public class ProjectDirectory extends ProjectFileObject {
private LinkedList<ProjectFileObject> objectList;
public ProjectDirectory(String name){
super(name);
this.objectList = new LinkedList<ProjectFileObject>();
}
public List<ProjectFileObject> getAll(){
return objectList;
}
public boolean containsFile(String fileName){
for(ProjectFileObject fileObject : objectList){
if(fileObject instanceof ProjectFile){
if(fileObject.getName().equals(fileName)){
return true;
}
}
}
return false;
}
public void addFile(ProjectFile file){
this.objectList.add(file);
}
public void deleteFile(String fileName){
for(ProjectFileObject fileObject : objectList){
if(fileObject instanceof ProjectFile){
if(fileObject.getName().equals(fileName)){
this.objectList.remove(fileObject);
}
}
}
}
public boolean containsDirectory(String dirName){
for(ProjectFileObject fileObject : objectList){
if(fileObject instanceof ProjectDirectory){
if(fileObject.getName().equals(dirName)){
return true;
}
}
}
return false;
}
public void addDirectory(String dirName){
ProjectDirectory dir = new ProjectDirectory(dirName);
this.objectList.add(dir);
}
public void deleteDirectory(String dirName){
for(ProjectFileObject fileObject : objectList){
if(fileObject instanceof ProjectDirectory){
if(fileObject.getName().equals(dirName)){
this.objectList.remove(fileObject);
}
}
}
}
public ProjectDirectory getDirectory(String dirName) {
for(ProjectFileObject fileObject : objectList){
if(fileObject instanceof ProjectDirectory){
if(fileObject.getName().equals(dirName)){
return (ProjectDirectory) fileObject;
}
}
}
return null;
}
public String toString(){
return this.getName();
}
}

View file

@ -0,0 +1,15 @@
package com.coder.client.file;
public class ProjectFile extends ProjectFileObject{
private String filePath;
public ProjectFile(String name, String filePath) {
super(name);
this.filePath = filePath;
}
public String toString(){
return this.getName() + " ("+filePath+")";
}
}

View file

@ -0,0 +1,14 @@
package com.coder.client.file;
public abstract class ProjectFileObject{
private String name;
public ProjectFileObject(String name){
this.name = name;
}
public String getName(){
return name;
}
}

View file

@ -0,0 +1,124 @@
package com.coder.client.file;
public class ProjectFileTree {
private ProjectDirectory root;
public ProjectFileTree(){
this.root = new ProjectDirectory("/");
}
public void printFileTree(){
if(root != null){
printDirectoryContent(root, 0);
}else{
System.out.println("File tree not set");
}
}
private void printDirectoryContent(ProjectDirectory dir, int indentation){
for(ProjectFileObject obj : dir.getAll()){
if(obj instanceof ProjectDirectory){
printIndentation(indentation);
System.out.println("DIR:"+obj);
printDirectoryContent((ProjectDirectory) obj, indentation+1);
}
}
for(ProjectFileObject obj : dir.getAll()){
if(obj instanceof ProjectFile){
printIndentation(indentation);
System.out.println("FILE:"+obj);
}
}
}
private void printIndentation(int indentation){
for(int i = 0; i < indentation; ++i){
System.out.print(" ");
}
}
public boolean parseFileList(String[] filePaths){
ProjectDirectory root = new ProjectDirectory("/");
for(String filePath : filePaths){
if(filePath.endsWith("/")){
System.out.println("SEVERE: file path is not pointing to a file: " + filePath);
this.root = null;
return false;
}
String[] tmp;
if(filePath.startsWith("/")){
tmp = filePath.substring(1, filePath.length()).split("/");
}else{
tmp = filePath.split("/");
}
int i;
ProjectDirectory tmpRoot = root;
for(i = 0; i < tmp.length-1; ++i){
String directoryName = tmp[i];
if(directoryName.isEmpty()){
System.out.println("SEVERE: a directory name cannot be empty");
this.root = null;
return false;
}
if(tmpRoot.containsDirectory(directoryName) == false){
tmpRoot.addDirectory(directoryName);
}else{
//directory already exist
}
tmpRoot = tmpRoot.getDirectory(directoryName);
}
String fileName = tmp[i];
if(tmpRoot.containsDirectory(fileName)){
//logger.sever("File list contains a directory and file with the same name");
System.out.println("SEVERE: File list contains a directory and file with the same name");
this.root = null;
return false;
}else if(tmpRoot.containsFile(fileName) == false){
ProjectFile file = new ProjectFile(fileName, filePath);
tmpRoot.addFile(file);
}else{
//file already exists
}
}
updateCurrentFileList(root, this.root);
return true;
}
private void updateCurrentFileList(ProjectDirectory sourceRoot, ProjectDirectory targetRoot){
//remove files in target that does not exist in source
for(ProjectFileObject targetFileObject : targetRoot.getAll()){
String fileObjectName = targetFileObject.getName();
if(targetFileObject instanceof ProjectFile){ //target file is a file
if(sourceRoot.containsFile(fileObjectName) == false){
targetRoot.deleteFile(fileObjectName);
targetFileObject = null;
}
}else if(targetFileObject instanceof ProjectDirectory){ //target file is a directory
if(sourceRoot.containsDirectory(fileObjectName) == false){
targetRoot.deleteDirectory(fileObjectName);
targetFileObject = null;
}
}
}
//add files to target that exist in source
for(ProjectFileObject sourceFileObject : sourceRoot.getAll()){
String fileObjectName = sourceFileObject.getName();
if(sourceFileObject instanceof ProjectFile){ //source file is a file
if(targetRoot.containsFile(fileObjectName) == false){
ProjectFile file = (ProjectFile)sourceFileObject;
targetRoot.addFile(file);
}
}else if(sourceFileObject instanceof ProjectDirectory){ //source file is a directory
if(targetRoot.containsDirectory(fileObjectName) == false){
targetRoot.addDirectory(fileObjectName);
}
//recursive
ProjectDirectory dir = (ProjectDirectory)sourceFileObject;
updateCurrentFileList(dir, targetRoot.getDirectory(fileObjectName));
}
}
}
}

View file

@ -0,0 +1,37 @@
package com.coder.client.file;
public class ProjectFileTreeTest {
ProjectFileTree tree = new ProjectFileTree();
public ProjectFileTreeTest(){
tree.printFileTree();
tree.parseFileList(new String[]{"/test"});
tree.printFileTree();
tree.parseFileList(new String[]{"/test", "/test/test"});
tree.printFileTree();
tree.parseFileList(new String[]{"/test/test/"});
tree.printFileTree();
tree.parseFileList(new String[]{"/test/"});
tree.printFileTree();
tree.parseFileList(new String[]{"/"});
tree.printFileTree();
tree.parseFileList(new String[]{"//"});
tree.printFileTree();
tree.parseFileList(new String[]{"//test"});
tree.printFileTree();
}
public static void main(String[] args){
new ProjectFileTreeTest();
}
}