0

I am currently working on rewriting some codes in an Util class.

It has static Sets of configurable property data defined, that needs to be used based on certain boolean flags.

    static Set<String> oneDayProperties = D
    static Set<String> oneDayPropertiesWithB = D,B 
    static Set<String> oneDayPropertiesForM = D,B
    static Set<String> oneDayPropertiesForMWithB = D,B
    static Set<String> oneDayPropertiesForL = D,B
    static Set<String> oneDayPropertiesForLWithB = D,B
    static Set<String> oneDayPropertiesNonDefault = D,B
    .
    .
    static Set<String> twoDayProperties = D,B
    static Set<String> twoDayPropertiesWithB = D,B
    static Set<String> twoDayPropertiesForM = D,B
    static Set<String> twoDayPropertiesForMWithB = D,B
    static Set<String> twoDayPropertiesForL = D,B
    static Set<String> twoDayPropertiesForLWithB = D,B
    static Set<String> twoDayPropertiesNonDefault = D,B
    ...

Which Set to return is based on the boolean properties sent as parameters to methods like below. There are multiple methods like this. All methods have the same set of parameters and return Set. Problem is if I need to add one more condition, it will make the code further ugly. Simple boolean parameters can be replaced with enums, but can't think of anything in this case because of these multi-dimensional conditions.

public static Set<String> get1DayProperties(boolean isForL, boolean isForM, boolean isBIncluded, String area) {
    if (DataCacheConstants.DEFAULT_AREA.equals(area)) {
        if (!isForL) {
            return !isForM ? (!isBIncluded ? oneDayProperties : oneDayPropertiesWithB)
                            : (!isBIncluded ? oneDayPropertiesForM : oneDayPropertiesForMWithB);
        }
        else {
            return !isBIncluded ? oneDayPropertiesForL : oneDayPropertiesForLWithB;
        }
    }
    else {
        return oneDayPropertiesNonDefault;
    }
}

public static Set<String> get2DayProperties(boolean isForL, boolean isForM, boolean isBIncluded, String area) {
                .
                .
                .
                
public static Set<String> get3DayProperties(boolean isForL, boolean isForM, boolean isBIncluded, String area) {
                .
                .
                .

Any help with ideas is much appreciated.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • I don't remember the name of the pattern, but you create a class for each Set of properties. The class has two methods. The first method tests if this Set of properties is valid for the conditions, and returns a boolean. The second method returns the Set of properties. You then add all of the classes to a List with a factory. You iterate through the List, and the first class that passes the test method returns the Set of properties. – Gilbert Le Blanc Jul 26 '20 at 13:49
  • @GilbertLeBlanc that's in line with what i wanted. Will give it a try. Thanks! – DOrchid Sep 09 '20 at 15:43

0 Answers0