package com.glezo.abstractFileSystem;

import java.util.ArrayList;

import com.glezo.generalizedList.GeneralizedList;
import com.glezo.generalizedList.GeneralizedListNode;

/*
	so what on earth is this?
	in quite some occasions I have needed an abstract representation of different file-system(ish)
	e.g.: mtp devices, ptp devices, exploring a remote ext4 after getting a shell, etc. 

	
*/

public class AbstractFileSystem 
{
	private GeneralizedList<AbstractFileSystemEntry>	root_node;
	
	
	public AbstractFileSystem(AbstractFileSystemEntry root)
	{
		this.root_node	=	new GeneralizedList<>();
		GeneralizedListNode<AbstractFileSystemEntry>	root_node_node=new GeneralizedListNode<AbstractFileSystemEntry>(true,root,null,null);
		this.root_node.set_root_node(root_node_node);
	}
	//-----------------------------------------------------------------------------------------------
	public void add_entry(AbstractFileSystemEntry e)
	{
		String parts[]=e.getPath().split("/");
		GeneralizedListNode<AbstractFileSystemEntry> pointer=this.root_node.get_root_node();
		for(int i=1;i<parts.length;i++)
		{
			String current_folder=parts[i];
			if(i+1==parts.length)
			{
				//need to add
				boolean is_dir=e.isDirectory();
				GeneralizedListNode<AbstractFileSystemEntry> new_node=new GeneralizedListNode<AbstractFileSystemEntry>(is_dir,e,null,pointer);
				if(pointer.get_son()==null)
				{
					pointer.set_son(new_node);
				}
				else
				{
					pointer=pointer.get_son();
					while(pointer.get_next_brother()!=null)
					{
						pointer=pointer.get_next_brother();
					}
					pointer.set_next_brother(new_node);
				}
			}
			else
			{
				//follow the path
				pointer=pointer.get_son();
				boolean finished=false;
				while(!finished)
				{
					if(pointer.get_data().getName().equals(current_folder))
					{
						finished=true;
					}
					else
					{
						pointer=pointer.get_next_brother();
					}
				}
			}
		}
	}
	//-----------------------------------------------------------------------------------------------
	//debug, mostly
	public String toString()
	{
		String result="";
		ArrayList<AbstractFileSystemEntry> in_order=this.root_node.in_order();
		for(int i=0;i<in_order.size();i++)
		{
			result+="\n"+in_order.get(i).getPath();
		}
		//remove first, dummy \n
		result=result.substring(1);
		return result;
	}
	//-----------------------------------------------------------------------------------------------
	 
}

@@ -379,18 +379,16 @@
  etc. %0A%0A
-%09%0A
 */%0A%0Apubl
@@ -480,16 +480,100 @@
 _node;%0A%09
+private int%09%09%09%09%09%09%09%09%09%09%09number_of_files;%0A%09private int%09%09%09%09%09%09%09%09%09%09%09number_of_directories;
 %0A%09%0A%09publ
@@ -852,16 +852,321 @@
 _node);%0A
+%09%09this.number_of_directories=0;%0A%09%09this.number_of_files=0;%0A%09%7D%0A%09//-----------------------------------------------------------------------------------------------%0A%09public int%09get_number_of_files()%09%09%7B%09return this.number_of_files;%09%09%7D%0A%09public int%09get_number_of_directories()%09%7B%09return this.number_of_directories;
 %09%7D%0A%09//--
@@ -1308,24 +1308,125 @@
 Entry e)%0A%09%7B%0A
+%09%09if(e.is_directory())%09%7B%09this.number_of_directories++;%09%7D%0A%09%09else%09%09%09%09%09%7B%09this.number_of_files++;%09%09%09%7D%0A%09%09%0A
 %09%09String par
@@ -1431,25 +1431,26 @@
 arts%5B%5D=e.get
-P
+_p
 ath().split(
@@ -1690,9 +1690,10 @@
 e.is
-D
+_d
 irec
@@ -2258,17 +2258,18 @@
 ta().get
-N
+_n
 ame().eq
@@ -2703,17 +2703,29 @@
 lt+=%22%5Cn%22
-+
+;%0A%09%09%09result+=
 in_order
@@ -2736,15 +2736,16 @@
 (i).
-getPath
+toString
 ();%0A

Update AbstractFileSystem.java