/*
Dateiverwaltung - File management in your browser
Copyright (C) 2017 Ulrich Hilger, http://uhilger.de
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see .
*/
package de.uhilger.filecms.data;
import java.io.Serializable;
/**
* A reference to a file consisting of the file's absolute path and additional
* information about whether or not the referenced file is a directory, is hidden, etc.
* Note that FileRef only references a file, the file itself and its contents are not
* modelled by this class.
*
* @author Ulrich Hilger, http://uhilger.de
* @author Bereitgestellt unter den Bedingungen der
* GNU Affero
* General Public License
*
* @version 2, 12.01.2008
*/
public class FileRef implements Serializable {
public static final long serialVersionUID = 42L;
private String absolutePath;
private Boolean isDirectory;
private Boolean isHidden;
private Long lastModified;
private Long length;
private String mimetype;
/**
* create a new instance of FileRef
. Note that the created FileRef is
* only a reference to a file, the file itself will not be created.
*
* @param absolutePath the absolute path that denotes this instance of FileRef
* @param isDirectory whether or not this file is a directory
* @param isHidden whether or not this file is a hidden file
* @param lastModified the date/time this file was last modified measured in milliseconds
* since the epoch (00:00:00 GMT, January 1, 1970)
* @param length the length of this file in bytes
*/
public FileRef(String absolutePath, boolean isDirectory, boolean isHidden,
long lastModified, long length)
{
super();
this.absolutePath = absolutePath;
this.isDirectory = isDirectory;
this.isHidden = isHidden;
this.lastModified = lastModified;
this.length = length;
}
/**
* Create a new instance of class FileRef
with a given absolute path.
* Other characteristics of the file are created with default settings
* (i.e. not a directory, not hidden, unknown modification date
* and length). Note that the created FileRef is only a reference to a file, the file
* itself will not be created.
*
* @param absolutePath the absolute path that denotes this instance
* of FileRef
*/
public FileRef(String absolutePath) {
this(absolutePath, false, false, 0, 0);
}
/**
* Create a new instance of class FileRef
with a given absolute path
* and an indicator whether or not the new FileRef denotes a directory.
* Other characteristics of the file are created with default settings
* (i.e. not hidden, unknown modification date and length). Note that the created
* FileRef is only a reference to a file, the file itself will not be created.
*
* @param absolutePath the absolute path that denotes this instance
* of FileRef
* @param isDirectory true, if the file to create should denote a directory
*/
public FileRef(String absolutePath, boolean isDirectory) {
this(absolutePath, isDirectory, false, 0, 0);
}
public String getMimetype() {
return mimetype;
}
public void setMimetype(String mimetype) {
this.mimetype = mimetype;
}
/**
* get the absolute path that denotes this file
* @return the path
*/
public String getAbsolutePath() {
return absolutePath;
}
/**
* Tests whether the file denoted by this abstract pathname is a
* directory.
*
* @return true
if this file is a directory,
* false
otherwise
*/
public boolean isDirectory() {
return isDirectory;
}
/**
* Tests whether the file denoted by this abstract pathname is a
* file.
*
* @return true
if this file is a file,
* false
otherwise
*/
public boolean isFile() {
return !isDirectory;
}
/**
* Tests whether the file denoted by this abstract pathname is a
* hidden file.
*
* @return true
if this file is a hidden file,
* false
otherwise
*/
public boolean isHidden() {
return isHidden;
}
/**
* Returns the time that this file was
* last modified.
*
* @return A long
value representing the time the file was
* last modified, measured in milliseconds since the epoch
* (00:00:00 GMT, January 1, 1970)
*/
public long getLastModified() {
return lastModified;
}
/**
* Returns the length of this file.
* The return value is unspecified if this file denotes a directory.
*
* @return The length, in bytes, of this file
*/
public long getLength() {
return length;
}
/**
* get the name of this file without path
* @param separatorChar the string that is used to separate directories and file names in
* path expressions
* @return the name of this file without path
*/
public String getName(String separatorChar) {
String path = getAbsolutePath();
String name = path.substring(path.lastIndexOf(separatorChar) + 1);
if(name == null || name.length() < 1) {
name = path;
}
return name;
}
/**
* get a string representation of this instance of FileRef
* @return the string representation of this FileRef object
*/
public String toString() {
return getAbsolutePath();
}
/**
* Indicates whether some object is equal to this instance of class FileRef. Two
* GenericFiles are regarded as equal when their absolute paths are equal case independently.
*
* @param obj the object to compare with this instance of FileRef
* @return true, if obj is equal to this instance of FileRef, false if not
*/
public boolean equals(Object obj) {
boolean isEqual = false;
if(obj != null && obj instanceof FileRef) {
isEqual = ((FileRef) obj).getAbsolutePath().toLowerCase().equals(
this.getAbsolutePath().toLowerCase());
}
return isEqual;
}
}