Using EnumSet by Example

In previous example I have explained using enum using color constants. Java util collection has provides EnumSet, it is a specialized Set implementation for use with enum types. In this post I have explained usage of EnumSet using an example.
A text can have multiple attributes like bold, italic, underlined etc. Given a text can have these properties, defined a sample Text class with bit fields as text properties
public class Text {
    //defining Text properties using int
    public static final  int BOLD = 1<<0;
    public static final  int ITALIC = 1<<1;
    public static final int UNDERLINED = 1<<2;
    
    private int style;
    public Text(int style){
        this.style = style;
    }
    
    public boolean isBold(){
        return (BOLD&style)==BOLD;
    }
    
    public boolean isItalic(){
        return (ITALIC&style)==ITALIC;
    }
    
    public boolean isUnderlined(){
        return (UNDERLINED&style)==UNDERLINED;
    }
    
    public static void main(String[] args) {
        Text t = new Text(BOLD|ITALIC);
        System.out.println("isBold = " + t.isBold());
        System.out.println("is Italic = "+ t.isItalic());
        System.out.println("is Underlined = " + t.isUnderlined());
        
    }
 
}

Above class uses bit operation to apply properties. Usage of the Text class is shown in main() method.Text’s constructor accepts styles and based on bid operations the properties styles are decided. Output of above program

isBold = true
is Italic = true
is Underlined = false


As above program using bit field, it has all disadvantages of int enum constants as discussed in previous post. Above implementation is prone to error as well as not type safe.
Above functionalities can be achieved by EnumSet, EnumSet provides efficient way of representing set of values from same enum type.

public class Text {
    public static enum Style {
        BOLD, ITALIC, UNDERLINED
    }
    
    Set<Style> styles = null;
    public Text(Set<Style> styles){
        this.styles = styles;
    }
    
    public boolean isBold(){
        return styles.contains(Style.BOLD);
    }
    
    public boolean isItalic(){
        return styles.contains(Style.ITALIC);
    }
    
    public boolean isUnderlined(){
        return styles.contains(Style.UNDERLINED);
    }
    
    public static void main(String[] args) {
        Text t = new Text(EnumSet.of(Style.BOLD,Style.UNDERLINED));
        System.out.println("isBold = " + t.isBold());
        System.out.println("is Italic = "+ t.isItalic());
        System.out.println("is Underlined = " + t.isUnderlined());
        
    }
}

Unlike previous implementation, we have defined enum for styles constants and Text’s constructor accept Set<Style> as parameter.EnumSet.of(…) returns of instance of EnumSet which acts as Set of constants of Style enum. This instance is equivalent to any other Collection classes and supports all corresponding functionalities like contains(). This approach provides additional benefits compared to previous implantations.
  • Type Safe
  • All benefits of Collection framework
  • Error safe

Comments

Popular posts from this blog

State Design Pattern by Example

Eclipse command framework core expression: Property tester

Composite Design Pattern by example