Enum boolean properties problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Thong Nguyen
    New Bees
    • Jan 2026
    • 6

    #1

    Enum boolean properties problem

    On a webapp where we often need to condition some server logic based on the page that is going to be returned to the user.


    Code:
    [FONT=Calibri][I][FONT=Tahoma][COLOR=#333333]public class PageCodes {
    public static final String FOF "FOF";
    public static final String FOMS = "FOM";
    public static final String BKG = "BKG";
    public static final String ITC = "ITC";
    public static final String PUR = "PUR";
    // etc..
    }[/COLOR][/FONT][/I][/FONT]
    code like this

    Code:
    [FONT=Calibri][I][FONT=Tahoma][COLOR=#333333]if (PageCode.PUR.equals(destinationPageCode) || PageCodes.ITC.equals(destinationPageCode)) {
    // some code with no obvious intent
    }[/COLOR][/FONT][/I]
    [I][FONT=Tahoma][COLOR=#333333]if (PageCode.FOF.equals(destinationPageCode) || PageCodes.FOM.equals(destinationPageCode)) {
    // some other code with no obvious intent either
    }[/COLOR][/FONT][/I][/FONT]
    Does anyone have a better solution?
  • Kevin Sours
    New Bees
    • Jan 2026
    • 6

    #2
    Enum for your page features instead of using booleans. The below given code will makes it easy to add/remove a feature to a page and makes the page definitions instantly readable even when there are 30-40 potential features.


    Code:
    [FONT=Calibri][I][FONT=Tahoma][COLOR=#333333]public enum PageFeature {
    AVAIL_PAGE,
    SHOPPING_CART;
    }
    public enum Page {
    FOF(AVAIL_PAGE),
    FOM(AVAIL_PAGE),
    BKG(),
    PUR(SHOPPING_CART, AVAIL_PAGE),
    private final EnumSet<PageFeature> features;
    PageCode(PageFeature ... features) {
    this.features = EnumSet.copyOf(Arrays.asList(features));
    }
    public boolean hasFeature(PageFeature feature) {
    return features.contains(feature);
    }
    }[/COLOR][/FONT][/I][/FONT]

    Comment

    Working...