Search code examples
javaenumsbit-fieldsenumset

Checking for either/or with an EnumSet


So I'm converting some bitfields in our application to use EnumSet instead, and I'm curious if there's a better way to do a comparison for X|Y. Currently we do something like:

if(bitfield & (X | Y) != 0) {
    //do stuff
}

The EnumSet equivalent seems to be:

if(enumSet.contains(X) || enumSet.contains(Y)) {
    //do stuff
}

Is there a cleaner way to do this? I know you can check for containsAll() like so:

EnumSet flagsToCheck = EnumSet.of(X, Y);
if(enumSet.containsAll(flagsToCheck)) {
    //do stuff
}

But that's for a scenario where you want to know if (X & Y) is set. Is there an equivalent way to check for (X | Y)? I would think there would be something like a containsAny() method, but I don't see anything that seems to have that effect.


Solution

  • I would say the existing approach is more readable than your bitwise approach. It says exactly what you mean: if the set contains X, or the set contains Y... Keep it as it is. It's already clean.

    If the set becomes larger, you could use:

    EnumSet<Foo> valid = EnumSet.of(Foo.X, Foo.Y, Foo.A, Foo.B);
    valid.retainAll(enumSet);
    if (valid.isEmpty()) {
        ...
    }
    

    But I'd only keep that for larger cases. For two or three options I'd use the longhand form.