commons-lang icon indicating copy to clipboard operation
commons-lang copied to clipboard

Add EnumUtils.equalsAny

Open mfg92 opened this issue 1 year ago • 5 comments

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.

mfg92 avatar Sep 24 '24 11:09 mfg92

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).

aherbert avatar Sep 24 '24 12:09 aherbert

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.

garydgregory avatar Sep 24 '24 13:09 garydgregory

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 avatar Sep 24 '24 13:09 mfg92

What can I do to move this issue forward?

mfg92 avatar Oct 09 '24 07:10 mfg92

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.

garydgregory avatar Oct 10 '24 20:10 garydgregory

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));
    }

?

garydgregory avatar Nov 04 '24 14:11 garydgregory

Closing: No reply in over 3 weeks.

garydgregory avatar Nov 30 '24 16:11 garydgregory