FileDocCategorySizeDatePackage
Resource.javaAPI DocApache Ant 1.7013569Wed Dec 13 06:16:22 GMT 2006org.apache.tools.ant.types

Resource

public class Resource extends DataType implements Comparable, Cloneable, ResourceCollection
Describes a "File-like" resource (File, ZipEntry, etc.). This class is meant to be used by classes needing to record path and date/time information about a file, a zip entry or some similar resource (URL, archive in a version control repository, ...).
since
Ant 1.5.2
see
org.apache.tools.ant.types.resources.Touchable

Fields Summary
public static final long
UNKNOWN_SIZE
Constant unknown size
public static final long
UNKNOWN_DATETIME
Constant unknown datetime for getLastModified
protected static final int
MAGIC
Magic number
private static final int
NULL_NAME
private String
name
private Boolean
exists
private Long
lastmodified
private Boolean
directory
private Long
size
Constructors Summary
public Resource()
Default constructor.


           
      
    
public Resource(String name)
Only sets the name.

This is a dummy, used for not existing resources.

param
name relative path of the resource. Expects "/" to be used as the directory separator.

        this(name, false, 0, false);
    
public Resource(String name, boolean exists, long lastmodified)
Sets the name, lastmodified flag, and exists flag.

param
name relative path of the resource. Expects "/" to be used as the directory separator.
param
exists if true, this resource exists.
param
lastmodified the last modification time of this resource.

        this(name, exists, lastmodified, false);
    
public Resource(String name, boolean exists, long lastmodified, boolean directory)
Sets the name, lastmodified flag, exists flag, and directory flag.

param
name relative path of the resource. Expects "/" to be used as the directory separator.
param
exists if true the resource exists
param
lastmodified the last modification time of the resource
param
directory if true, this resource is a directory

        this(name, exists, lastmodified, directory, UNKNOWN_SIZE);
    
public Resource(String name, boolean exists, long lastmodified, boolean directory, long size)
Sets the name, lastmodified flag, exists flag, directory flag, and size.

param
name relative path of the resource. Expects "/" to be used as the directory separator.
param
exists if true the resource exists
param
lastmodified the last modification time of the resource
param
directory if true, this resource is a directory
param
size the size of this resource.

        this.name = name;
        setName(name);
        setExists(exists);
        setLastModified(lastmodified);
        setDirectory(directory);
        setSize(size);
    
Methods Summary
public java.lang.Objectclone()
Clone this Resource.

return
copy of this.

        try {
            return super.clone();
        } catch (CloneNotSupportedException e) {
            throw new UnsupportedOperationException(
                    "CloneNotSupportedException for a Resource caught. "
                    + "Derived classes must support cloning.");
        }
    
public intcompareTo(java.lang.Object other)
Delegates to a comparison of names.

param
other the object to compare to.
return
a negative integer, zero, or a positive integer as this Resource is less than, equal to, or greater than the specified Resource.
since
Ant 1.6

        if (isReference()) {
            return ((Comparable) getCheckedRef()).compareTo(other);
        }
        if (!(other instanceof Resource)) {
            throw new IllegalArgumentException(
                "Can only be compared with Resources");
        }
        return toString().compareTo(other.toString());
    
public booleanequals(java.lang.Object other)
Implement basic Resource equality.

param
other the object to check against.
return
true if the specified Object is equal to this Resource.
since
Ant 1.7

        if (isReference()) {
            return getCheckedRef().equals(other);
        }
        return other.getClass().equals(getClass()) && compareTo(other) == 0;
    
public java.io.InputStreamgetInputStream()
Get an InputStream for the Resource.

return
an InputStream containing this Resource's content.
throws
IOException if unable to provide the content of this Resource as a stream.
throws
UnsupportedOperationException if InputStreams are not supported for this Resource type.
since
Ant 1.7

        if (isReference()) {
            return ((Resource) getCheckedRef()).getInputStream();
        }
        throw new UnsupportedOperationException();
    
public longgetLastModified()
Tells the modification time in milliseconds since 01.01.1970 .

return
0 if the resource does not exist to mirror the behavior of {@link java.io.File File}.

        if (isReference()) {
            return ((Resource) getCheckedRef()).getLastModified();
        }
        if (!isExists() || lastmodified == null) {
            return UNKNOWN_DATETIME;
        }
        long result = lastmodified.longValue();
        return result < UNKNOWN_DATETIME ? UNKNOWN_DATETIME : result;
    
protected static intgetMagicNumber(byte[] seed)
Create a "magic number" for use in hashCode calculations.

param
seed byte[] to seed with.
return
a magic number as int.


                              
         
        return new BigInteger(seed).intValue();
    
public java.lang.StringgetName()
Name attribute will contain the path of a file relative to the root directory of its fileset or the recorded path of a zip entry.

example for a file with fullpath /var/opt/adm/resource.txt in a file set with root dir /var/opt it will be adm/resource.txt.

"/" will be used as the directory separator.

return
the name of this resource.

        return isReference() ? ((Resource) getCheckedRef()).getName() : name;
    
public java.io.OutputStreamgetOutputStream()
Get an OutputStream for the Resource.

return
an OutputStream to which content can be written.
throws
IOException if unable to provide the content of this Resource as a stream.
throws
UnsupportedOperationException if OutputStreams are not supported for this Resource type.
since
Ant 1.7

        if (isReference()) {
            return ((Resource) getCheckedRef()).getOutputStream();
        }
        throw new UnsupportedOperationException();
    
public longgetSize()
Get the size of this Resource.

return
the size, as a long, 0 if the Resource does not exist (for compatibility with java.io.File), or UNKNOWN_SIZE if not known.
since
Ant 1.6.3

        if (isReference()) {
            return ((Resource) getCheckedRef()).getSize();
        }
        return isExists()
            ? (size != null ? size.longValue() : UNKNOWN_SIZE)
            : 0L;
    
public inthashCode()
Get the hash code for this Resource.

return
hash code as int.
since
Ant 1.7

        if (isReference()) {
            return getCheckedRef().hashCode();
        }
        String name = getName();
        return MAGIC * (name == null ? NULL_NAME : name.hashCode());
    
public booleanisDirectory()
Tells if the resource is a directory.

return
boolean flag indicating if the resource is a directory.

        if (isReference()) {
            return ((Resource) getCheckedRef()).isDirectory();
        }
        //default false:
        return directory != null && directory.booleanValue();
    
public booleanisExists()
The exists attribute tells whether a file exists.

return
true if this resource exists.

        if (isReference()) {
            return ((Resource) getCheckedRef()).isExists();
        }
        //default true:
        return exists == null || exists.booleanValue();
    
public booleanisFilesystemOnly()
Fulfill the ResourceCollection contract.

return
whether this Resource is a FileResource.
since
Ant 1.7

        //default false:
        return isReference() && ((Resource) getCheckedRef()).isFilesystemOnly();
    
public java.util.Iteratoriterator()
Fulfill the ResourceCollection contract.

return
an Iterator of Resources.
since
Ant 1.7

        return isReference() ? ((Resource) getCheckedRef()).iterator()
            : new Iterator() {
            private boolean done = false;
            public boolean hasNext() {
                return !done;
            }
            public Object next() {
                if (done) {
                    throw new NoSuchElementException();
                }
                done = true;
                return Resource.this;
            }
            public void remove() {
                throw new UnsupportedOperationException();
            }
        };
    
public voidsetDirectory(boolean directory)
Set the directory attribute.

param
directory if true, this resource is a directory.

        checkAttributesAllowed();
        this.directory = directory ? Boolean.TRUE : Boolean.FALSE;
    
public voidsetExists(boolean exists)
Set the exists attribute.

param
exists if true, this resource exists.

        checkAttributesAllowed();
        this.exists = exists ? Boolean.TRUE : Boolean.FALSE;
    
public voidsetLastModified(long lastmodified)
Set the last modification attribute.

param
lastmodified the modification time in milliseconds since 01.01.1970.

        checkAttributesAllowed();
        this.lastmodified = new Long(lastmodified);
    
public voidsetName(java.lang.String name)
Set the name of this Resource.

param
name relative path of the resource. Expects "/" to be used as the directory separator.

        checkAttributesAllowed();
        this.name = name;
    
public voidsetRefid(Reference r)
Overrides the base version.

param
r the Reference to set.

        if (name != null
            || exists != null
            || lastmodified != null
            || directory != null
            || size != null) {
            throw tooManyAttributes();
        }
        super.setRefid(r);
    
public voidsetSize(long size)
Set the size of this Resource.

param
size the size, as a long.
since
Ant 1.6.3

        checkAttributesAllowed();
        this.size = new Long(size > UNKNOWN_SIZE ? size : UNKNOWN_SIZE);
    
public intsize()
Fulfill the ResourceCollection contract.

return
the size of this ResourceCollection.
since
Ant 1.7

        return isReference() ? ((Resource) getCheckedRef()).size() : 1;
    
public final java.lang.StringtoLongString()
Get a long String representation of this Resource. This typically should be the value of toString() prefixed by a type description.

return
this Resource formatted as a long String.
since
Ant 1.7

        return isReference() ? ((Resource) getCheckedRef()).toLongString()
            : getDataTypeName() + " \"" + toString() + '"";
    
public java.lang.StringtoString()
Get the string representation of this Resource.

return
this Resource formatted as a String.
since
Ant 1.7

        if (isReference()) {
            return getCheckedRef().toString();
        }
        String n = getName();
        return n == null ? "(anonymous)" : n;