package basics.filesystem.file;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import basics.application.Binder;
import basics.testing.Test;
import basics.utl.FormatterUtl;

public class FileInfo extends File
{
private static final long serialVersionUID = 1L;

public FileInfo(String pathname)
{
   super(pathname);
}

public FileInfo(File file)
{
   super(file.getAbsolutePath());
}

public String getNameOnly()
{
   return FileUtl.getNameOnly(this.getAbsolutePath());
}

public String getFormattedSize()
{
   return FormatterUtl.formatBytesPadLeft(super.length(), 3);
}

public String getFormattedDate()
{
   Date d = new Date(super.lastModified());
   SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd:HHmm");
   return sdf.format(d);
}

public String toString()
{
   StringBuilder s = new StringBuilder();
   s.append(getNameOnly());
   s.append(", ");
   s.append(getFormattedSize());
   s.append(", ");
   s.append(getFormattedDate());
   return s.toString();
}

public static void unittest()
{
   File f = new File(Binder.getBasedir(), "work/a.txt");
   FileInfo fi = new FileInfo(f.getAbsoluteFile());
   Test.assertEqualsTrue(fi.getNameOnly(), "a", "a is not " + fi.getNameOnly() + " ? "
      + f.getAbsolutePath());
}
}
