package org.sugarj.common.path;

import java.io.File;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;

import org.sugarj.common.Environment;

/**
 * @author Sebastian Erdweg <seba at informatik uni-marburg de>
 */
public class RelativePath extends Path {
  private String relativePath;
  private Path base;
    
  /**
   *  For externalization only.
   */
  public RelativePath() { }
  
  public RelativePath(String relativePath) {
    this(null, relativePath);
  }
  
  public RelativePath(Path base, String relativePath) {
    this.relativePath = trimFront(trimBack(relativePath)).replace(File.separatorChar, '/');
    this.base = base;
    
    if (relativePath == null || relativePath.isEmpty())
      throw new IllegalArgumentException();
  }
  
  public String getRelativePath() {
    return relativePath;
  }
  
  public void setBasePath(Path base) {
    this.base = base;
  }
  
  public Path getBasePath() {
    return base;
  }

  @Override
  public String getAbsolutePath() {
    if (getBasePath() == null)
      throw new IllegalStateException("Base of relative path is still open; cannot construct absolute path yet.");
      
    return getBasePath() + Environment.sep + getRelativePath();
  }

  @Override
  public void writeExternal(ObjectOutput out) throws IOException {
    out.writeObject(base);
    out.writeObject(relativePath);
  }

  @Override
  public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
    base = (Path) in.readObject();
    relativePath = (String) in.readObject();
  }
}

@@ -1569,10 +1569,137 @@
 ();%0A  %7D%0A
+  %0A  @Override%0A  public String toString() %7B%0A    if (base == null)%0A      return relativePath;%0A    return getAbsolutePath();%0A  %7D%0A
 %7D%0A

fix resolution of dep files