https://github.com/sidmishraw/fmt
A simple parametric message formatter utility for Java
https://github.com/sidmishraw/fmt
java-string-formatter java8 string-formatter
Last synced: 3 months ago
JSON representation
A simple parametric message formatter utility for Java
- Host: GitHub
- URL: https://github.com/sidmishraw/fmt
- Owner: sidmishraw
- License: bsd-3-clause
- Created: 2018-10-18T05:40:23.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-11-01T07:38:03.000Z (over 7 years ago)
- Last Synced: 2025-01-15T06:48:39.395Z (about 1 year ago)
- Topics: java-string-formatter, java8, string-formatter
- Language: Java
- Homepage:
- Size: 7.81 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# fmt
```
Author: Sidharth Mishra
Version: 1.0.0
JDK: 11
```
[](https://travis-ci.com/sidmishraw/fmt)
## About
`fmt` is a simple fluent-API based string-template utility. It is used for providing parametric string formatting –– somewhat like Python's `str.format()`.
```java
var template = "{kong}{{kong}}!";
var result = fmt.inTemplate(template).put("kong", "King").put("king", "Kong").eval(); // KingKong!
```
In `fmt`, the template parameter is defined as `{template-parameter}`. Moreover, `fmt` makes keeps resolving all the template-parameters recursively till it can no longer resolve any more template parameters.
## Approaches
The first approach would be to get a param-key-value pair out and start resolving the template recursively till all the template parameters have been resolved.
For example:
```
template = "{king}{{one}}{{{kong}}}"
params = {
"king": "kong",
"one": "two",
"kong": "king"
}
step 1: let one be picked (because I'm using a hashmap internally),
template1 = "{king}{two}{{{kong}}}"
step 2: let now king is selected
template2 = "kong{two}{{{kong}}}"
step 3: now kong is selected
template3 = "kong{two}{{king}}" << it should stop here -- for one pass >>
but the final result should be:
"kong{two}king",
```
to achieve the final result, `fmt` retries recursively trying to resolve the new template parameters. If it not able to resolve, it unwinds and returns the result.
## Motivation
I needed an utility that provided parameteric substitutions in Java like in Python. The existing MessageFormatter gives indexed based substitution and it is limiting for my use-case.