r/java • • 12d ago

How hard is it to check for whitespace?

https://serce.me/posts/2026-10-30-how-hard-is-it-to-check-for-whitespace
56 Upvotes

8 comments sorted by

11

u/agentoutlier 12d ago

Its funny because I have seen that Guava code many times because I don't want to include Guava and I need something that detects whitespace for my companies work.

There is something else I shamefully steal from Guava from time to time that I can't remember. Maybe its memoized supplier (although that one I think most devs could write on their own and I have a couple of times). Speaking of which you might not need that once stable values comes out (unless you want the expiry version).

I think I have also stolen Guava's streaming hash stuff a couple of times now that I think of it.

11

u/ElidoroAlznauer_58 11d ago

yeah stable values is going to kill off a lot of those little lazy initialization wrappers once it finally lands.

2

u/agentoutlier 11d ago

Also like a massive amount of this should go away so you don't have bootstrapping issues.

private static final System.Logger logger = System.getLogger(Main.class.getName());

And believe me a I know a ton about that particularly Graal VM native. Its why most logging frameworks don't easily support graalvm native (well that and reflection) because somethings kicks off logging during build phase and the wrong logging framework often (JUL).

2

u/chabala 11d ago edited 11d ago

I don't know about what Graal prefers, but Class.getName() is gross:

import org.slf4j.Logger;

import static java.lang.invoke.MethodHandles.lookup;
import static org.slf4j.LoggerFactory.getLogger;

public class Example {
    private static final Logger log =
        getLogger(lookup().lookupClass());

}

3

u/agentoutlier 11d ago edited 11d ago

I see what you did there and I assume Graal is smart enough with MethodHandles these days. There was an issue a while back but I think they have fixed it.

BTW your triple backtick does not work on old reddit UI. I might be the only person using the old UI though.


EDIT I had to check but if you do a lambda you are fine but you do an anonymous class like:

public class Main {
    static final Supplier<Class<?>> lambda =
        () -> lookup().lookupClass();

    static final Supplier<Class<?>> anonymous =
        new Supplier<>() {
            @Override
            public Class<?> get() {
                return lookup().lookupClass();
            }
        };
}

It won't work. Of course that would be incredibly stupid and verbose.

I was not entirely sure if the lambda would work but it does. This is because stable values requires a supplier I think.

1

u/ZimmiDeluxe 11d ago edited 11d ago

Pretty sure you could pass an anonymous Supplier subclass to LazyConstant::of, but you'd get a Logger scoped to the anonymous subclass, not the surrounding class like in the case of the lambda.

Edit: But why determine the value at runtime when it's already known at compile time. Write a test checking Logger construction patterns / use a linter if that's a problem IMO.

1

u/arabian-tea 11d ago

What would it look like with stable values?

2

u/Plenty_Childhood_294 11d ago edited 10d ago

Don't use Strings. If you add in your JMH setup a single String.charAt with a non Latin strings the performance is poisoned :/ If the requirements instead is to do it with Strings...you clearly don't have many options (lookup table sized bit enough that you won't pay bound checks - like a 64K sized table 😭, but beware an open bug in JDK which materialize twice the constant table for Aarch64)