CodeMonkey
CodeMonkey

Reputation: 12424

Java Check if enum value is part of a returned set of other enum values

I got an enum with 3 values using Java. I got classes which are related to 1 or more of the enum values.

I know how to create a list and make an iteration, but is there a better way to return a single value of related enum values in which I could check if containing another value? For example something like:

public MyEnum mySupportedValues() 
{
    return MyEnum.Value1 | MyEnum.Value3; //not even sure if thats correct
}

And then be able to check if an enum value is a part of the supported list:

public boolean isMyEnumSupported(ClassA aInstance)
{
    MyEnum aSupportedValues = aInstance.mySupportedValues();
    MyEnum mySupportedValue = getMyValue();
    return aSupportedValues contains mySupportedValue; //not sure how to check
}

Upvotes: 2

Views: 2111

Answers (3)

user3161880
user3161880

Reputation: 1055

Lightweight solution

public class Main {

    private enum Flag {
        VALUE1, VALUE2, VALUE3, VALUE4;

        int getBitFlag() {
            return 1 << ordinal();
        }

        boolean isSupported(int bitFlag) {
            return (0 != (getBitFlag() & bitFlag));
        }
    }

    private static int mySupportedValues() {
        return Flag.VALUE1.getBitFlag() | Flag.VALUE3.getBitFlag();
    }

    private static boolean isMyEnumSupported(Flag flag) {
        int supportedValues = mySupportedValues();
        return flag.isSupported(supportedValues);
    }

    public static void main(String[] args) {
        Flag toBeTested = Flag.VALUE3;
        System.out.println(isMyEnumSupported(toBeTested) ? "Supported" : "Not Supported");

        toBeTested = Flag.VALUE2;
        System.out.println(isMyEnumSupported(toBeTested) ? "Supported" : "Not Supported");
    }

}

EnumSet is based on collection framework and uses iterator when you check contains. Above solution uses bit-masks.

Note: Solution will work correctly only up to 32 values.

Upvotes: 3

swiedenfeld
swiedenfeld

Reputation: 308

It should work with slight modifications of your code.

public EnumSet<MyEnum> mySupportedValues() 
{
   return EnumSet.of(MyEnum.Value1, MyEnum.Value3);
}


public boolean isMyEnumSupported(ClassA aInstance)
{
   EnumSet<MyEnum> aSupportedValues = aInstance.mySupportedValues();
   MyEnum mySupportedValue = getMyValue();
   return aSupportedValues.contains(mySupportedValue);
}

https://docs.oracle.com/javase/8/docs/api/java/util/EnumSet.html EnumSetis a specialized Set class to take up enum constants.

Upvotes: 1

GhostCat
GhostCat

Reputation: 140427

Enums in Java are not just simple numerical values that you can "numerically" or together.

Your method thus must return some kind of "collection" of enum values, so it should read

public EnumSet<MyEnum> mySupportedValues() {
  return EnumSet.of(MyEnum.Value1, MyEnum.Value2);
}

instead. As "some collection" in this context pretty much boils down to EnumSet being the correct choice here.

And then your other method can simply use the contains() method of that EnumSet object you got from that other method.

Upvotes: 2

Related Questions