// joi/7/jfiles/JFile.java // // // Copyright 2003 Bill Campbell and Ethan Bolker import java.util.Date; import java.io.File; /** * A JFile object models a file in a hierarchical file system. *
* Extend this abstract class to create particular kinds of JFiles,
* e.g.:
* Directory -
* a JFile that maintains a list of the files it contains.
* TextFile -
* a JFile containing text you might want to read.
*
* @see Directory
* @see TextFile
*
* @version 7
*/
public abstract class JFile
{
/**
* The separator used in pathnames.
*/
public static final String separator = File.separator;
private String name; // a JFile knows its name
private User owner; // the owner of this file
private Date createDate; // when this file was created
private Date modDate; // when this file was last modified
private Directory parent; // the Directory containing this file
/**
* Construct a new JFile, set owner, parent, creation and
* modification dates. Add this to parent (unless this is the
* root Directory).
*
* @param name the name for this file (in its parent directory).
* @param creator the owner of this new file.
* @param parent the Directory in which this file lives.
*/
protected JFile( String name, User creator, Directory parent )
{
this.name = name;
this.owner = creator;
this.parent = parent;
if (parent != null) {
parent.addJFile( name, this );
}
createDate = modDate = new Date(); // set dates to now
}
/**
* The name of the file.
*
* @return the file's name.
*/
public String getName()
{
return name;
}
/**
* The full path to this file.
*
* @return the path name.
*/
public String getPathName()
{
if (this.isRoot()) {
return separator;
}
if (parent.isRoot()) {
return separator + getName();
}
return parent.getPathName() + separator + getName();
}
/**
* The size of the JFile
* (as defined by the child class)..
*
* @return the size.
*/
public abstract int getSize();
/**
* Suffix used for printing file names
* (as defined by the child class).
*
* @return the file's suffix.
*/
public abstract String getSuffix();
/**
* Set the owner for this file.
*
* @param owner the new owner.
*/
public void setOwner( User owner )
{
this.owner = owner;
}
/**
* The file's owner.
*
* @return the owner of the file.
*/
public User getOwner()
{
return owner;
}
/**
* The date and time of the file's creation.
*
* @return the file's creation date and time.
*/
public String getCreateDate()
{
return createDate.toString();
}
/**
* Set the modification date to "now".
*/
protected void setModDate()
{
modDate = new Date();
}
/**
* The date and time of the file's last modification.
*
* @return the date and time of the file's last modification.
*/
public String getModDate()
{
return modDate.toString();
}
/**
* The Directory containing this file.
*
* @return the parent directory.
*/
public Directory getParent()
{
return parent;
}
/**
* A JFile whose parent is null is defined to be the root
* (of a tree).
*
* @return true when this JFile is the root.
*/
public boolean isRoot()
{
return (parent == null);
}
/**
* How a JFile represents itself as a String.
* That is,
*
* owner size modDate name+suffix ** * @return the String representation. */ public String toString() { return getOwner() + "\t" + getSize() + "\t" + getModDate() + "\t" + getName() + getSuffix(); } }