Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use static final unmodifiable Set as constant for checking variables value

I've often run through a validation pattern where, to be valid, some variables must contain one of a prefixed number of values.

PSEUDO CODE:
    IF x == CONSTANT_1 || X == CONSTANT_2 || ... || x == CONSTANT_N
    THEN X is valid

In order to avoid the chain of OR terms, I created a static final unmodifiable set, which contains all the constants:

public final static String CONSTANT_1 = *value* ;
public final static String CONSTANT_2 = *value* ;
...
public final static String CONSTANT_N = *value* ;

public final static Set SET_OF_CONSTANTS = Collections.unmodifiableSet(new HashSet(){
    private static final long serialVersionUID = 1L;
    {
        add(CONSTANT_1); 
        add(CONSTANT_2);
        ...
        add(CONSTANT_3);
    }
});

And I perform the check in the following way:

if(!SET_OF_CONSTANTS.contains(x)){ 
    //X NOT VALID 
}

I'd like to know if this is a good programming practice, if there are any alternatives, and if it's true that using a Hash Table query (O(1) in theory) instead of the OR terms-chain improves performance and maybe also code-readability.

like image 780
Luigi Massa Gallerano Avatar asked Dec 11 '12 12:12

Luigi Massa Gallerano


People also ask

How do you create a constant set in Java?

To make any variable a constant, we must use 'static' and 'final' modifiers in the following manner: Syntax to assign a constant value in java: static final datatype identifier_name = constant; The static modifier causes the variable to be available without an instance of it's defining class being loaded.

Can we use final with static?

The static keyword means the value is the same for every instance of the class. The final keyword means once the variable is assigned a value it can never be changed. The combination of static final in Java is how to create a constant value.

Why would it be OK to declare a final or static final variable as public?

public makes it accessible across other classes. static makes it uniform value across all the class instances. final makes it non-modifiable value. So basically it's a "constant" value which is same across all the class instances and which cannot be modified.

What's the purpose of private static final String variables?

Making anything "private" means it is only available from within the class it was defined, "static" makes that variable available from ANYWHERE in that class, and "final" does not allow that variable to be changed, adding the modifier "final" changes your "variable" to a "constant" due to it's constant value instead of ...


1 Answers

Overall, I think this is very good style.

There's not a huge difference, but I'd personally define SET_OF_CONSTANTS like so:

      public final static String CONSTANT_1 = "*value*";
      public final static String CONSTANT_2 = "*value*";
              ...
      public final static String CONSTANT_N = "*value*";

      public final static Set<String> SET_OF_CONSTANTS = Collections.unmodifiableSet(
        new HashSet<String>(Arrays.asList(
              CONSTANT_1, 
              CONSTANT_2,
                      ...
              CONSTANT_N
              )));

It's not entirely clear to me whether you even need the separate CONSTANT_1 constants, or whether you can simply fold the values into SET_OF_CONSTANTS.

As far as performance goes, I would not start optimizing anything until I've profiled the code on real data.

Finally, note that when x is a string, the following is probably incorrect:

IF x == CONSTANT_1 || x == CONSTANT_2 || ... || x == CONSTANT_N

Here, the == should probably be replaced with calls to equals().

like image 148
NPE Avatar answered Nov 15 '22 20:11

NPE