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 EnumUtils.equalsAny #1278

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Add EnumUtils.equalsAny #1278

wants to merge 1 commit into from

Conversation

mfg92
Copy link

@mfg92 mfg92 commented Sep 24, 2024

Added EnumUtils.equalsAny similar to StringUtils.equalsAny. This can help to have shorter, easier to read code.

Usage example:

if (EnumUtils.equalsAny(myObject.getMyField().getMyEnum(), Vehicle.CAR, Vehicle.TRUCK, Vehicle.MOTORBIKE)) {
    // do something ...
}

If desired, I can also implement an EnumUtils.equalsNone.

@aherbert
Copy link
Contributor

Similar to (null checks not included):

EnumSet.of(searchEnums[0], searchEnums).contains(testEnum)

This code would leverage the optimised EnumSet code for any Enum that has less than 64 members (as internally it uses bit flags in a long).

@garydgregory
Copy link
Member

garydgregory commented Sep 24, 2024

How is this any different from org.apache.commons.lang3.ArrayUtils.contains(Object[], Object)?

If we do anything, I could see using a solution like @aherbert points out. The disadvantage is that it creates an new EnumSet object for every search.

@mfg92
Copy link
Author

mfg92 commented Sep 24, 2024

How is this any different from org.apache.commons.lang3.ArrayUtils.contains(Object[], Object)?

Using this approach results in longer, less readable code, as there is no vararg parameter.

if (ArrayUtils.contains(new Vehicle[]{ Vehicle.CAR, Vehicle.TRUCK, Vehicle.MOTORBIKE}, myObject.getMyField().getMyEnum())) {
    // do something ...
}

Also, ArrayUtils.contains is not generic, so no type checking can be done. I can write code like this and the compiler will not warn me that this expression will always evaluate to false:

if (ArrayUtils.contains(new Vehicle[]{ "123", "abc"}, myObject.getMyField().getMyEnum())) {
    // do something ...
}

Similar to (null checks not included):

EnumSet.of(searchEnums[0], searchEnums).contains(testEnum)

This code would leverage the optimised EnumSet code for any Enum that has less than 64 members (as internally it uses bit flags in a long).

if (EnumSet.of(Vehicle.CAR, Vehicle.TRUCK, Vehicle.MOTORBIKE).contains(myObject.getMyField().getMyEnum())) {
    // do something ...
}

This is certainly an option, but it has two disadvantages:

  • less readable than EnumUtils.equalsAny.
  • it is not possible to: check at once if my enum is null or has one of several given values

@mfg92
Copy link
Author

mfg92 commented Oct 9, 2024

What can I do to move this issue forward?

@garydgregory
Copy link
Member

Hello @mfg92

What can I do to move this issue forward?

You need to gather positive consensus from the community, which I do not see ATM.

@SafeVarargs
public static <E> boolean equalsAny(E testEnum, E... searchEnums) {
if (ArrayUtils.isNotEmpty(searchEnums)) {
for (final E searchEnum : searchEnums) {

Choose a reason for hiding this comment

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

Hello,
I think it would be good to add a contains method in ArraysUtils the reuse it here, since you are basically doing a search in the array.

@garydgregory
Copy link
Member

Hello @mfg92
How about using the existing API, like this (from ArrayUtilsTest):

    @Test
    public void testContainsAnyEnum() {
        assertTrue(ArrayUtils.containsAny(ElementType.values(), ElementType.ANNOTATION_TYPE));
        assertFalse(ArrayUtils.containsAny(ElementType.values(), (ElementType) null));
    }

?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants