whatkai
whatkai

Reputation: 179

compare values in enum java

So here is my situation I am comparing two situations of the WallEnums to the rest of them.

import static com.gowallgo.enumtypes.WallEnums.CAW;
"" ( and the rest )


   /**
     * {@link Set} of cancel {@link WallEnums}s
     */
    private static final Set<WallEnums> WALL_CODES = asSet(RES, CAW, AAP, ASV, CQP, OQR);


// more stuff and then I use it here . 

if (wallEnum != WALL_CODES.contains(wallEnum)){}

this begs for refactoring . where should I start so I don't need to make a static import for each code ?

Upvotes: 2

Views: 185

Answers (3)

fps
fps

Reputation: 34480

Use EnumSet:

// Do not import anything

// This creates a Set that contains all posible values
// In case you need a subset use: EnumSet.of(WallEnums.RES, WallEnums.CAW, etc)
private static final Set<WallEnums> WALL_CODES = EnumSet.allOf(WallEnums.class);

// Later...
if (WALL_CODES.contains(someWallEnum)) {
    // Do stuff if someWallEnum belongs to WALL_CODES set
}

This code creates a set of enums using optimized EnumSet class. Then you could use any Set operation as usual, i.e contains().

Upvotes: 1

SMA
SMA

Reputation: 37103

You dont have to import each enum type, You could try importing every element of enum using:

import static com.gowallgo.enumtypes.WallEnums.*;

Upvotes: 1

MJ01
MJ01

Reputation: 21

If you don't want to import every enum value you can use

import com.gowallgo.enumtypes.WallEnums

WallEnums enum = WallEnums.RES;  //Now you have to use EnumName.VALUE

Upvotes: 2

Related Questions