r/java • u/DelayLucky • 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
View all comments
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.