moznion's tech blog

moznion's technical memo

sprint - Fluent, small and fast string formatter for Java

I published "sprint" version 0.0.2.

And it is also available on maven central. http://search.maven.org/#artifactdetails%7Cnet.moznion%7Csprint%7C0.0.2%7Cjar

Sprint is a string formatter library for Java.
Simple usage is below.

final Sprint sprint = new Sprint();
System.out.Println(sprint.ff("Hello: {}!", "John")); // => Hello: John!

Pros of this formatter library are following.

Fluent template

Sprint takes a fluent (or possibly loose?) template like "{}:{}". {} is a placeholder. This formatter fills arguments into placeholders which are corresponded.
There is no need to mind about correspondence between placeholders of a template and type of arguments. Sprint fills argument with calling Object#toString().

Fast formatting

Sprint parses template to pick up placeholders, and it constructs data structure according to parsed result. This formatter builds a string with scanning this data structure.
Sprint stores this parsed data structure with linking to template in ConcurrentHashMap which is in instance of Sprint, so when formatting with the same template, it uses corresponded pre-parsed data structure.
It means this formatter parses template only at once, so it can reduce redundant template parsing processing.

Result of benchmarking is below (result of jdk1.8.0_92, benchmark code is here). f:id:moznion:20160605223844p:plain

Small

Source code quantity is small and simple :)

Conclusion

Sprint is a fluent, small and fast string formatter for Java.
This formatter is a beta quality, it has potential for growth of performance.
Enjoy.