java.lang.Cloneable
, java.lang.Comparable<Resource>
, java.lang.Iterable<Resource>
, ResourceCollection
AbstractClasspathResource
, ArchiveResource
, FileResource
, LogOutputResource
, PropertyResource
, ResourceDecorator
, StringResource
, URLResource
public class Resource extends DataType implements java.lang.Comparable<Resource>, ResourceCollection
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, ...).
Touchable
Modifier and Type | Field | Description |
---|---|---|
protected static int |
MAGIC |
Magic number
|
static long |
UNKNOWN_DATETIME |
Constant unknown datetime for getLastModified
|
static long |
UNKNOWN_SIZE |
Constant unknown size
|
description, location, project
Constructor | 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.
|
Modifier and Type | Method | Description |
---|---|---|
<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.
|
checkAttributesAllowed, checkChildrenAllowed, circularReference, dieOnCircularReference, dieOnCircularReference, dieOnCircularReference, getCheckedRef, getCheckedRef, getCheckedRef, getDataTypeName, getRefid, invokeCircularReferenceCheck, isChecked, isReference, noChildrenAllowed, pushAndInvokeCircularReferenceCheck, setChecked, tooManyAttributes
finalize, getClass, notify, notifyAll, wait, wait, wait
getDescription, getLocation, getProject, log, log, setDescription, setLocation, setProject
isEmpty, stream
public static final long UNKNOWN_SIZE
public static final long UNKNOWN_DATETIME
protected static final int MAGIC
public Resource()
public Resource(java.lang.String name)
This is a dummy, used for not existing resources.
name
- relative path of the resource. Expects
"/" to be used as the directory separator.public Resource(java.lang.String name, boolean exists, long lastmodified)
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.public Resource(java.lang.String name, boolean exists, long lastmodified, boolean directory)
name
- relative path of the resource. Expects
"/" to be used as the directory separator.exists
- if true the resource existslastmodified
- the last modification time of the resourcedirectory
- if true, this resource is a directorypublic Resource(java.lang.String name, boolean exists, long lastmodified, boolean directory, long size)
name
- relative path of the resource. Expects
"/" to be used as the directory separator.exists
- if true the resource existslastmodified
- the last modification time of the resourcedirectory
- if true, this resource is a directorysize
- the size of this resource.protected static int getMagicNumber(byte[] seed)
seed
- byte[] to seed with.public java.lang.String getName()
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.
public void setName(java.lang.String name)
name
- relative path of the resource. Expects
"/" to be used as the directory separator.public boolean isExists()
public void setExists(boolean exists)
exists
- if true, this resource exists.public long getLastModified()
File.lastModified()
;
or 0 if the notion of modification time is meaningless for this class
of resource (e.g. an inline string)public void setLastModified(long lastmodified)
lastmodified
- the modification time in milliseconds since 01.01.1970.public boolean isDirectory()
public void setDirectory(boolean directory)
directory
- if true, this resource is a directory.public void setSize(long size)
size
- the size, as a long.public long getSize()
public java.lang.Object clone()
public int compareTo(Resource other)
compareTo
in interface java.lang.Comparable<Resource>
other
- the object to compare to.public boolean equals(java.lang.Object other)
equals
in class java.lang.Object
other
- the object to check against.public int hashCode()
hashCode
in class java.lang.Object
public java.io.InputStream getInputStream() throws java.io.IOException
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.public java.io.OutputStream getOutputStream() throws java.io.IOException
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.public java.util.Iterator<Resource> iterator()
iterator
in interface java.lang.Iterable<Resource>
public int size()
size
in interface ResourceCollection
public boolean isFilesystemOnly()
isFilesystemOnly
in interface ResourceCollection
public java.lang.String toString()
public final java.lang.String toLongString()
toString()
prefixed by a type description.public void setRefid(Reference r)
public <T> T as(java.lang.Class<T> clazz)
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.
T
- desired typeclazz
- a classpublic <T> java.util.Optional<T> asOptional(java.lang.Class<T> clazz)
as(Class)
as an Optional
.T
- desired typeclazz
- a classOptional
resource of a desired typeprotected Resource getCheckedRef()
DataType
getCheckedRef
in class DataType