- All Known Subinterfaces:
ImageResource
- All Known Implementing Classes:
GeneralSampleImage
,Kimia1070
,Kimia216
,Kimia99
,KimiaCollage
,NamedIccProfile
,Type1CoreFont
This interface is supposed to be implemented by some enum class that specifies the root of a resource tree. The
following example defines such an enum type that refers to three images. The path relative to the location of the
enum class itself must be specified and images must be stored there. The enum type must define an appropriate
constructor and implement method getFileName()
:
public enum MyImages implements NamedResource { image1("image1.tif"), image2("image2.png"), image3("jpegs/image3.jpg"); // field to associate the resource's file name private final String filename; @Override public String getFileName() { return filename; } // constructor MyImages(String filename) { this.filename = filename; } }
By default, resource files are assumed to be placed in a folder with the same name as the resource class (i.e., "MyImages") relative to the location of the class. Resources may then be simply referenced by their enum-name, for example,
URL url = MyImages.image1.getURL(); ImagePlus im = IJ.openImage(url.toString());
The benefit of using a named resource (over specifying resources by path strings) is that the resource is guaranteed to exist at compile time (this is assured by validating the existence of all named resources in the test phase) and can be retrieved even if located inside a JAR file. Also, everything is defined in a single place and renaming a resource is easy.
- Version:
- 2022/08/23
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final String
Specifies the name of the resource directory relative to some resource class. -
Method Summary
Modifier and TypeMethodDescriptionReturns the file name for the associated resource (to be implemented by concrete classes).default String
Returns the resource directory relative to the implementing class.static String
getRelativeDirectory
(Class<? extends NamedResource> clazz) default String
Returns the path to the associated resource relative to the location of the implementing class.static String[]
getResourceFileNames
(Class<? extends NamedResource> clazz) Returns the names of the actual files contained in the associated resource directory of the specified class, which must implement theNamedResource
interface.default InputStream
Returns anInputStream
for reading from this resource.default URL
getURL()
Returns the URL to the associated resource.default boolean
Returns true if the associated resource (class) was loaded from a JAR file.
-
Field Details
-
RelativeDirectorySuffix
Specifies the name of the resource directory relative to some resource class. For example, given some resource class insrc/main/java/.../foo/ZeClass.java
the associated resource files are assumed to be in directorysrc/main/resources/.../foo/ZeClass-data/
- See Also:
-
-
Method Details
-
getRelativeDirectory
Returns the resource directory relative to the implementing class. The default implementations assumes that this is a single-level directory with exactly the same name as the implementing class, extended byRelativeDirectorySuffix
("-data"). Implementations may override this definition to return some other relative directory. Note that the directory should not have a legal Java package name to avoid confusion in the build process (e.g., by javadoc).- Returns:
- the relative resource directory for the associated resource
-
getRelativeDirectory
-
getRelativePath
Returns the path to the associated resource relative to the location of the implementing class. This method is not supposed to be overridden.- Returns:
- the relative path to the associated resource
-
getFileName
Returns the file name for the associated resource (to be implemented by concrete classes).- Returns:
- the name of the resource file
-
getURL
Returns the URL to the associated resource. This method is not supposed to be overridden. Throws an exception if the requested resource does not exist.- Returns:
- the URL to the associated resource
-
isInsideJar
Returns true if the associated resource (class) was loaded from a JAR file.- Returns:
- true if inside a JAR file
-
getStream
Returns anInputStream
for reading from this resource. See alsoClass.getResourceAsStream(String)
. This method is not supposed to be overridden.- Returns:
- an
InputStream
for the associated resource
-
getResourceFileNames
Returns the names of the actual files contained in the associated resource directory of the specified class, which must implement theNamedResource
interface. This can be used to check if a given named resource has a matching file in a case-sensitive way.- Parameters:
clazz
- the resource class- Returns:
- an array of strings
-