Utility
A collection of helpful utility functions and classes.
By Tom Ansill
Contents
-
Utility
- Collection of commonly used functions.bytesToHex(byte[])
- Converts a byte array to hexidecimal string.hexToBytes(String)
- Converts a hexidecimal string to byte array.format(String,Object,Object...)
- Formats string, replaces any '{}' with object'stoString()
representation.f(String,Object,Object...)
- Shorthand function forformat(String,Object,Object...)
.simpleToString(Object)
-toString()
implementation for the lazy. It will create a string with class name and its fields. Example: MyDogClass(name="fido", color="Brown", age=1). IntoString()
of your classes, you just putUtility.simpleToString(this)
.sensibleToString(Object object)
- Simply adds double quotation marks between a String if inputObject
is a string, otherwise, returnstoString()
of non-String
object. This function help to solve my minor beef withString
'stoString()
implementation where one can easily confuse withnull
or"null"
when object automatically gets converted to String in likeSystem.out.println("Hello " + nullableString)
.generateString(long)
- Generates a random sequence of string. The characters in the random string will be alphanumeric[a-zA-z0-9]
.generateString(Random, long)
- Generates a random sequence of string with provided random generator. The characters in the random string will be alphanumeric[a-zA-z0-9]
.
-
ResourceUtility
- Collection of functions related to reading resources within.jar
files.getAllFilesInResource(Class<?>,String,boolean)
- Scans all resources inside.jar
file that owns the inputClass<?>
and outputs aSet<String>
of path of resources.getResourceListing(Class<?>,String)
- Returns a listing of specified path in the.jar
that owns the inputClass<?>
. NOTE: This is not same asgetResource(String)
because directories are not a thing in.jar
files. All of the files are flattened into a single big root directory. Meaning if you export a.jar
file and attempt to find a directory in it with justgetResource(String)
, it wouldn't work.getResourceFileContent(Class<?>,String)
- Reads entire file in.jar
that owns the inputClass<?>
and output it as String.
-
Version
- Class to detect and interpret versions in Jars. Assuming the Jar file will have manifest file that has version on it. Class will attempt to find it and interpret it. First it looks at package'sgetImplementationVersion()
, thengetSpecificationVersion()
, finally, as last-resort, it will look forverison.properties
in the resources. If that fails, then it will just outputX.Y.Z
. -
ExCollectors
- ExtendedCollectors
utility class for more terminalStream
functions.toMap()
- Convenience function ofCollectors.toMap(K,V)
if you are already streamingMap.Entry<K,V>
. Equivalent toCollectors.toMap(Map.Entry::getKey,Map.Entry::getValue)
.toConcurrentMap()
- Same astoMap()
but asConcurrentMap
.
-
function/*
- Package of lambda functionsRunnableWithException
- Variant ofRunnable
that throwsException
.ConsumerWithException
- Variant ofConsumer
that throwsException
.FunctionWithException
- Variant ofFunction
that throwsException
.SupplierWithException
- Variant ofSupplier
that throwsException
.BiConsumerWithException
- Variant ofBiConsumer
that throwsException
.BiFunctionWithException
- Variant ofBiFunction
that throwsException
.TriConsumer
- Extension ofConsumer
but with 3 parameters.TriConsumerWithException
- Variant ofTriConsumer
that throwsException
.TriFunction
- Extension ofFunction
but with 3 parameters.TriFunctionWithException
- Variant ofTriFunction
that throwsException
.QuadConsumer
- Extension ofConsumer
but with 4 parameters.QuadConsumerWithException
- Variant ofQuadConsumer
that throwsException
.QuadFunction
- Extension ofFunction
but with 4 parameters.QuadFunctionWithException
- Variant ofQuadFunction
that throwsException
.
Prerequisites
- Java 8 or better
- Java Validation Library
Download and Install
Package Repository
The library is availble for download on Sonatype public Maven repository (https://oss.sonatype.org/#nexus):The library is availble for download on Sonatype public Maven repository (https://oss.sonatype.org/#nexus):
Utility (Utility
, ResourceUtility
, ExCollectors
, and functions/*
)
<dependency>
<groupId>com.ansill.utility</groupId>
<artifactId>utility</artifactId>
<version>0.1.1</version>
</dependency>
Version
<dependency>
<groupId>com.ansill.utility</groupId>
<artifactId>version</artifactId>
<version>0.1.1</version>
</dependency>
Build and Install
Maven (or other similar build tools) is needed to build and install JavaUtility
$ git clone https://github.com/tomansill/javautility
$ cd javautility
$ mvn install