Java EnumSet-添加并检查集合是否包含标志



我正在努力了解如何正确地将EnumSet用于等价于C#的Flags。以下是我的实现和使用它的方法。正如您所看到的,检查标志是否存在并不能正常工作。你能告诉我我做错了什么吗?

我有以下带有EnumSet的Enum:

public enum ExcelRangeBordersFlag {
    BORDER_LEFT,
    BORDER_BOTTOM,
    BORDER_TOP,
    BORDER_RIGHT;
    public static final EnumSet<ExcelRangeBordersFlag> SELECTED_BORDERS = EnumSet.noneOf(ExcelRangeBordersFlag.class);
}

(这是否正确实施?)

我现在希望能够做一些事情,比如添加到集合中,并检查集合是否包含值。但是,这不起作用:

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();
    }

最后一个方法显示所有ExcelRangeBorder类型都包含在内,即使我执行以下操作:

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

输出:

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

我认为问题的根源在于SELECTED_BORDERS不应该是ExcelRangeBordersFlag中的静态字段,而应该是您传递的东西。例如,看起来你应该写

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;
}

看起来,您正试图传递一个ExcelRangeBordersFlag,就好像它是一组标志值一样,但这并不是它的工作方式。ExcelRangeBordersFlag是类似于BORDER_BOTTOMBORDER_LEFT的值。你应该绕过他们中的一个Set

相关内容

  • 没有找到相关文章

最新更新