Skip to content

imthosea/libdsmf

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:

String result = new IndexFormatter<String>("There are {} birds and {} cats.") {
  private final String[] args = new String[] {"5", "4"};
  private final StringBuilder builder = new StringBuilder();
  @Override
  protected void constant(int start, int end) {
    // msg from superclass
    System.out.println("constant requested: " + msg.substring(start, end));
    builder.append(msg, start, end);
  }
  @Override
  protected void parameter(int index) {
    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]);
  }
  @Override
  protected String finish() {
    return builder.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?
new StringIndexFormatter(
    "That'll cost %3%",
    '%', '%', // start/end delimiter
    i -> "$" + ((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 valid
StringNameFormatter.format("Your name is {name}", Map.of("name", "Thosea")); // Your name is Thosea
// if you return a non-string, it will be adapted using toString
StringNameFormatter.format("Try eating {tiurf}", name -> new StringBuilder(name).reverse()); // Try eating fruit
// you can still change the delimiter
new StringNameFormatter(
    "The %thing? has changed",
    '%', '?', // start/end delimiter
    NamedParameterSupplier.of(Map.of("thing", "delimiter"))
).format(); // The delimiter has changed

Dependency

Gradle (Kotlin)
maven {
  name = "teamcelestial"
  url = "https://maven.teamcelestial.org/public"
}
implementation("me.thosea:libdsmf:<version>")
Gradle (Groovy)
maven {
  name "teamcelestial"
  url "https://maven.teamcelestial.org/public"
}
implementation "me.thosea:libdsmf:<version>"
Maven
<repository>
  <id>teamcelestial</id>
  <url>https://maven.teamcelestial.org/public</url>
</repository>
<dependency>
  <groupId>me.thosea</groupId>
  <artifactId>libdsmf</artifactId>
  <version>version</version>
</dependency>

Currently, the latest version is 1.0.0. You can find javadocs here.

Licensing

The project is under BSD 2-clause.

About

Argument formatter for Java 17+

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published