Search code examples
javaenumset

Java EnumSet - Add and check if set contains a flag


I am trying to understand how to properly use EnumSet for an equivalent to C#'s Flags. Here is my implementation and methods that use this. As you can see, checking if a flag exists is not working properly. Can you please let me know what I am doing wrong?

I have the following Enum with an EnumSet:

public enum ExcelRangeBordersFlag {
    BORDER_LEFT,
    BORDER_BOTTOM,
    BORDER_TOP,
    BORDER_RIGHT;

    public static final EnumSet<ExcelRangeBordersFlag> SELECTED_BORDERS = EnumSet.noneOf(ExcelRangeBordersFlag.class);
}

(Is this implemented correctly?)

I now want to be able to do things like add to the set, and check to see if the set contains a value. But, this isn't working:

private static ExcelRangeBordersFlag getBordersFlag(boolean bottom, boolean top, boolean left, boolean right){
        ExcelRangeBordersFlag results =  null; 

        if (bottom){
            results.SELECTED_BORDERS.add(ExcelRangeBordersFlag.BORDER_BOTTOM);
        }

        if (top){
            results.SELECTED_BORDERS.add(ExcelRangeBordersFlag.BORDER_TOP);
        }

        if (left){
            results.SELECTED_BORDERS.add(ExcelRangeBordersFlag.BORDER_LEFT);
        }

        if (right){
            results.SELECTED_BORDERS.add(ExcelRangeBordersFlag.BORDER_RIGHT);
        }

        System.out.println(results);

        return results;
    }


private static void MergeAndWriteToCell(Sheet sh, int sRow, int eRow, int sCell, int eCell, ExcelRangeBordersFlag borders , String text){
        CellRangeAddress cellRangeAddress = new CellRangeAddress(sRow, eRow, sCell, eCell);
        sh.addMergedRegion(cellRangeAddress);
        Row row = sh.getRow(sRow);
        Cell cell = row.getCell(sCell);
        cell.setCellValue(text);

        if (borders.SELECTED_BORDERS.contains(ExcelRangeBordersFlag.BORDER_BOTTOM)){
            RegionUtil.setBorderBottom(CellStyle.BORDER_MEDIUM, cellRangeAddress, sh, wb);
            System.out.println("Contains bottom.");
        }

        if (borders.SELECTED_BORDERS.contains(ExcelRangeBordersFlag.BORDER_LEFT)){
            RegionUtil.setBorderLeft(CellStyle.BORDER_MEDIUM, cellRangeAddress, sh, wb);
            System.out.println("Contains left.");
        }

        if (borders.SELECTED_BORDERS.contains(ExcelRangeBordersFlag.BORDER_TOP)){
            RegionUtil.setBorderTop(CellStyle.BORDER_MEDIUM, cellRangeAddress, sh, wb);
            System.out.println("Contains top.");
        }

        if (borders.SELECTED_BORDERS.contains(ExcelRangeBordersFlag.BORDER_RIGHT)){
            RegionUtil.setBorderRight(CellStyle.BORDER_MEDIUM, cellRangeAddress, sh, wb);
            System.out.println("Contains right.");
        }

        System.out.println();

    }

The last method shows that all ExcelRangeBorder types are contained, even when I do the following:

borders = getBordersFlag(false, false, false, false);
        MergeAndWriteToCell(sh, 4,4,1,3, borders, "Percent of Original List Price Received*");

Output:

null
Contains bottom.
Contains left.
Contains top.
Contains right.

Solution

  • I think the root of your problem is that SELECTED_BORDERS should not be a static field in ExcelRangeBordersFlag, but rather, should be the thing you pass around. For example, it looks like you should write

    private static Set<ExcelRangeBordersFlag> getBordersFlag(
          boolean bottom, boolean top, boolean left, boolean right){
        ExcelRangeBordersFlag results = EnumSet.noneOf(ExcelRangeBordersFlag.class); 
    
        if (bottom){
            results.add(ExcelRangeBordersFlag.BORDER_BOTTOM);
        }
    
        if (top){
            results.add(ExcelRangeBordersFlag.BORDER_TOP);
        }
    
        if (left){
            results.add(ExcelRangeBordersFlag.BORDER_LEFT);
        }
    
        if (right){
            results.add(ExcelRangeBordersFlag.BORDER_RIGHT);
        }
    
        System.out.println(results);
    
        return results;
    }
    

    It looks ike you're trying to pass around an ExcelRangeBordersFlag as if it were a set of the flag values, but that's not how it works. An ExcelRangeBordersFlag is a value like BORDER_BOTTOM or BORDER_LEFT. You should be passing around a Set of them.