开发者

what data structure to use in my case?

开发者 https://www.devze.com 2023-03-08 07:17 出处:网络
A FileManager Class has a static filed to hold a file collection, this collection may contains files or folders or both , a folder may contains files or folders or both, the FileManager Class contains

A FileManager Class has a static filed to hold a file collection, this collection may contains files or folders or both , a folder may contains files or folders or both, the FileManager Class contains public method for client code to call such as addFile, addFolder, deleteFile, deleteFolder, these method operate on the collection.

My question is:

What java data structure is best for this case ?

How to create model class for File and Folder ?

some example will be good.

best regars.

// added @ 2011/05/27 thank everybody ! acutaly I am trying to buidl a eclipse-rcp application to manage some jdbc connection profile. here is my code:

package com.amarsoft.sysconfig.plugin.model;

/**
 * @author ggfan@amarsoft
 *
 */
public class TreeNode {

    /**
     * unique key
     */
    private String key;

    /**
     * used as 开发者_C百科label in a JFace TreeViewer, 
     */
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public String getKey() {
        return key;
    }


}

public class LeafNode extends TreeNode {

    private FolderNode parent;

    public void setParent(FolderNode parent) {
        this.parent = parent;
    }

    public TreeNode getParent() {
        return parent;
    }
}

package com.amarsoft.sysconfig.plugin.model;

import java.util.List;

public class FolderNode extends TreeNode {

    private List<TreeNode> children;

    public void setChildren(List<TreeNode> children) {
        this.children = children;
    }

    public List<TreeNode> getChildren() {
        return children;
    }
}




package com.amarsoft.sysconfig.plugin.model;

import org.dom4j.Element;
import org.dom4j.tree.DefaultElement;

/**
 * 连接配置模型类
 * @author ggfan@amarsoft
 *
 */
public class ConnectionProfile extends LeafNode{

    /**
     * url
     */
    private String url;

    /**
     * JDBC driver id
     */
    private int driver;

    /**
     * user name for logon
     */
    private String user;

    /**
     * password for logon
     */
    private String pswd;

    /**
     * default constructor
     */
    public ConnectionProfile() {

    }

    /**
     * construct a instance using a XML element
     * @param xmlElement the XML element
     */
    public ConnectionProfile(Element xmlElement){
        this.setName(xmlElement.attributeValue("name"));
        this.setUrl(xmlElement.element("url").getTextTrim());
        this.setUser(xmlElement.element("user").getTextTrim());
        this.setPswd(xmlElement.element("password").getTextTrim());
    }

    /**
     * serialize as XML
     * @return
     */
    public Element asXML(){
        Element e = new DefaultElement("profile");
        e.addAttribute("name", this.getName());
        e.addElement("url", escapeNull(this.getUrl()));
        e.addElement("user", escapeNull(this.getUser()));
        e.addElement("password", escapeNull(this.getPswd()));
        return e;
    }

    private String escapeNull(String s) {
        return s == null ? "" : s;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getPswd() {
        return pswd;
    }

    public void setPswd(String pswd) {
        this.pswd = pswd;
    }

    public void setDriver(int driver) {
        this.driver = driver;
    }

    public int getDriver() {
        return driver;
    }

}

public class ConnectionProfileManager {

private static List<TreeNode> profiles = new ArrayList<TreeNode>();

public static void loadProfiles() throws DocumentException{
    Element profiles = XMLUtil.readRoot(ConnectionProfileManager.class.getResourceAsStream("samples_profile.xml"));
    //Element profiles = XMLUtil.readRoot(new File(ApplicationFiles.CONNNECTION_PROFILES));
    if(profiles != null){
        for(Element profile : profiles.elements()){
            loadNode(profile, ConnectionProfileManager.profiles);
        }
    }
}


private static void loadNode(Element node, List<TreeNode> parent){
    if(node.getName().equals(XMLConstants.CP_TAG_PROFILE)){
        ConnectionProfile profile = new ConnectionProfile(node);
        parent.add(profile);
    }else if(node.getName().equals(XMLConstants.CP_TAG_FOLDER)){
        FolderNode folder = new FolderNode();
        folder.setChildren(new ArrayList<TreeNode>());
        folder.setName(node.attributeValue(XMLConstants.CP_ATTR_NAME));
        for(Element child : node.elements()){
            loadNode(child, folder.getChildren());
        }
        parent.add(folder);
    }
}

public static void saveProfiles(){
    Element root = new DefaultElement(XMLConstants.CP_TAG_PROFILES);
    for(TreeNode node : ConnectionProfileManager.profiles){
        saveNode(node, root);
    }

    XMLUtil.save(root, new File("c:\\1.xml"));
}

private static void saveNode(TreeNode node, Element root) {
    if(node instanceof ConnectionProfile){
        ConnectionProfile p = (ConnectionProfile)node;
        root.add(p.asXML());
    }else if(node instanceof FolderNode){
        FolderNode folder = (FolderNode)node;
        Element e = new DefaultElement(XMLConstants.CP_TAG_FOLDER);
        e.addAttribute(XMLConstants.CP_ATTR_NAME, node.getName());
        for(TreeNode child : folder.getChildren()){
            saveNode(child, e);
        }
        root.add(e);
    }
}


public static void addProfile(ConnectionProfile profile){
    profiles.add(profile);
}

public static void addProfile(TreeNode parentNode, ConnectionProfile profile){

}

public static List<TreeNode> getProfiles() {
    return profiles;
}

}

with these class I get my tree works, but I found It's hard to support add operation.


You've kinda of dictacted the answer already in the question..

The File class is (according to the JavaDocs) an:

abstract representation of file and directory pathnames.

So from what you've described:

 // A file manager class
 class FileManager {

     // has a static field to hold a file collection 
     static Collection<File> fileCollection;

     // contains public methods such as
     public addFile(File f) { }
     public deleteFile(File f) { }
     public addFolder(File f) { }
     public deleteFolder(File f { }
 }

If you have to look at implementing your own version of the File class, then the JavaDocs for that should be a good start to understanding this.

AS to what collection is best for the file collection, I think a Set makes most sense. There's no point having more than one file (e.g. a List and two entries of the same file would be meaningless), and testing membership of a set is a very quick operation. For example, in addFile you might check it exists before trying to add, and similarly for delete you'd want to make sure that it exists before you delete it.

A couple of points about the design you've mentioned.

  1. Static fields like this as nasty. They make it difficult to test and are a pain for multi-threading. Could you make it an instance variable?

  2. Given that File is an abstract representation of a path name, why'd you need the method addFile and addFolder, they are going to have the same implementation?


A Collection of Files?

Most collections support add and delete, so there is no need for a special data structure. Java File can be a file and a directory. Simply call isDirectory to find out if it is a directory or a file.

Unless you have more requirements, I think this would make up a pretty easy to use FileManager:

List<File> fileManager = new ArrayList<File>();
0

精彩评论

暂无评论...
验证码 换一张
取 消