org.apache.tools.ant.types

## Class Resource

- java.lang.Object
- - org.apache.tools.ant.ProjectComponent
  - - org.apache.tools.ant.types.DataType
    - - org.apache.tools.ant.types.Resource

All Implemented Interfaces:
  java.lang.Cloneable, java.lang.Comparable<Resource>, java.lang.Iterable<Resource>, ResourceCollection

Direct Known Subclasses:
  AbstractClasspathResource, ArchiveResource, FileResource, LogOutputResource, PropertyResource, ResourceDecorator, StringResource, URLResource

    public class Resource
    extends DataType
    implements java.lang.Comparable<Resource>, 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 Also:
  `Touchable`

### Field Summary

Fields
`protected static int` `MAGIC` Magic number
`static long` `UNKNOWN_DATETIME` Constant unknown datetime for getLastModified
`static long` `UNKNOWN_SIZE` Constant unknown size

### Fields inherited from class org.apache.tools.ant.types.DataType

`checked, ref`

### Fields inherited from class org.apache.tools.ant.ProjectComponent

`description, location, project`

### Constructor Summary

Constructors
Constructor and Description
`Resource()` Default constructor.
`Resource(java.lang.String name)` Only sets the name.
`Resource(java.lang.String name, boolean exists, long lastmodified)` Sets the name, lastmodified flag, and exists flag.
`Resource(java.lang.String name, boolean exists, long lastmodified, boolean directory)` Sets the name, lastmodified flag, exists flag, and directory flag.
`Resource(java.lang.String name, boolean exists, long lastmodified, boolean directory, long size)` Sets the name, lastmodified flag, exists flag, directory flag, and size.

### Method Summary

All Methods Static Methods Instance Methods Concrete Methods
`<T> T` `as(java.lang.Class<T> clazz)` Returns a view of this resource that implements the interface given as the argument or null if there is no such view.
`<T> java.util.Optional<T>` `asOptional(java.lang.Class<T> clazz)` Return `as(Class)` as an `Optional`.
`java.lang.Object` `clone()` Clone this Resource.
`int` `compareTo(Resource other)` Delegates to a comparison of names.
`boolean` `equals(java.lang.Object other)` Implement basic Resource equality.
`protected Resource` `getCheckedRef()` Performs the check for circular references and returns the referenced object.
`java.io.InputStream` `getInputStream()` Get an InputStream for the Resource.
`long` `getLastModified()` Tells the modification time in milliseconds since 01.01.1970 (the "epoch").
`protected static int` `getMagicNumber(byte[] seed)` Create a "magic number" for use in hashCode calculations.
`java.lang.String` `getName()` 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.
`java.io.OutputStream` `getOutputStream()` Get an OutputStream for the Resource.
`long` `getSize()` Get the size of this Resource.
`int` `hashCode()` Get the hash code for this Resource.
`boolean` `isDirectory()` Tells if the resource is a directory.
`boolean` `isExists()` The exists attribute tells whether a resource exists.
`boolean` `isFilesystemOnly()` Fulfill the ResourceCollection contract.
`java.util.Iterator<Resource>` `iterator()` Fulfill the ResourceCollection contract.
`void` `setDirectory(boolean directory)` Set the directory attribute.
`void` `setExists(boolean exists)` Set the exists attribute.
`void` `setLastModified(long lastmodified)` Set the last modification attribute.
`void` `setName(java.lang.String name)` Set the name of this Resource.
`void` `setRefid(Reference r)` Overrides the base version.
`void` `setSize(long size)` Set the size of this Resource.
`int` `size()` Fulfill the ResourceCollection contract.
`java.lang.String` `toLongString()` Get a long String representation of this Resource.
`java.lang.String` `toString()` Get the string representation of this Resource.

### Methods inherited from class org.apache.tools.ant.types.DataType

`checkAttributesAllowed, checkChildrenAllowed, circularReference, dieOnCircularReference, dieOnCircularReference, dieOnCircularReference, getCheckedRef, getCheckedRef, getCheckedRef, getDataTypeName, getRefid, invokeCircularReferenceCheck, isChecked, isReference, noChildrenAllowed, pushAndInvokeCircularReferenceCheck, setChecked, tooManyAttributes`

### Methods inherited from class org.apache.tools.ant.ProjectComponent

`getDescription, getLocation, getProject, log, log, setDescription, setLocation, setProject`

### Methods inherited from class java.lang.Object

`finalize, getClass, notify, notifyAll, wait, wait, wait`

### Methods inherited from interface org.apache.tools.ant.types.ResourceCollection

`isEmpty, stream`

### Methods inherited from interface java.lang.Iterable

`forEach, spliterator`

### Field Detail

#### UNKNOWN_SIZE

    public static final long UNKNOWN_SIZE

Constant unknown size

See Also:
  Constant Field Values

#### UNKNOWN_DATETIME

    public static final long UNKNOWN_DATETIME

Constant unknown datetime for getLastModified

See Also:
  Constant Field Values

#### MAGIC

    protected static final int MAGIC

Magic number

### Constructor Detail

#### Resource

    public Resource()

Default constructor.

#### Resource

    public Resource(java.lang.String name)

Only sets the name.

This is a dummy, used for not existing resources.

Parameters:
  `name` - relative path of the resource. Expects "/" to be used as the directory separator.

#### Resource

    public Resource(java.lang.String name,
                    boolean exists,
                    long lastmodified)

Sets the name, lastmodified flag, and exists flag.

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

#### Resource

    public Resource(java.lang.String name,
                    boolean exists,
                    long lastmodified,
                    boolean directory)

Sets the name, lastmodified flag, exists flag, and directory flag.

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

#### Resource

    public Resource(java.lang.String name,
                    boolean exists,
                    long lastmodified,
                    boolean directory,
                    long size)

Sets the name, lastmodified flag, exists flag, directory flag, and size.

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

### Method Detail

#### getMagicNumber

    protected static int getMagicNumber(byte[] seed)

Create a "magic number" for use in hashCode calculations.

Parameters:
  `seed` - byte[] to seed with.
Returns:
  a magic number as int.

#### getName

    public java.lang.String getName()

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.

Returns:
  the name of this resource.

#### setName

    public void setName(java.lang.String name)

Set the name of this Resource.

Parameters:
  `name` - relative path of the resource. Expects "/" to be used as the directory separator.

#### isExists

    public boolean isExists()

The exists attribute tells whether a resource exists.

Returns:
  true if this resource exists.

#### setExists

    public void setExists(boolean exists)

Set the exists attribute.

Parameters:
  `exists` - if true, this resource exists.

#### getLastModified

    public long getLastModified()

Tells the modification time in milliseconds since 01.01.1970 (the "epoch").

Returns:
  the modification time, if that is meaningful (e.g. for a file resource which exists); 0 if the resource does not exist, to mirror the behavior of `File.lastModified()`; or 0 if the notion of modification time is meaningless for this class of resource (e.g. an inline string)

#### setLastModified

    public void setLastModified(long lastmodified)

Set the last modification attribute.

Parameters:
  `lastmodified` - the modification time in milliseconds since 01.01.1970.

#### isDirectory

    public boolean isDirectory()

Tells if the resource is a directory.

Returns:
  boolean flag indicating if the resource is a directory.

#### setDirectory

    public void setDirectory(boolean directory)

Set the directory attribute.

Parameters:
  `directory` - if true, this resource is a directory.

#### setSize

    public void setSize(long size)

Set the size of this Resource.

Parameters:
  `size` - the size, as a long.
Since:
  Ant 1.6.3

#### getSize

    public long getSize()

Get the size of this Resource.

Returns:
  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

#### clone

    public java.lang.Object clone()

Clone this Resource.

Overrides:
  `clone` in class `DataType`
Returns:
  copy of this.

#### compareTo

    public int compareTo(Resource other)

Delegates to a comparison of names.

Specified by:
  `compareTo` in interface `java.lang.Comparable<Resource>`
Parameters:
  `other` - the object to compare to.
Returns:
  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

#### equals

    public boolean equals(java.lang.Object other)

Implement basic Resource equality.

Overrides:
  `equals` in class `java.lang.Object`
Parameters:
  `other` - the object to check against.
Returns:
  true if the specified Object is equal to this Resource.
Since:
  Ant 1.7

#### hashCode

    public int hashCode()

Get the hash code for this Resource.

Overrides:
  `hashCode` in class `java.lang.Object`
Returns:
  hash code as int.
Since:
  Ant 1.7

#### getInputStream

    public java.io.InputStream getInputStream()
                                       throws java.io.IOException

Get an InputStream for the Resource.

Returns:
  an InputStream containing this Resource's content.
Throws:
  `java.io.IOException` - if unable to provide the content of this Resource as a stream.
  `java.lang.UnsupportedOperationException` - if InputStreams are not supported for this Resource type.
Since:
  Ant 1.7

#### getOutputStream

    public java.io.OutputStream getOutputStream()
                                         throws java.io.IOException

Get an OutputStream for the Resource.

Returns:
  an OutputStream to which content can be written.
Throws:
  `java.io.IOException` - if unable to provide the content of this Resource as a stream.
  `java.lang.UnsupportedOperationException` - if OutputStreams are not supported for this Resource type.
Since:
  Ant 1.7

#### iterator

    public java.util.Iterator<Resource> iterator()

Fulfill the ResourceCollection contract.

Specified by:
  `iterator` in interface `java.lang.Iterable<Resource>`
Returns:
  an Iterator of Resources.
Since:
  Ant 1.7

#### size

    public int size()

Fulfill the ResourceCollection contract.

Specified by:
  `size` in interface `ResourceCollection`
Returns:
  the size of this ResourceCollection.
Since:
  Ant 1.7

#### isFilesystemOnly

    public boolean isFilesystemOnly()

Fulfill the ResourceCollection contract.

Specified by:
  `isFilesystemOnly` in interface `ResourceCollection`
Returns:
  whether this Resource is a FileProvider.
Since:
  Ant 1.7

#### toString

    public java.lang.String toString()

Get the string representation of this Resource.

Overrides:
  `toString` in class `DataType`
Returns:
  this Resource formatted as a String.
Since:
  Ant 1.7

#### toLongString

    public final java.lang.String toLongString()

Get a long String representation of this Resource. This typically should be the value of `toString()` prefixed by a type description.

Returns:
  this Resource formatted as a long String.
Since:
  Ant 1.7

#### setRefid

    public void setRefid(Reference r)

Overrides the base version.

Overrides:
  `setRefid` in class `DataType`
Parameters:
  `r` - the Reference to set.

#### as

    public <T> T as(java.lang.Class<T> clazz)

Returns a view of this resource that implements the interface given as the argument or null if there is no such view.

This allows extension interfaces to be added to resources without growing the number of permutations of interfaces decorators/adapters need to implement.

This implementation of the method will return the current instance itself if it can be assigned to the given class.

Type Parameters:
  `T` - desired type
Parameters:
  `clazz` - a class
Returns:
  resource of a desired type
Since:
  Ant 1.8.0

#### asOptional

    public <T> java.util.Optional<T> asOptional(java.lang.Class<T> clazz)

Return `as(Class)` as an `Optional`.

Type Parameters:
  `T` - desired type
Parameters:
  `clazz` - a class
Returns:
  `Optional` resource of a desired type
Since:
  Ant 1.10.2

#### getCheckedRef

    protected Resource getCheckedRef()

Description copied from class: `DataType`
Performs the check for circular references and returns the referenced object.

Overrides:
  `getCheckedRef` in class `DataType`
Returns:
  the dereferenced object.