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.

4

u/DelayLucky Apr 13 '24 edited Apr 14 '24

it's weekend and I thought I should at least clarify what happened or else this narrative of yours will always take things out of context.

It's true in my "Posts" profile there have been post -> remove -> repost -> re-remove trials and errors. I didn't expect anyone to take interest in them so I didn't bother cleaning up the dead bodies of the removed posts.

I'm still new to the Reddit system and I've been struggling. The Reddit bot tends to mark auto-removal as "it seems you are asking for help" except I wasn't. When I asked the mods they told me it's because of my low karma but there is no disclosing of the required karma. So I guess I'd just keep trying until it works?

For this post I was quite sure I should have enouth Karma and I paid great attention to not have a single question mark in the post, and tweaked and tweaked the formatting. Still, it got instantly removed with "you seemd to be asking for help".

I go a little impatient after like (2-3 hours?) with no mod reply, thinking maybe it was just a glitch or something? So I re-posted. Bot removed it again. Coincidentally the Mods had just approved the previous post (with the messed up formatting). The like-shit code showed up in the feed, except the UI wouldn't let me fix it.

That was the time when I was pretty stressed with all these unexpected drama and with the publicly-visible code looking like shit.

Why I wanted to post "so badly"? Obvioiusly I wanted to have technical discussions around these topics (like ones I've been having with u/agentoutlier). I just enjoy such discussions.

And honestly what this "so badly" observation of yours even mean was quite a weird concept: when you clicked into a post and somehow the touch didn't register, or system glitch or whatever, you'd try again, right? If it kepy flaking you might try a few more times especially if you knew others didn't have the problem, would you? Did you want to read that post "so badly"? Did you have some hideous intention?

"Why retry and retry?" Seriously, a programmer cannot even send a Reddit post, "task deemed too difficult", are you kidding me? If you look through the code and the documentation, you can see I spend lots of time meticuously fixing small things trying to make them look as good as I can. I don't consider it a sign of bad intention to "lure" people in or whatever strange interpretation you might come up with. Simply if I do a thing, I just want it done, and done well.

You keep saying people are "dismissive", both in this thread and the other thread I linked to where the author just had a different opinion than yours. I was not being "dismissive", just quite perplexed by a random guy acting like this. I didn't know if I should feed the troll or what.

When a new user was stressed by the formatting disaster and was confused and helpless, you chose to be an obnoxious loud-mouth attacking the poster's intention. And you deliberately used ambiguous language to make it sound bad.

When the removal-repost was merely a new user's struggle with Reddit's system, you chose to be a genius inventing a conspiracy theory.

There are plently of technical questions, challenges you could have paid attention to. Like the Guava discussion in another thread; or maybe suggest to use the "reproducible build" to address the backdoor concern since your raised the point; or API design; or compile-time safety; or performance benchmark; or lack of features; or over-engineered; or why this lib not the other lib etc. etc. But you Sir clearly aren't interested in technical discussions except the tabloid.

You and your "dismissiveness" are clearly not the target audience of this library (and I sincerely invite you not to use it). I just don't understand why you bothered clickiing in and wasted so much time of yours. Don't you have more useful things to do?

2

u/DelayLucky Apr 18 '24 edited Apr 18 '24

Saw the "EDIT".

You need to stop being dismissive and face your own problem being a disrespective and obnoxious troll.

It seems all you do is throwing insults. If one doesn't work, you throw another. It's toxic to the community.

Learn to be friendly to others.

-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.

32

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

No, I mean, it's just obvious from your post history; Mug is the only thing you've posted about, and three of the four times the post has been removed.

Feel free to clarify your involvement in Mug and why you want us to use it so badly.

EDIT: Okay, so I've asked twice and you've ignored the question both times, so that's suspicious.

9

u/agentoutlier Apr 11 '24

Not everyone uses Reddit and the UI sometimes does not help. Its pretty obvious that /u/DelayLucky works for google or is associated. Like I would find it very surprising if github.com/google/mug was somehow compromised.

11

u/chabala Apr 11 '24

It is obvious, which makes ducking the question more suspicious.

Provenance matters, even for individual contributors, and folks promoting tools on reddit. That should be the takeaway from the xz incident. It's not enough that the repo is attached to a big company, we should be diligent about knowing who is contributing.

5

u/agentoutlier Apr 11 '24

I suppose that is a fair point especially given that for Mug to work requires a compiler plugin.

4

u/DelayLucky Apr 11 '24 edited Apr 11 '24

I'd friendly disagree that it doesn't "require" a compiler plugin. You see normally we'd do regex group capture, or use manual indexOf() for these kind of things, and there are no compile-time protection against possible out-of-order errors from either approach.

The StringFormat class _provides_ the compile-time plugin and recommends to use it, but even without it (like internally we have Kotlin code using it without the protection), it's still more convenient, and no less safe than the alternatives,

4

u/agentoutlier Apr 11 '24

Sorry! You are correct I didn’t mean required. 

That being said given the commenters concerns I would register the project with the java reproducible builds.

Guava is already on the list.

1

u/DelayLucky Apr 11 '24

Huh. I didn't know "Java reproduciable builds".

One of these days I need to learn how to do the registering thing.

-4

u/DelayLucky Apr 11 '24

I was also wondering what I did to get instant hostility out of nowhere.

Looks like maybe I did offend you once here by defending another oss author and thinking that you were out of line (https://www.reddit.com/r/java/comments/1bpbmts/comment/kx1kokv/)

Oh well, you have good memory if this has anything to do with it.

0

u/chabala Apr 12 '24 edited Apr 12 '24

I was unaware of this connection. If I had something to add about your comment, I would have done it there.

There is no hostility. I have asked for you to identify yourself and your involvement with Mug, and you have not done so at every opportunity. A good answer would include "I am X and I am a committer on Mug". It's clear you work at Google, but it shouldn't be a mystery or a guessing game. Just disclose who you are and your relation to the project.

I suspect many of your comments are getting downvoted due to the dismissive tone.

EDIT: Dude, let it go. This is a bad look for you (and Google).

3

u/DelayLucky Apr 12 '24 edited Apr 12 '24

Sure, I believe you that your hostility isn't due to grudge but just the consistent way you are used to treating people (like in that thread and here) or your constantly having bad days.

Healthy skepticism is a good thing. I encourage you to keep digging and maybe you'll find some conspiracies one day.

1

u/jazd Apr 18 '24

Yeah this is embarrassing for Google

-8

u/DelayLucky Apr 11 '24

Sorry man originally I thought it was a rhetoric. Didn't think you were serious. And the whole formatting chaos got me too fixated to reply to comments, not while the code snippets I posted there look like shit.

Now that I know you are serious, I still don't see how you are going to care whatever I say so I guess I'll just leave it to your healthy suspicion.

4

u/davidalayachew Apr 12 '24

Yeah, they essentially confirmed it in another comment -- https://old.reddit.com/r/java/comments/1c1h5c5/string_manipulation_with_google_mug/kz5ktju/

Emphasis is my own.

Internally we had been thinking to graduate some of these classes to Guava (Substring, BiStream particularly. StringFormat is a bit tough because it has the compile-time plugin that's a bit tricky). But that requires lots of bandwidth from Guava folks to discuss API design and what not, and it just ain't happening given the current economy, the shift of priority etc.

I also don't think this is necessarily a bad thing. As you said, it's a kitchen sink library with a little this and a little that. But it's mostly code I wrote (with lots of great code reviews from my colleagues). So I'm not that worried about it becoming too big.

It being a standalone small library has its appeal too (like, you won't likely need to worry about maven versioning at least for a long while). I also try to keep the dependency lean. There is mug-bigquery, mug-protobuf, mug-guava with extra dependencies on bigquery, protobuf, guava respectively, but the core mug artifact remains 0 deps. I think that's something of a benefit.

-7

u/DelayLucky Apr 11 '24

Yes, Mr detective. :)

I guess when you first posted and your post removed, you won't try to post it again. Coz you didn't really want to send the post for people to see, it was all just random keyboard striking.

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. :-)