package fitnesse.wiki.mem;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;

import fitnesse.wiki.fs.FileSystem;
import util.Clock;

public class MemoryFileSystem implements FileSystem {
    private final Hashtable<String, String> files = new Hashtable<String, String>();

    public void makeFile(String path, String content) {
        files.put(path, content);
    }

    public void makeDirectory(String path) {
        files.put(path, "");
    }

    public boolean exists(String path) {
        for (String filePath: files.keySet()) {
            if (filePath.startsWith(path)) return true;
        }
        return false;
    }

    public String[] list(String path) {
        ArrayList<String> result = new ArrayList<String>();
        for (String filePath: files.keySet()) {
            if (!filePath.startsWith(path)) continue;
            if (filePath.equals(path)) continue;
            String rest = filePath.substring(path.length() + 1);
            int size = rest.indexOf("/");
            if (size < 0) size = rest.length();
            String newPath = rest.substring(0, size);
            if (!result.contains(newPath)) result.add(newPath);
        }
        return result.toArray(new String[result.size()]);
    }

    public String getContent(String path) {
        return files.get(path);
    }

  @Override
  public void delete(String pathToDelete) {
    for (Iterator<String> iter = files.keySet().iterator(); iter.hasNext(); ) {
      String f = iter.next();
      if (f.startsWith(pathToDelete))
        iter.remove();
    }
  }

  @Override
  public long lastModified(String path) {
    return Clock.currentTimeInMillis();
  }
}

@@ -49,16 +49,39 @@
 ayList;%0A
+import java.util.Date;%0A
 import j
@@ -100,16 +100,16 @@
 htable;%0A
-
 import j
@@ -274,22 +274,23 @@
 String, 
-String
+Payload
 %3E files 
@@ -313,22 +313,23 @@
 String, 
-String
+Payload
 %3E();%0A%0A  
@@ -406,24 +406,32 @@
 t(path, 
+payload(
 content)
 ;%0A    %7D%0A
@@ -422,16 +422,17 @@
 content)
+)
 ;%0A    %7D%0A
@@ -505,11 +505,20 @@
 th, 
+payload(
 %22%22)
+)
 ;%0A  
@@ -1375,24 +1375,32 @@
 es.get(path)
+.payload
 ;%0A    %7D%0A%0A  @
@@ -1703,36 +1703,351 @@
 urn 
-Clock.currentTimeInMillis();
+files.get(path).lastModified;%0A  %7D%0A%0A  private Payload payload(String payload) %7B%0A    return new Payload(payload);%0A  %7D%0A%0A  private static class Payload %7B%0A    private final String payload;%0A    private final long lastModified;%0A%0A    private Payload(String payload) %7B%0A      this.payload = payload;%0A      this.lastModified = new Date().getTime();%0A    %7D
 %0A  %7D

Store last modified date on Memory filesystem