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 TriState toBoolean helper methods #472

Merged
merged 1 commit into from Oct 16, 2021

Conversation

lynxplay
Copy link
Contributor

@lynxplay lynxplay commented Oct 8, 2021

While the TriState enum already has helper methods to quickly convert a
boolean into a TriState (both nullable wrappers and their primitive
counterparts), helper methods to convert a TriState back into a boolean
representation are currently missing.

This commit introduces two methods that do exactly that, one
allowing a straight forward conversion to a Boolean as it can represent
a TriState through its nullability as well as another method that
discards the TriState#NOT_SET by accepting a supplier that will be
used to map a not set tri-state.


I would be very, very happy about suggestions/names for TriState#toBooleanOrElseGet. I based the current method name off of the Optional#orElseGet method that also uses a supplier to map the "not-present" state of an optional, but there might very well be a much better name that I am missing.

@kezz
Copy link
Member

kezz commented Oct 8, 2021

Thanks for the PR! Besides the checkstyle issues (which will be flagged up when this build completes), I like this PR! In terms of the implementation, I wonder if we could have the Boolean values as a property of the enum directly.

I'd want to say an Index, but I don't think that supports null values. Maybe a Map<Boolean, TriState> with a map that supports null keys? This could be generated on initialisation using the Boolean values for each enum.

I hope that makes sense :')

@lynxplay lynxplay force-pushed the feature/tri-state-to-boolean branch from 1ba82a2 to 1cd0b55 Compare October 8, 2021 15:30
@lynxplay
Copy link
Contributor Author

lynxplay commented Oct 8, 2021

checkstyle issues (which will be flagged up when this build completes)

were fixed 👍
In regards to the concept of an Index or a Map to map the TriState to a Boolean, a HashMap afaik allows for null as a key. Would you suggest creating a child of the Index utility ?
Tho I am not too sure a map is really needed here. TriState will not outgrow the complexity of two ternary operators anyway concerning the very name of the utility limits it to three states.

@lynxplay lynxplay force-pushed the feature/tri-state-to-boolean branch from 1cd0b55 to daabe1a Compare October 8, 2021 16:28
@kashike kashike self-assigned this Oct 8, 2021
@kashike kashike added this to the 4.10.0 milestone Oct 8, 2021
Copy link
Member

@kezz kezz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be worth a toBooleanOrElse method, but it's not massively necessary, and this is just fine regardless :)

@lynxplay
Copy link
Contributor Author

lynxplay commented Oct 8, 2021

Might be worth a toBooleanOrElse method, but it's not massively necessary, and this is just fine regardless :)

I'll add it :)

While the TriState enum already has helper methods to quickly convert a
boolean into a TriState (both nullable wrappers and their primitive
counterparts), helper methods to convert a TriState back into a boolean
representation are currently missing.

This commit introduces two methods that do exactly that, one
allowing a straight forward conversion to a Boolean as it can represent
a TriState through its nullability as well as another method that
discards the TriState#NOT_SET by accepting a supplier that will be
used to map a not set tri-state.
@lynxplay lynxplay force-pushed the feature/tri-state-to-boolean branch from daabe1a to b1550bf Compare October 8, 2021 16:49
@lynxplay
Copy link
Contributor Author

lynxplay commented Oct 8, 2021

Added the toBooleanOrElse method.
Additionally, I expanded the test cases a little, now passing the opposite into the methods that might use a default (e.g. toBooleanOrElse and toBooleanOrElseGet) to ensure that the methods do not accidentally use the default (e.g. true) on a tri-state (e.g. TriState#TRUE).

This was referenced Feb 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants