Package io.github.classgraph
Class ResourceList
java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractList<E>
java.util.ArrayList<T>
io.github.classgraph.ResourceList
- All Implemented Interfaces:
Serializable
,AutoCloseable
,Cloneable
,Iterable<Resource>
,Collection<Resource>
,List<Resource>
,RandomAccess
public class ResourceList extends ArrayList<T> implements AutoCloseable
An AutoCloseable list of AutoCloseable
Resource
objects.- See Also:
- Serialized Form
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
ResourceList.ByteArrayConsumer
AFunctionalInterface
for consuming the contents of aResource
as a byte array.static interface
ResourceList.ByteBufferConsumer
static interface
ResourceList.InputStreamConsumer
static interface
ResourceList.ResourceFilter
Filter aResourceList
using a predicate mapping aResource
object to a boolean, producing anotherResourceList
for all items in the list for which the predicate is true. -
Constructor Summary
Constructors Constructor Description ResourceList()
Create a new modifiable empty list ofResource
objects.ResourceList(int sizeHint)
Create a new modifiable empty list ofResource
objects, given a size hint.ResourceList(Collection<Resource> resourceCollection)
Create a new modifiable emptyResourceList
, given an initial collection ofResource
objects. -
Method Summary
Modifier and Type Method Description void
add(int index, T element)
boolean
add(T element)
boolean
addAll(int index, Collection<? extends T> c)
boolean
addAll(Collection<? extends T> c)
Map<String,ResourceList>
asMap()
Return thisResourceList
as a map from resource path (obtained fromResource.getPath()
) to aResourceList
ofResource
objects that have that path.ResourceList
classFilesOnly()
Return a newResourceList
consisting of only the resources with the filename extension ".class".void
clear()
void
close()
Close all theResource
objects in thisResourceList
.static ResourceList
emptyList()
Return an unmodifiable emptyResourceList
.boolean
equals(Object o)
ResourceList
filter(ResourceList.ResourceFilter filter)
Find the subset of theResource
objects in this list for which the given filter predicate is true.List<Map.Entry<String,ResourceList>>
findDuplicatePaths()
Find duplicate resource paths within thisResourceList
.void
forEachByteArray(ResourceList.ByteArrayConsumer byteArrayConsumer)
Fetch the content of eachResource
in thisResourceList
as a byte array, pass the byte array to the givenResourceList.ByteArrayConsumer
, then close the underlying InputStream or release the underlying ByteBuffer by callingResource.close()
.void
forEachByteArray(ResourceList.ByteArrayConsumer byteArrayConsumer, boolean ignoreIOExceptions)
Fetch the content of eachResource
in thisResourceList
as a byte array, pass the byte array to the givenResourceList.ByteArrayConsumer
, then close the underlying InputStream or release the underlying ByteBuffer by callingResource.close()
.void
forEachByteBuffer(ResourceList.ByteBufferConsumer byteBufferConsumer)
Read eachResource
in thisResourceList
as aByteBuffer
, pass theByteBuffer
to the givenResourceList.InputStreamConsumer
, then release theByteBuffer
after theResourceList.ByteBufferConsumer
returns, by callingResource.close()
.void
forEachByteBuffer(ResourceList.ByteBufferConsumer byteBufferConsumer, boolean ignoreIOExceptions)
Read eachResource
in thisResourceList
as aByteBuffer
, pass theByteBuffer
to the givenResourceList.InputStreamConsumer
, then release theByteBuffer
after theResourceList.ByteBufferConsumer
returns, by callingResource.close()
.void
forEachInputStream(ResourceList.InputStreamConsumer inputStreamConsumer)
Fetch anInputStream
for eachResource
in thisResourceList
, pass theInputStream
to the givenResourceList.InputStreamConsumer
, then close theInputStream
after theResourceList.InputStreamConsumer
returns, by callingResource.close()
.void
forEachInputStream(ResourceList.InputStreamConsumer inputStreamConsumer, boolean ignoreIOExceptions)
Fetch anInputStream
for eachResource
in thisResourceList
, pass theInputStream
to the givenResourceList.InputStreamConsumer
, then close theInputStream
after theResourceList.InputStreamConsumer
returns, by callingResource.close()
.ResourceList
get(String resourcePath)
Returns a list of all resources with the requested path.List<String>
getPaths()
Get the paths of all resources in this list relative to the package root.List<String>
getPathsRelativeToClasspathElement()
Get the paths of all resources in this list relative to the root of the classpath element.List<URI>
getURIs()
Get the URIs of all resources in this list, by callingResource.getURI()
for each item in the list.List<URL>
getURLs()
Get the URLs of all resources in this list, by callingResource.getURL()
for each item in the list.int
hashCode()
Iterator<T>
iterator()
ListIterator<T>
listIterator()
ResourceList
nonClassFilesOnly()
Return a newResourceList
consisting of non-classfile resources only.T
remove(int index)
boolean
remove(Object o)
boolean
removeAll(Collection<?> c)
boolean
retainAll(Collection<?> c)
T
set(int index, T element)
Methods inherited from class java.util.ArrayList
clone, contains, ensureCapacity, forEach, get, indexOf, isEmpty, lastIndexOf, listIterator, removeIf, replaceAll, size, sort, spliterator, subList, toArray, toArray, trimToSize
-
Constructor Details
-
ResourceList
public ResourceList()Create a new modifiable empty list ofResource
objects. -
ResourceList
public ResourceList(int sizeHint)Create a new modifiable empty list ofResource
objects, given a size hint.- Parameters:
sizeHint
- the size hint
-
ResourceList
Create a new modifiable emptyResourceList
, given an initial collection ofResource
objects.- Parameters:
resourceCollection
- the collection ofResource
objects.
-
-
Method Details
-
emptyList
Return an unmodifiable emptyResourceList
.- Returns:
- the unmodifiable empty
ResourceList
.
-
get
Returns a list of all resources with the requested path. (There may be more than one resource with a given path, from different classpath elements or modules, so this returns aResourceList
rather than a singleResource
.)- Parameters:
resourcePath
- The path of a resource- Returns:
- A
ResourceList
ofResource
objects in this list that have the given path (there may be more than one resource with a given path, from different classpath elements or modules, so this returns aResourceList
rather than a singleResource
.) Returns the empty list if no resource with is found with a matching path.
-
getPaths
Get the paths of all resources in this list relative to the package root.- Returns:
- The paths of all resources in this list relative to the package root, by calling
Resource.getPath()
for each item in the list.
-
getPathsRelativeToClasspathElement
Get the paths of all resources in this list relative to the root of the classpath element.- Returns:
- The paths of all resources in this list relative to the root of the classpath element, by calling
Resource.getPathRelativeToClasspathElement()
for each item in the list.
-
getURLs
Get the URLs of all resources in this list, by callingResource.getURL()
for each item in the list. Note that any resource with ajrt:
URI (e.g. a system resource, or a resource from a jlink'd image) will causeIllegalArgumentException
to be thrown, sinceURL
does not support this scheme, sogetURIs()
is strongly preferred overgetURLs()
.- Returns:
- The URLs of all resources in this list.
-
getURIs
Get the URIs of all resources in this list, by callingResource.getURI()
for each item in the list.- Returns:
- The URIs of all resources in this list.
-
classFilesOnly
Return a newResourceList
consisting of only the resources with the filename extension ".class".- Returns:
- A new
ResourceList
consisting of only the resources with the filename extension ".class".
-
nonClassFilesOnly
Return a newResourceList
consisting of non-classfile resources only.- Returns:
- A new
ResourceList
consisting of only the resources that do not have the filename extension ".class".
-
asMap
Return thisResourceList
as a map from resource path (obtained fromResource.getPath()
) to aResourceList
ofResource
objects that have that path.- Returns:
- This
ResourceList
as a map from resource path (obtained fromResource.getPath()
) to aResourceList
ofResource
objects that have that path.
-
findDuplicatePaths
Find duplicate resource paths within thisResourceList
.- Returns:
- A
List
ofMap.Entry
objects for all resources in the classpath and/or module path that have a non-unique path (i.e. where there are at least two resources with the same path). The key of each returnedMap.Entry
is the path (obtained fromResource.getPath()
), and the value is aResourceList
of at least two uniqueResource
objects that have that path.
-
filter
Find the subset of theResource
objects in this list for which the given filter predicate is true.- Parameters:
filter
- TheResourceList.ResourceFilter
to apply.- Returns:
- The subset of the
Resource
objects in this list for which the given filter predicate is true.
-
forEachByteArray
public void forEachByteArray(ResourceList.ByteArrayConsumer byteArrayConsumer, boolean ignoreIOExceptions)Fetch the content of eachResource
in thisResourceList
as a byte array, pass the byte array to the givenResourceList.ByteArrayConsumer
, then close the underlying InputStream or release the underlying ByteBuffer by callingResource.close()
.- Parameters:
byteArrayConsumer
- TheResourceList.ByteArrayConsumer
.ignoreIOExceptions
- if true, anyIOException
thrown while trying to load any of the resources will be silently ignored.- Throws:
IllegalArgumentException
- if ignoreExceptions is false, and anIOException
is thrown while trying to load any of the resources.
-
forEachByteArray
Fetch the content of eachResource
in thisResourceList
as a byte array, pass the byte array to the givenResourceList.ByteArrayConsumer
, then close the underlying InputStream or release the underlying ByteBuffer by callingResource.close()
.- Parameters:
byteArrayConsumer
- TheResourceList.ByteArrayConsumer
.- Throws:
IllegalArgumentException
- if trying to load any of the resources results in anIOException
being thrown.
-
forEachInputStream
public void forEachInputStream(ResourceList.InputStreamConsumer inputStreamConsumer, boolean ignoreIOExceptions)Fetch anInputStream
for eachResource
in thisResourceList
, pass theInputStream
to the givenResourceList.InputStreamConsumer
, then close theInputStream
after theResourceList.InputStreamConsumer
returns, by callingResource.close()
.- Parameters:
inputStreamConsumer
- TheResourceList.InputStreamConsumer
.ignoreIOExceptions
- if true, anyIOException
thrown while trying to load any of the resources will be silently ignored.- Throws:
IllegalArgumentException
- if ignoreExceptions is false, and anIOException
is thrown while trying to open any of the resources.
-
forEachInputStream
Fetch anInputStream
for eachResource
in thisResourceList
, pass theInputStream
to the givenResourceList.InputStreamConsumer
, then close theInputStream
after theResourceList.InputStreamConsumer
returns, by callingResource.close()
.- Parameters:
inputStreamConsumer
- TheResourceList.InputStreamConsumer
.- Throws:
IllegalArgumentException
- if trying to open any of the resources results in anIOException
being thrown.
-
forEachByteBuffer
public void forEachByteBuffer(ResourceList.ByteBufferConsumer byteBufferConsumer, boolean ignoreIOExceptions)Read eachResource
in thisResourceList
as aByteBuffer
, pass theByteBuffer
to the givenResourceList.InputStreamConsumer
, then release theByteBuffer
after theResourceList.ByteBufferConsumer
returns, by callingResource.close()
.- Parameters:
byteBufferConsumer
- TheResourceList.ByteBufferConsumer
.ignoreIOExceptions
- if true, anyIOException
thrown while trying to load any of the resources will be silently ignored.- Throws:
IllegalArgumentException
- if ignoreExceptions is false, and anIOException
is thrown while trying to load any of the resources.
-
forEachByteBuffer
Read eachResource
in thisResourceList
as aByteBuffer
, pass theByteBuffer
to the givenResourceList.InputStreamConsumer
, then release theByteBuffer
after theResourceList.ByteBufferConsumer
returns, by callingResource.close()
.- Parameters:
byteBufferConsumer
- TheResourceList.ByteBufferConsumer
.- Throws:
IllegalArgumentException
- if trying to load any of the resources results in anIOException
being thrown.
-
close
public void close()Close all theResource
objects in thisResourceList
.- Specified by:
close
in interfaceAutoCloseable
-
equals
-
hashCode
public int hashCode() -
add
public boolean add(T element) -
add
public void add(int index, T element) -
remove
-
remove
public T remove(int index) -
addAll
-
addAll
-
removeAll
-
retainAll
-
clear
public void clear() -
set
public T set(int index, T element) -
iterator
-
listIterator
- Specified by:
listIterator
in interfaceList<T>
- Overrides:
listIterator
in classArrayList<T>
-