Class ResourceLocation

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
    Modifier and Type
    Class
    Description
    static enum 
    Represents the type of a ResourceLocation.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private final String
    The (file) name of the resource.
    private final String
    The path of the resource.
    protected static final Supplier<Path>
    The temporary directory for resources which are copied from the classpath.
  • Constructor Summary

    Constructors
    Constructor
    Description
    ResourceLocation(@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 by modifyPath(String).
  • Method Summary

    Modifier and Type
    Method
    Description
    abstract @NotNull File
    Constructs a new File from the resource.
    Resources on the classpath can not be converted into a file.
    abstract @NotNull Path
    Constructs a new Path from the resource.
    Resources on the classpath cannot be converted into a path.
    abstract @NotNull Path
    Copies the resource to the temporary directory.
    The copied resource will be deleted on program exit.
    abstract @NotNull Path
    copy(@NotNull Path target)
    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 use copy() instead.
    boolean
     
    abstract boolean
    Checks if the resource exists.
    static @NotNull ResourceLocation
    external(@NotNull String file)
    Creates a new resource location for a resource on the filesystem.
    static @NotNull ResourceLocation
    external(@Nullable String path, @NotNull String name)
    Creates a new resource location for a resource on the filesystem.
    abstract byte @NotNull []
    Reads the content of the resource as a byte array.
    final @NotNull String
    Returns the (file) name of the resource as a string.
    abstract @NotNull Stream<String>
    Reads the content of the resource as a stream of lines.
    final @NotNull String
    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
    Returns the type of the resource.
    int
     
    static @NotNull ResourceLocation
    internal(@NotNull String file)
    Creates a new resource location for a resource on the classpath.
    static @NotNull ResourceLocation
    internal(@Nullable String path, @NotNull String name)
    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.
    (package private) static @NotNull Pair<String,String>
    splitPath(@NotNull String file)
    Splits a file into a path and a name.
     

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • TEMP

      protected static final Supplier<Path> TEMP
      The temporary directory for resources which are copied from the classpath.
    • path

      private final String path
      The path of the resource.
    • file

      private final String file
      The (file) name of the resource.
  • Constructor Details

    • ResourceLocation

      ResourceLocation(@Nullable @Nullable String path, @NotNull @NotNull String file)
      Constructs a new resource location with the given path and name.
      The path will be stripped and then modified by modifyPath(String).
      Parameters:
      path - The path of the resource
      file - The name of the resource
      Throws:
      NullPointerException - If the file is null
  • Method Details

    • internal

      @NotNull public static @NotNull ResourceLocation internal(@NotNull @NotNull String file)
      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 resource
      name - The name of the resource
      Returns:
      A new resource location
      Throws:
      NullPointerException - If the name is null
    • external

      @NotNull public static @NotNull ResourceLocation external(@NotNull @NotNull String file)
      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 resource
      name - 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 resource
      name - The name of the resource
      Returns:
      The resource location
      Throws:
      NullPointerException - If the path is null
      IllegalArgumentException - 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 resource
      name - The name of the resource
      preferredType - The preferred type of the resource
      Returns:
      The resource location
      Throws:
      NullPointerException - If the path is null
      IllegalArgumentException - If the resource was not found
    • splitPath

      @NotNull static @NotNull Pair<String,String> splitPath(@NotNull @NotNull String file)
      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

      @NotNull protected abstract @NotNull String modifyPath(@Nullable @Nullable String path)
      Modifies the path of the resource on construction.
      Parameters:
      path - The path to modify
      Returns:
      The modified path
    • getType

      @NotNull public abstract @NotNull ResourceLocation.Type getType()
      Returns the type of the resource.
      Returns:
      The type
    • getPath

      @NotNull public final @NotNull String getPath()
      Returns the path of the resource.
      Returns:
      The path
    • getFile

      @NotNull public final @NotNull String getFile()
      Returns the (file) name of the resource as a string.
      Returns:
      The (file) name
    • asFile

      @NotNull public abstract @NotNull File asFile()
      Constructs a new File 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

      @NotNull public abstract @NotNull Path asPath()
      Constructs a new Path 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

      @NotNull public abstract @NotNull InputStream getStream() throws IOException
      Creates an input stream to the resource.
      Returns:
      An input stream
      Throws:
      IOException - If an I/O error occurs
    • getBytes

      public abstract byte @NotNull [] getBytes() throws IOException
      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

      @NotNull public abstract @NotNull String getString() throws IOException
      Reads the content of the resource as a single string.
      Returns:
      The resource content
      Throws:
      IOException - If an I/O error occurs
    • getLines

      @NotNull public abstract @NotNull Stream<String> getLines() throws IOException
      Reads the content of the resource as a stream of lines.
      Returns:
      The resource content
      Throws:
      IOException - If an I/O error occurs
    • copy

      @NotNull public abstract @NotNull Path copy() throws IOException
      Copies the resource to the temporary 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

      @NotNull public abstract @NotNull Path copy(@NotNull @NotNull Path target) throws IOException
      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 use copy() instead.
      Parameters:
      target - The target path
      Returns:
      The path of the copied resource
      Throws:
      IOException - If an I/O error occurs
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object