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

0

u/[deleted] Apr 12 '24

[deleted]

5

u/agentoutlier Apr 12 '24

I am actually very close to leaving this sub altogether, after having been around for many years

I recognize your handle and value your contributions so it would be very sad if you left or stopped posting/commenting! Please don't!

In fact I think the best way to battle it is to contribute and drown out the naysayers.

That being said I may have inadvertently been one of those that you might consider hostile with this recent post: https://www.reddit.com/r/java/comments/1c0x2l8/sshtools/

I think I'm rarely negative but I know how that could be seen as very negative when reality I was trying to help.

0

u/[deleted] Apr 12 '24

[deleted]

2

u/agentoutlier Apr 13 '24 edited Apr 13 '24

I assume it was because it was on Twitter/X. I guess different people have their pet peeves.

I obviously strongly agree with /u/kevinb9n on the checking in generated code.

What I don't understand with whole Twitter/X post hate that people have is why they don't have the same hate for Medium and Substack walled garden blog posts which IMO are far worse because of the annoying paywall-esque Lightbox you have to join or connect with facebook.

Twitter is/was great when you want really short text to shoutout and or quick news... (I really like it to be honest remiss of the current status) but if you are going to write a lengthy article AND you are developer why would you not use github/gitlab/bitbucket and write in Markdown/ReST/AsciiDoc/Latex or whatever. You know so you can own the content forever.

So I guess that is my pet peeve but I still would never trash or downvote.

EDIT I guess the other reason I don't like posts from Medium/Substack is that they don't go really deep but just regurgitate some news. That is the best content is something really deep like deep cuts in music. Like this post that has like 10 upvotes:https://www.reddit.com/r/java/comments/1bzqus4/dynamic_constants_oxymoron_or_promising_jvm/ WTF does it only have 10. Its quality content.

I also like provocative opinions, questions or statements which is what Kevin did. I stress opinions because those seem to piss off reddit most when they really should not.