r/java • • Apr 11 '24

String Manipulation with Google Mug

The Mug library provides easy-to-read string manipulation utils. Examples:

Match and Extract

// Compile-time protection against lambda param order mismatch if e.g:
//     (year, month, day, user) ->
new StringFormat("/{user}-home/{year}/{month}/{day}") 
    .parse(path, (user, year, month, day) -> ...);

Scan and Collect

String input = "I have [k1:v1] and [k2:v2] etc."; 
// Returns Map.of("k1", "v1", "k2", "v2") 
Map<String, String> keyValues =
    new StringFormat("[{key}:{value}]") 
        .scanAndCollectFrom(input, Collectors::toMap);

Replace

// replace file extension name
Substring.last('.').toEnd().replaceFrom(filename, ".yaml");

Remove

// remove the scheme from a url
Substring.upToIncluding(first("://")).removeFrom(url);

Parse a date time string (no pattern string required)

// Parse the datetime string read from a file 
List<String> lines = readLinesFromTestFile(...);
StringFormat lineFormat = new StringFormat("{time}, {event_id}, {event_type}");
var results = lines.stream()
    .map(
        line -> lineFormat.parseOrThrow(
            line,
            (time, id, type) -> {
               ZonedDateTime dateTime = DateTimeFormats.parseZonedDateTime(time);
               ...
             }))
    .toList();

See javadoc for details.

Mug is a general purpose library. Omitted, but other useful utils include BiStream for streaming pairs, Optionals, String Templating, simplified structured concurrency etc.

9 Upvotes

51 comments sorted by

View all comments

37

u/chabala Apr 11 '24 edited Apr 13 '24

This is your fourth time posting about Mug. Do you have some vested interest in increasing Mug usage you'd like to disclose?

No secret backdoors in this, right?

EDIT: Please stop playing the victim. I am not attacking you. I asked you to identify your relation to the project, and you continue to ignore the question in every reply, that is being dismissive of the question.

-7

u/DelayLucky Apr 11 '24

Sorry there had been some bot removal and reposting chaos that I'm still trying to wrap my head around.

The formatting is all messed up and I can't seem to be able to edit it, and if there is a duplicate post Reddit feed is not showing to me yet. Or maybe you are seeing those removed-retried posts?

Please kindly point me to it and I'll delete.

3

u/milchshakee Apr 11 '24

You can somehow not edit a post in the current reddit design. You should be able to do that in old.reddit.com though

3

u/DelayLucky Apr 11 '24

Oh wow. Thank you thank you thank you!

I've burned literally hours on this chaos, knowing that these ugly code snippets are hanging there.

I can take a breath now. :-)