java.lang.Object
net.luis.utils.resources.ResourceLocation
- Direct Known Subclasses:
ExternalResourceLocation
,InternalResourceLocation
public abstract sealed class ResourceLocation
extends Object
permits ExternalResourceLocation, InternalResourceLocation
Represents a resource which can be loaded from the classpath or from the filesystem.
-
Nested Class Summary
Nested Classes -
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionResourceLocation
(@Nullable String path, @NotNull String file) Constructs a new resource location with the given path and name.
The path will be stripped and then modified bymodifyPath(String)
. -
Method Summary
Modifier and TypeMethodDescriptionabstract @NotNull File
asFile()
Constructs a newFile
from the resource.
Resources on the classpath can not be converted into a file.abstract @NotNull Path
asPath()
Constructs a newPath
from the resource.
Resources on the classpath cannot be converted into a path.abstract @NotNull Path
copy()
Copies the resource to thetemporary directory
.
The copied resource will be deleted on program exit.abstract @NotNull Path
Copies the resource to the given target path.
The copied resource will stay on the filesystem and will not be deleted on program exit.
If a temporary copy of the resource is needed usecopy()
instead.boolean
abstract boolean
exists()
Checks if the resource exists.static @NotNull ResourceLocation
Creates a new resource location for a resource on the filesystem.static @NotNull ResourceLocation
Creates a new resource location for a resource on the filesystem.abstract byte @NotNull []
getBytes()
Reads the content of the resource as a byte array.final @NotNull String
getFile()
Returns the (file) name of the resource as a string.getLines()
Reads the content of the resource as a stream of lines.final @NotNull String
getPath()
Returns the path of the resource.static @NotNull ResourceLocation
getResource
(@Nullable String path, @NotNull String name) Gets a resource from the classpath or from the filesystem.
Trys to load the resource from the filesystem first.
If the resource was not found in the filesystem then the classpath will be tried.
If the resource was not found an exception will be thrown.static @NotNull ResourceLocation
getResource
(@Nullable String path, @NotNull String name, @Nullable ResourceLocation.Type preferredType) Gets a resource from the classpath or from the filesystem.
Trys to load the resource from the preferred type first.
If the preferred type is null then the resource will be tried to load from the classpath first.
If the resource was not found in the preferred type then the other type will be tried.
If the resource wa not found an exception will be thrown.abstract @NotNull InputStream
Creates an input stream to the resource.abstract @NotNull String
Reads the content of the resource as a single string.abstract @NotNull ResourceLocation.Type
getType()
Returns the type of the resource.int
hashCode()
static @NotNull ResourceLocation
Creates a new resource location for a resource on the classpath.static @NotNull ResourceLocation
Creates a new resource location for a resource on the classpath.protected abstract @NotNull String
modifyPath
(@Nullable String path) Modifies the path of the resource on construction.Splits a file into a path and a name.toString()
-
Field Details
-
TEMP
The temporary directory for resources which are copied from the classpath. -
path
The path of the resource. -
file
The (file) name of the resource.
-
-
Constructor Details
-
ResourceLocation
Constructs a new resource location with the given path and name.
The path will be stripped and then modified bymodifyPath(String)
.- Parameters:
path
- The path of the resourcefile
- The name of the resource- Throws:
NullPointerException
- If the file is null
-
-
Method Details
-
internal
Creates a new resource location for a resource on the classpath.- Parameters:
file
- The file of the resource- Returns:
- A new resource location
- Throws:
NullPointerException
- If the file is null
-
internal
@NotNull public static @NotNull ResourceLocation internal(@Nullable @Nullable String path, @NotNull @NotNull String name) Creates a new resource location for a resource on the classpath.- Parameters:
path
- The path of the resourcename
- The name of the resource- Returns:
- A new resource location
- Throws:
NullPointerException
- If the name is null
-
external
Creates a new resource location for a resource on the filesystem.- Parameters:
file
- The file of the resource- Returns:
- A new resource location
- Throws:
NullPointerException
- If the file is null
-
external
@NotNull public static @NotNull ResourceLocation external(@Nullable @Nullable String path, @NotNull @NotNull String name) Creates a new resource location for a resource on the filesystem.- Parameters:
path
- The path of the resourcename
- The name of the resource- Returns:
- A new resource location
- Throws:
NullPointerException
- If the name is null
-
getResource
@NotNull public static @NotNull ResourceLocation getResource(@Nullable @Nullable String path, @NotNull @NotNull String name) Gets a resource from the classpath or from the filesystem.
Trys to load the resource from the filesystem first.
If the resource was not found in the filesystem then the classpath will be tried.
If the resource was not found an exception will be thrown.- Parameters:
path
- The path of the resourcename
- The name of the resource- Returns:
- The resource location
- Throws:
NullPointerException
- If the path is nullIllegalArgumentException
- If the resource was not found- See Also:
-
getResource
@NotNull public static @NotNull ResourceLocation getResource(@Nullable @Nullable String path, @NotNull @NotNull String name, @Nullable @Nullable ResourceLocation.Type preferredType) Gets a resource from the classpath or from the filesystem.
Trys to load the resource from the preferred type first.
If the preferred type is null then the resource will be tried to load from the classpath first.
If the resource was not found in the preferred type then the other type will be tried.
If the resource wa not found an exception will be thrown.- Parameters:
path
- The path of the resourcename
- The name of the resourcepreferredType
- The preferred type of the resource- Returns:
- The resource location
- Throws:
NullPointerException
- If the path is nullIllegalArgumentException
- If the resource was not found
-
splitPath
Splits a file into a path and a name.- Parameters:
file
- The file to split- Returns:
- A pair of the path and the name
- Throws:
NullPointerException
- If the file is null
-
modifyPath
Modifies the path of the resource on construction.- Parameters:
path
- The path to modify- Returns:
- The modified path
-
getType
Returns the type of the resource.- Returns:
- The type
-
getPath
Returns the path of the resource.- Returns:
- The path
-
getFile
Returns the (file) name of the resource as a string.- Returns:
- The (file) name
-
asFile
Constructs a newFile
from the resource.
Resources on the classpath can not be converted into a file.- Returns:
- The resource as a
File
- Throws:
UnsupportedOperationException
- If the resource is on the classpath
-
asPath
Constructs a newPath
from the resource.
Resources on the classpath cannot be converted into a path.- Returns:
- The resource as a
Path
- Throws:
UnsupportedOperationException
- If the resource is on the classpath
-
exists
public abstract boolean exists()Checks if the resource exists.- Returns:
- True if the resource exists, otherwise false
-
getStream
Creates an input stream to the resource.- Returns:
- An input stream
- Throws:
IOException
- If an I/O error occurs
-
getBytes
Reads the content of the resource as a byte array.- Returns:
- The bytes of the resource
- Throws:
IOException
- If an I/O error occurs
-
getString
Reads the content of the resource as a single string.- Returns:
- The resource content
- Throws:
IOException
- If an I/O error occurs
-
getLines
Reads the content of the resource as a stream of lines.- Returns:
- The resource content
- Throws:
IOException
- If an I/O error occurs
-
copy
Copies the resource to thetemporary directory
.
The copied resource will be deleted on program exit.- Returns:
- The path of the copied resource
- Throws:
IOException
- If an I/O error occurs
-
copy
Copies the resource to the given target path.
The copied resource will stay on the filesystem and will not be deleted on program exit.
If a temporary copy of the resource is needed usecopy()
instead.- Parameters:
target
- The target path- Returns:
- The path of the copied resource
- Throws:
IOException
- If an I/O error occurs
-
equals
-
hashCode
public int hashCode() -
toString
-