-1

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.
blgrnboy
  • 4,877
  • 10
  • 43
  • 94
  • 1
    I don't understand what you're trying to ask. – user1231232141214124 Dec 14 '15 at 17:46
  • getBordersFlag should give me back the enum with a proper EnumSet. The MergeAndWriteToCell should be able to see if the set contains any one of the border values. – blgrnboy Dec 14 '15 at 17:48
  • 1
    That isn't a question – user1231232141214124 Dec 14 '15 at 17:48
  • The example shows that this isn't working. The question is, how do I make it work? – blgrnboy Dec 14 '15 at 17:49
  • 1
    Have you actually tried debugging your code? – user1231232141214124 Dec 14 '15 at 17:50
  • In your `getBordersFlag` method what is the purpose of `ExcelRangeBordersFlag results = null;`? Your code `results.SELECTED_BORDERS.add(...)` is same as `ExcelRangeBordersFlag.SELECTED_BORDERS.add(...)` since `SELECTED_BORDERS` is static field of `ExcelRangeBordersFlag` class (enum). – Pshemo Dec 14 '15 at 17:51
  • Also could you post [minimal but **full** code example which will let us reproduce your problem](http://sscce.org) (so we could simply copy-paste it). Your current code contains things like `CellStyle.BORDER_MEDIUM` which are not part of your example. – Pshemo Dec 14 '15 at 17:54

1 Answers1

2

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.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413