Necrozze
Necrozze

Reputation: 61

Adding unnumbered amount of objects

I have a problem that I can't get around how to solve, I'm currently working on a mini filesystem wich requiers to create an massive amount of objects (in theory). I currently tried ArrayList<INode> nodes = new ArrayList<INode>(); and then when I add an object(INode) nodes.add(new INodeDirectory(paths[i]));

But when I check later if this node excist in nodesit doesent, any suggestions on how to solve this or what to use instead?

MiniFs class:

package se.kth.id1020.minifs;

import edu.princeton.cs.introcs.StdOut;
import java.util.HashMap;

public class MiniFs implements FileSystem {

  private final INodeDirectory root;
  private HashMap<String,Integer> map = new HashMap<String, Integer>();
  private int n = 0; //Number of objects created
  priate ArrayList<INode> nodes = new ArrayList<INode>();

  public MiniFs() {
    root = new INodeDirectory("/");
    map.put("/",n);
  }
  //Create a directory/folder
  @Override
  public void mkdir(String path) {
    String paths[] = path.split("/");
    if(paths.length == 2){
        nodes.add(new INode<directory(paths[paths.length - 1]));
        n++;
        map.put(paths[1],n);
        StdOut.println("OK.");
    }
    else{
        for(int i = 1; i < paths.length; i++){
            if(i == paths.length - 1){
                if(map.containsKey(paths[i])){
                    StdOut.println("Directory already excists");
                }
                else{
                    nodes.add(new INodeDirectory(paths[i]));
                    n++;
                    map.put(paths[i],n);
                    StdOut.println("OK.");
                }
            }
            else if(map.containsKey(paths[i]) == false){
                throw new UnsupportedOperationException("Error: you have to create " + paths[i] + " first!");
            }
        }
    }
  }
  //Create a file
  @Override
  public void touch(String path) {
    String paths[] = path.split("/");
    if(paths.length == 2){
        nodes.add(new INodeFile(paths[paths.length - 1]));
        n++;
        map.put(paths[paths.length - 1], n);
        StdOut.println("OK.");
    }
    else{
        for(int i = 1; i < paths.length; i++){
            if(i == paths.length - 1){
                if(map.containsKey(paths[i])){
                    StdOut.println("File already excists");
                } 
                else{
                    nodes.add(new INodeFile(paths[i]));
                    n++;
                    map.put(paths[i],n);
                    StdOut.println("OK.");
                }
            }
            else if(map.containsKey(paths[i]) == false){
                throw new UnsupportedOperationException("You have to create " + paths[i] + " first!");
            }
        }
    }
  }
  //Create a pointer to a file or directory
  @Override
  public void ln (String path, String target){
      String paths[] = path.split("/");
      String targets[] = target.split("/");
      if(paths.length == 2 && targets.length == 2 && map.containsKey(paths[1]) && map.containsKey(targets[1])){
          int pathIndex = nodes.indexOf(paths[1]);
          int targetIndex = nodes.indexOf(targets[1]);
          nodes.get(pathIndex).setPointer(nodes.get(targetIndex));
          StdOut.println("OK.");
      }
  }
}

INode class:

package se.kth.id1020.minifs;

public abstract class INode {

  private String name;
  private ArrayList<INode> pointer;
  private long accessTime;

  public INode(String name) {
    this.name = name;
    this.accessTime = System.currentTimeMillis();
  }

  public void setPointer(INode n) {
      pointer.add(n);
  }

  public long getAccessTime() {
    return accessTime;
  }

  public void setAccessTime(long accessTime) {
    this.accessTime = accessTime;
  }

  public String getName() {
    return name;
  }

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

INodeDirectory class:

package se.kth.id1020.minifs;

public class INodeDirectory extends INode {

  public INodeDirectory(String name) {
    super(name);
  }
}

INodeFile class:

package se.kth.id1020.minifs;

public class INodeFile extends INode {

  public INodeFile(String name) {
    super(name);
  }
}

Upvotes: 0

Views: 64

Answers (2)

chiastic-security
chiastic-security

Reputation: 20520

You need to override .equals() for anything that you're putting into an ArrayList and checking with .contains() or .indexOf().

It's very easy to get your IDE to help with this. I'd strongly recommend implementing .hashCode() as well, in case you want to use a HashMap later on (otherwise you'll hit a similar problem).

You have an even more serious problem, though, which is that when you check whether it's in there, you're passing a String to indexOf() rather than an INode. This will fail because the list doesn't store strings. If you want to check whether a node is there, you need to create a new node from the String, and check whether that's present.

Upvotes: 2

holtc
holtc

Reputation: 1820

You are trying to check if the ArrayList contains a certain instance of the INode class. However, you don't define how to compare tow INodes. ArrayList.contains() uses the INode class's default .equals() method, which will not actually work unless it is overriden using @Override in the INode class. If you are using Eclipse, you can easily generate the .equals() and .hashCode() functions by pressing Alt+Shift+S and selecting "Generate hashcode() and equals()

Upvotes: 0

Related Questions