A collection of small commonly useful utilities and helpers for Java. This project should be extended by other commonly useful classes and methods.
Feel free to add more generally useful libraries here! When you do (and please do!), kindly consider the following:
BEFORE EXTENDING this project, please be (reasonably) sure that the desired functionality is not already contained in another libarary. Candidates to check are
- Apache Commons, check the components on the page
- Google Guava,
- Java API Docs
Also, please try to keep the dependencies of the project as small as possible. Whenever external libraries are needed only for some specific functionality, set the respective dependencies to the provided
scope and add a respective note to this readme.
- Helper methods to read from the command line
- With a message to print or without
- Restricted to boolean yes/no answers.
- A few ANSI color code constants.
- NOTE: requires the dependency
org.rauschig
:jarchivelib
:1.2.0
to exist on the classpath. This dependency is not resolved transitively from this project. - has a method to extract archive files with automatic archive format determination
- NOTE: requires the dependency
org.apache.commons
:commons-configuration2
:2.2
,commons-beanutils
:commons-beanutils
:1.9.3
andcommons-jxpath
:commons-jxpath
:1.3
to exist on the classpath. This dependency is not resolved transitively from this project. - Helper methods for the apache commons configuration 2 project
- configuration parameter checking
- configuration parameter path creation
- configuration file reading
- Method to get a UTF-8 encoded reader from an InputStream
- Method to get a list of UTF-8 encoded lines from an InputStream
- Helper methods to read or write files
- Automatically handles regular or gzipped files
- Convention: A File instance with a name that ends with .gz or .gzip Is handled like a gzipped file automatically, in reading and writing.
- Method to create a JAR file from an array of given files
- Method to add a file into an existing JAR
- Method to find a (file) resource via file path, URI or on the classpath.
- NOTE requires the dependency
net.bytebuddy
:byte-buddy-agent
:1.7.9
to exist on the classpath. This dependency is not resolved transitively from this project. - Allows to load JAR files during runtime
- Exploits the fact that
the system class loader is an
URIClassLoader` until Java 8 - Automatically detects Java version to pick the correct JAR loading strategy
- Beginning with Java 9, uses the
java.lang.instrument
package and employs the Agent class included in this project- Needs to determine the file path of the JAR containing the Agent class
- This JAR needs to have a
META-INF/MANIFEST.MF
file with the entryAgent-Class: de.julielab.java.utilities.classpath.Agent
- The
julielab-java-utilities
JAR is searched on the classpath for this purpose. - In case of an uber JAR or fat JAR (e.g. through the Maven assembly or shadow plugins), the uber JAR itself is pointed to. The uber JAR must then have the manifest entry as explained above.
- Simple to use API to check parameter values for being not null or not empty. Automatically generates an error report if checks fail via an `IllegalArgumentException.
- Example:
PrerequisiteChecker.checkThat().notNull(ob1, ob2).notEmpty(coll).withNames("ob1", "ob2", "coll").execute()
- Is deactivated by default: No checks will be performed unless the Java system property
de.julielab.prerequisitechecksenabled
is set totrue
. - Can be used with the
Supplier
interface. This allows to quickly check a path within a given object, e.g.PrerequisiteChecker.checkThat().notNull(ob).notNull(() -> ob.prop1).notNull(() -> ob.prop1.prop2).withNames("Base object), "Property 1", "Property 2).execute()
- NOTE requires the dependency
org.mapdb
:mapdb
:3.0.7
for the actual cache implementation. This dependency is not resolved transitively from this project. - The
CacheService
is a singleton that is configured once per application. It then offersCacheAccess
objects for caching. - The
CacheService
is configured via aCacheConfiguration
object. Possible settings include persistent caching, cache size, usage of a in-memory cache in addition to persistent caching, read-only access and the usage of remote caching (see theCachServer
below). - The
CacheAccess
objects provide a simple interface to an underlyingMapDB
cache. The Ā“CacheMapSettingscan be used to pass configuration to the
MapDB`implementation. - This package also offers the
CacheServer
which is a simple HTTP server to encapsulate persistent caches. The main advantage is that multiple clients can then access the same persistent cache. This is not possible otherwise because the cache files can only be opened by a single JVM. When setting remote caching to theCacheConfiguration
and specifying host and HTTP port, theCacheAccess
intances returned by theCacheService
areRemoteCacheAccess
objects. Otherwise, nothing is different from using local caching.
- NOTE requires the dependency
org.apache.lucene
:lucene-core
:8.9.0
commons-io
:commons-io
:2.7
for the actual index implementation. This dependency is not resolved transitively from this project. - Useful when a large map is required that exceeds memory availability
- Indexes keys and values in map-fashion into a Lucene index
- Offers simple methods for retrieval
- Offers in-memory Guava caching for speedup
- NOTE requires the dependency
org.apache.commons
:org.apache.commons
:3.8.1
for theRange
class. This dependency is not resolved transitively from this project. - Helper classes for objects that cover some kind of integer-valued span
- e.g. word spans, character spans, time spans
- Particularly useful when span-objects are sought that overlap with a given span.
de.julielab.java.utilities.spanutils.OffsetMap
maps integer ranges to arbitrary objects, e.g. text annotations. It returns a overlapping-range-restricted subset of itself on request and can return contained objects overlapping a given range.de.julielab.java.utilities.spanutils.OffsetSet
stores a set of ranges and allows to retrieve the first range in the set that overlaps a given range.de.julielab.java.utilities.spanutils.SpanOffsetSet
this is basically theOffsetSet
but accepts objects implementingSpan
, allowing for more general objects.
- Creates an InputStream that is constructed from an iterator of String
- Useful when an API requires an InputStream and the String input is very large and should not be read completely into memory before writing the stream to file or similar scenarios.
- Taken from https://stackoverflow.com/a/62160797/1314955
- Get InputStreams and Readers from
java.net.URI
- Automatically handles regular or gzipped files, analogous to FileUtilities.java