3

i am doing programming in GWT

i have class FolderColection and Folder, in FolderColection Class there is Hashmap hm

public class folderCollection{

  public Map<String,Folder>FolderCollection = new HashMap<String,Folder>();

  public void addFolder(Folder folder){
    String folderKey = folder.getKey();
    FolderCollection.put(folderKey, folder);
  }
}

In Folder class

public Class Folder{
  // Not a complete code , but an idea of a code 
  String name;
  String dateOfcreation;

  private FolderCollection  folderCollection = new FolderCollection();
  // Folder can also have many sub folders
  //More variables
  // all get set methods 
}

Now for example : all are folders

 1.A
    1.Aa
      1.Aa1
      2.Aa2
    2.Ab
 2.B
    1.Ba
    2.Bb
 3.C

A, B , C Folders are in FolderCollection. As A is also a folder and it contains FolderCollection (Folder Aa , Folder Ab). Similarly Folder Aa has FolderCollection of (Folder Aa1, Folder Aa2).

I able to make this which i have explained above.

I have difficulty in Accesing the object for example object of Folder Aa1. Suppose I want to change the name of Folder Aa2, for that i have to iterate till that object.

Please Help me to solve this.

i have path as all the folder names are added in tree widget accordint to the parentsTreeItem.

ex: if i have to change name of Aa2 then I have

 String [] path ={A,Aa,Aa2};

Some Help me with this will be of great Help.

dting
  • 38,604
  • 10
  • 95
  • 114
NewCodeLearner
  • 738
  • 3
  • 14
  • 38
  • 4
    If you have the path you don't have to _iterate_ through the hashmap, just get the value for each path element from the respective folder collection. – Thomas Jan 24 '12 at 14:58
  • 1
    @Thomas : exactly, but i think i have to write recursion code for it.But i am not able to do that.Please Can you tell me a small piece of code so that i will understand. – NewCodeLearner Jan 24 '12 at 15:16
  • 1
    btw, sort out your capitalisation of classes, methods and vars. Classes+interfaces should begin with upper case, methods and vars lowercase. – wmorrison365 Jan 24 '12 at 15:41

2 Answers2

1

I expect you're going to have to have a getFolder(?) on your Folder class that you provide the folder name to and it returns the sub-Folder with that name. You can use your path to traverse this in a loop to "cd" to the level you're interested in. Then, I guess a getFile will return a File object. You can then rename your file (though your path would have to be updated to reflect any Folder name changes).

Folder rootFolder = ...;
String[] path = ...;
Folder f = null;
for (int i=0; i<path.length; i++) {
    folder = rootFolder.getFolder(path[i]);
}

// f now contains the folder at "path"
// N.B. Haven't handled FolderNotFoundException
File file = f.getFile("Aa2.txt");
file.setName("Aa2.new.txt");

/* Folder#getFolder returning sub-folder for name passed */
public Folder getFolder(String name) {
    Folder f = folderCollection.get(name);    //no null checks done! 
    if (f == null) {
        ... //FolderNotFoundException?
    }
    return f;
}

Alternatively, you could provide the path (or an encapsulated representation of path) to Folder#getFolderInPath and handle it all internally to Folder. This version is iterative, looping over the path array, updating f with that level in the folder path on each iteration. It uses getFolder from the above:

Folder rootFolder = ...;
String[] path = ...;
Folder f = rootFolder.getFolderInPath(path);
File file = f.getFile("Aa2.txt");

/* Folder#getFolder returning sub-folder for name passed 
 * This is an "iterative" implementation - looping path array
 */
public Folder getFolderInPath(String[] path) {
    Folder f = null;
    for (int i=0; i<path.length; i++) {
        folder = this.getFolder(path[i]);
    }
    // f now contains the folder at "path"
    // N.B. Haven't handled FolderNotFoundException
    return f;
}

This is a recursive version of the above and uses getFolder (above x 2) again. Needs some explaining - #getFolderInPath now just delegates to the recursive method #getFolderRecursive, passing the root values of the recursion (the starting folder ["this"] and the start point in path - index).

Method #getFolderRecursive(Folder recursively calls itself until the path is traversed (while index < path.length). When it is traversed successfully (FolderNotfoundException?) then index = path.length. At each level, we make the recursive call with the folder found matching the name at that level in the path (governed by index). At the bottom level, the folder found with #getFolder is the last in the path and should be returned. The recursive methods are then unravelled, passing f up the tree in var folderArtPath (for clarity). The else condition sets this variable when the path is traversed and it is carried up the stack in the line folderAtPath = in the if block:

Folder rootFolder = ...;
String[] path = ...;
Folder f = rootFolder.getFolderInPath(path);
File file = f.getFile("Aa2.txt");

/* Folder#getFolder returning sub-folder for name passed 
 * This is an "recursive" implementation - digging into the Folder structure
 */
public Folder getFolderInPath(String[] path) {
    //Begin recursion with "this2 folder"
    return getFolderRecursive(this, path, 0);
}

/* Internal recursive method
 */
private Folder getFolderRecursive(Folder baseFolder, String[] path, int index) {

    Folder folderAtPath = null; //This is going to carry the Folder at "path"

    if (index < path.length) {  //Recursive base condition (are we done?)

        //Get folder f with name according to path and index
        Folder f = baseFolder.getFolder(path[index]));    //FolderNotFoundException?

        //Recursively call found folder f with path and index referring
        //to next path-part to be used (index+1)
        folderAtPath = getFolderRecursive(f, path, index+1);
    }
    else {
        folderAtPath = baseFolder;
    }

    return folderAtPath;
}

There may be a better way to do this but I can't examine it now. Spent a bit too much time on it but had to correct my mini-cook-up. Recursion's a bit of fun... find a simple example on line and just play with it.

You also may want to make Folder and File have some common interface.

wmorrison365
  • 5,995
  • 2
  • 27
  • 40
  • Corrected small prob and spent way too much time now. Whoops! – wmorrison365 Jan 24 '12 at 16:15
  • Thanks.But u didnt understood my question.. Folder class is user defined class. which is having a property of Folder like name , id.. etc.. this folder is saved in Hashmap of foldercolection and each Folder has its own floder collection. you can check Thomas's comment. he got it write. but i am not able to do it. – NewCodeLearner Jan 24 '12 at 16:31
  • 1
    Ok. Thomas is right, you don't need to iterate the FolderCollection, you just need to ask it for the value for the key which is the folder name (using `Map`'s `get(key)` function). However, you still need to provide a public-accessible method to `Folder` that enables you to access `folderCollection` map ivar. I've provided an example of this above in `Folder#getFolder`. See how it accesses your `folderCollection.get(name)`. The following two code-segments build on this idea to encapsulate the traversal of the whole path through method `#getFolderInPath`. There's an iterative and recursive e.g. – wmorrison365 Jan 24 '12 at 17:11
0

If you have the path you don't have to iterate through the hashmap, just get the value for each path element from the respective folder collection.

NewCodeLearner
  • 738
  • 3
  • 14
  • 38