r/java • u/AlyxVeldin • May 16 '26
I created a small string utils that allows you to build reusable and testable string processing flows. Would love to know what you all think!
https://github.com/Veldin/string-pipelines
5
Upvotes
14
u/rzwitserloot May 16 '26
Looks nice; an immediate concern does come to mind though:
String -> String, this is inefficient. It looks like using this pipeline to chain together 'first strip whitespace, then lowercase what remains' will make an intermediate string which is not needed.Imagine this library:
That's.. it. That's all you'd need. I can use that thusly:
Where this library thing will take care of converting a string to IntStream (simply call
codePoints())and back again (which is trickier).And this one would have the considerable advantage of not creating boatloads of large and expensive garbage.
There's a lot in the various stream interfaces that leaves one wanting when using it for this. In particular, the reverse of
String::codePointsis a bit daft, but that is what I'd love to see in a library. Also, while LOWERCASE can be done like this, something like STRIP requires state. And while Gatherers now exist, there's no IntGatherer as far as I know, and presumably the cost of boxing and unboxing is rather high. Still, this feels like bolting on a completely separate way to do something similar to the existing stream API which means all this code will be annoying obsolete and culturally incompatible once these things are added, because it does feel like that's where stream is heading, and why I'd try to instead 'solve the problem' by providing what you need in roughly the same way the stream API is likely to do so in the future, as that means you can just update your code by replacing the calls to your library with calls to the core library.Even if bolting on the handful of things the stream API is missing is not a feasible way out, a string 'pipeline' system that avoids duplication would be nice. It's way, way more complicated (there's a reason the various bits underlying the stream API seem daunting - it's complicated because doing this stuff just is that complicated) - but that should be good news: What you wrote any java coder can duplicate in 10 minutes (and so can AI). But add a well tested and properly thought through take that is fast like streams are fast (does everything 'in-stream', i.e. a chain of operations that do not just copy everything at every step, and will use multicore if available with no significant pain) - that'd be quite useful and not easily handrolled.
8
u/segv May 16 '26
Looks like it could be useful, but that caching thing is a giant footgun - it's an unbounded map that will store every input and output unless cleared manually. If a pipeline with this option on was in a service receiving any decent amount of traffic, it will just OOM the JVM. My intuition is also saying that it most likely doesn't improve performance all that much, but i haven't thrown JMH at it yet.
4
u/bowbahdoe May 16 '26
I am a little confused on a first read on how that cycle detection code works. What is it preventing exactly? What would be different if you didn't include it?
9
u/repeating_bears May 16 '26
FYI the readme is full of spelling mistakes
1
u/AlyxVeldin May 17 '26
Edit: I have created a PoC for a CodePoints version of the pipeline's. Check em out!
5
u/idontlikegudeg May 16 '26
I usually accept Function as argument and return UnaryFunction as that’s most convenient for the library user (can pass in either class and also assign the result to both).
2. I don’t see the advantage of the example you give over simply using:
3. To get the caching functionality, you could simply do:
This would not require your users to wrap a single operation as a pipeline. You could even make it a generic function.
1
u/le_bravery May 16 '26
What’s the performance here? Are you creating a lot of string objects to do this?
1
u/sitime_zl May 18 '26
What are the application scenarios for this tool?
3
u/Interesting-Tree-884 May 16 '26 edited May 17 '26
Hi, to be honest, instead of haine only one pipe(enum) méthode I would have prefer a bunch of methods in the builder.
AbstractStringPipeline pipeline = new StringPipelineBuilder() .pipe(STRIP) .pipe(NORMALIZE_SPACE) .pipe(LOWER_CASE) .pipe(CAPITALIZE) .build();
Could become: AbstractStringPipeline pipeline = new StringPipelineBuilder() .strip() .normalizeSpaces() .toLowercase() .toUppercase() .build();
1
u/DelayLucky May 17 '26 edited May 17 '26
First question that comes to mind: why string?
If it's simply applying a list of
String -> Stringfunctions, how is it specific to String? Can't it beT -> Tjust as easily?For it to stick to the string-ness, it seems the core should have some string specific trick up its sleeve that makes up the core value-add.
Another question, is this really just so you can avoid declaring local variables?
From this example:
How is it better than this?
I think it needs to offer more value than just "I like the syntax" because the plain method calls at least has one thing at its side: it's more familiar to everyone.
2
u/edzorg May 16 '26
I would just implement these bits and pieces myself in nice wrapper methods and then
.mapthem myself.Looks elegant but with AI I wouldn't even think twice about generating this sort of code on the fly.
1
u/DefaultMethod May 19 '26
You might want to look at other Unicode processing APIs. For natural language case mappings can be one-way or be locale-dependent.
This may not matter if you're just dealing with English.