Skip to content

Repository files navigation

libdmsf

Lib "Darn Sample Message Formatter" is a somewhat simple message formatter for Java 17+.

Usage

Index-based args

This is similar to the formatting in most logging frameworks. By default, the specifier is {}.

To use it, construct an implementation of IndexFormatter and call format:

Stringresult = newIndexFormatter<String>("There are {} birds and {} cats.") {
privatefinalString[] args = newString[] {"5", "4"};
privatefinalStringBuilderbuilder = newStringBuilder();
@Overrideprotectedvoidconstant(intstart, intend) {
// msg from superclassSystem.out.println("constant requested: " + msg.substring(start, end));
builder.append(msg, start, end);
}
@Overrideprotectedvoidparameter(intindex, booleanexplicit) {
System.out.println("parameter requested: " + index);
// for simplicity, bound checks are omitted,// but note that any exceptions are propagated to the caller method!builder.append(args[index]);
}
@OverrideprotectedStringfinish() {
returnbuilder.toString();
}
}.format();
System.out.println("Final string: " + result);

The result should look like this:

constant requested: There are parameter requested: 0
constant requested: birds and parameter requested: 1
constant requested: cats.
Final string: There are 5 birds and 4 cats.

StringIndexFormatter is provided as a basic implementation of IndexFormatter that returns a string:

StringIndexFormatter.format("Do you want {} or {}?", "apples", "berries"); // Do you want apples or berries?newStringIndexFormatter(
"That'll cost %3%",
'%', '%', // start/end delimiteri -> "$" + ((double) i * 1.5)
).format(); // That'll cost $3.75

Additionally, the index of an argument can be specified in between the delimiter (starting at 0):

format("Do you want {1} or {0}? Or maybe {2}?", "apples", "berries", "bananas"); // Do you want berries or apples? Or maybe bananas?

Name-based args

Messages can also be formatted with name-based specifiers.

To use it, you can construct an implementation of NameFormatter and call format, or use StringNameFormatter:

// unlike index-based args, parameter names must be specified, i.e. "{}" is not validStringNameFormatter.format("Your name is {name}", Map.of("name", "Thosea")); // Your name is Thosea// if you return a non-string, it will be adapted using toStringStringNameFormatter.format("Try eating {tiurf}", name -> newStringBuilder(name).reverse()); // Try eating fruit// you can still change the delimiternewStringNameFormatter(
"The %thing? has changed",
'%', '?', // start/end delimiterNamedParameterSupplier.of(Map.of("thing", "delimiter"))
).format(); // The delimiter has changed

Setup

This library is available on Maven Central.

Gradle
implementation("io.github.imthosea:libdsmf:<version>")
Maven
<dependency>
<groupId>io.github.imthosea</groupId>
<artifactId>libdsmf</artifactId>
<version>version</version>
</dependency>

Currently, the latest version is 2.0.0.
There's only a few files, so you can also pretty easily copy paste them directly into your project.

Licensing

The project is under BSD 2-clause.

About

Argument formatter for Java 17+

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages