Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add omitEmpty flag setter to toStringHelper #7415

Closed
4 tasks done
YutaHiguchi-bsn opened this issue Oct 2, 2024 · 1 comment · Fixed by #7426
Closed
4 tasks done

Add omitEmpty flag setter to toStringHelper #7415

YutaHiguchi-bsn opened this issue Oct 2, 2024 · 1 comment · Fixed by #7426
Assignees
Milestone

Comments

@YutaHiguchi-bsn
Copy link

1. What are you trying to do?

This is similar to #1404 adding support of omitting empty values from toStringHelper output.

What I noticed is that the current code already has the feature,
just that there is no public method like omitNullValues() to enable the omitEmptyValues flag.

private boolean omitEmptyValues = false;

It supports various types detecting empty-ness

private static boolean isEmpty(Object value) {
// Put types estimated to be the most frequent first.
if (value instanceof CharSequence) {
return ((CharSequence) value).length() == 0;
} else if (value instanceof Collection) {
return ((Collection<?>) value).isEmpty();
} else if (value instanceof Map) {
return ((Map<?, ?>) value).isEmpty();
} else if (value instanceof java.util.Optional) {
return !((java.util.Optional<?>) value).isPresent();
} else if (value instanceof OptionalInt) {
return !((OptionalInt) value).isPresent();
} else if (value instanceof OptionalLong) {
return !((OptionalLong) value).isPresent();
} else if (value instanceof OptionalDouble) {
return !((OptionalDouble) value).isPresent();
} else if (value instanceof Optional) {
return !((Optional) value).isPresent();
} else if (value.getClass().isArray()) {
return Array.getLength(value) == 0;
}
return false;
}

Can omitEmptyValues() be added to ToStringHelper methods, so that the implemented feature can be exercised?

2. What's the best code you can write to accomplish that without the new feature?

        return MoreObjects.toStringHelper(this)
            .omitNullValues()
            .add("optional", optional.orElse(null))
            .add("array", Array.getLength(array) == 0 ? null : array)
            .add("collection", collection.isEmpty() ? null : collection)
            .toString();

3. What would that same code look like if we added your feature?

        return MoreObjects.toStringHelper(this)
            .omitEmptyValues()
            .add("optional", optional)
            .add("array", array)
            .add("collection", collection)
            .toString();

(Optional) What would the method signatures for your feature look like?

No response

Concrete Use Cases

Exclude empty Optional, Collection, etc. from toString() output.

Packages

com.google.common.base

Checklist

@kluever
Copy link
Member

kluever commented Oct 8, 2024

Good eye --- we have this exact feature available internally (which is why some of the implementation bits have "leaked" into Guava). I'm not opposed to open-sourcing it, given that "emptiness" seems to be a fairly well-understood (but not officially documented or statically encoded) concept.

I think we'll need to beef up the javadocs on our API a bit --- we may want to explicitly list the current types we check for emptiness, and also give ourselves some wiggle room for expanding (or shrinking) that list in the future.

copybara-service bot pushed a commit that referenced this issue Oct 9, 2024
RELNOTES=Add `ToStringHelper.omitEmptyValues()`.
PiperOrigin-RevId: 683758719
@cpovirk cpovirk added this to the 33.4.0 milestone Dec 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants