This module holds utilities used in other libraries of the toolkit. Check the packages'
documentation for more details. You can find a quick recap of the main features in the sections
below.
This module is not meant to be imported directly. It will be included by using any other part of the
toolkit. However, if you only want to use the utilities, logging or dependency injection (i.e.: for
a desktop application), you can import it with the following code:
valclassLogger:Logger=Logger(Runtime::class)// Logger for the `Runtime` classvalinstanceLogger:Logger=Logger(this::class)// Logger for this instance's classlogger.info{""" You can add a quick log without declaring a Logger with 'com.hexagonkt.helpers.logger'. It is a default logger created for the System class (same as `Logger(System::class)`). """}classLogger.trace{"Message only evaluated if trace enabled at ${Jvm.id}"}classLogger.debug{"Message only evaluated if debug enabled at ${Jvm.id}"}classLogger.warn{"Message only evaluated if warn enabled at ${Jvm.id}"}classLogger.info{"Message only evaluated if info enabled at ${Jvm.id}"}valexception=IllegalStateException("Exception")classLogger.warn(exception){"Warning with exception"}classLogger.error(exception){"Error message with exception"}classLogger.error{"Error without an exception"}classLogger.time("Logs the time used to run the following block of code"){valmessage="Block of code to be timed"assert(message.isNotBlank())}instanceLogger.flare{"Prints a log that stands out for ease searching"}
By default, Hexagon uses the Java Util Logging logging library, you can use any of its
implementations by just adding another logging adapter as a dependency. Below you can see some
alternatives:
You can take advantage of dependency injection using the InjectionManager object.
The implementation is a map of classes (with an optional tag) to provider functions (in essence:
Map<KClass<*>, () -> Any>). It is a very simple, yet complete, DI implementation.
You can bind supplier functions or objects to classes. If a class is already bound, later calls to
bind* methods are ignored. However, you can use the forceBind* methods if you need to override
a binding (in tests for example).
Check this sample to bind constructor functions or objects to classes, and inject them later:
// Bind classes to functions (create a different instance with each `inject` call)InjectionManager.bind<Date>{java.sql.Date(System.currentTimeMillis())}// Bind classes to objects (returns the same instance for all `inject` calls)InjectionManager.bind<String>("STR")// You can use labels to inject different instancesInjectionManager.bind<String>("Hexagon","toolkit")InjectionManager.bind<Date>("+1h"){java.sql.Date(System.currentTimeMillis()+3_600_000)}valcurrentSqlDate=InjectionManager.inject<Date>()valcurrentSqlDateInferredType:Date=InjectionManager.inject()// Inject different values for a class using tags (can be any type, not only string)valnextHourSqlDate:Date=InjectionManager.inject("+1h")valnextHourSqlDateInferredType:Date=InjectionManager.inject("+1h")// Injecting classes bound to objects return always the same instancevaldefaultString=InjectionManager.inject<String>()valtaggedString:String=InjectionManager.inject("toolkit")// Overriding previously bound classes is not allowed (ignored)InjectionManager.bind<String>("STR Ignored")valignoredBinding=InjectionManager.inject<String>()// You can overwrite previously bound classes using `forceBind*` methodsforceBind(String::class,"STR Overridden")valoverriddenBinding=InjectionManager.inject<String>()
Info
Dependency Injection is not required by the toolkit. All classes and methods have versions
receiving all of their dependencies, so you can use them instead relying on injection (or use
another DI library of your choice).
The core module has utilities to serialize/parse data classes to JSON and YAML. Read the following
snippet for details:
12345678
SerializationManager.formats=linkedSetOf(Json)// Loads JSON format (using it as default)valjason=Person("Jason","Jackson",LocalDate.of(1989,12,31))valjasonJson=jason.serialize(Json)// Can also be Yaml or an string: "application/json"valparsedJason=jasonJson.parse(Person::class)// Uses default format (JSON)assert(jason==parsedJason)assert(jason!==parsedJason)