Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

EnumSet Efficiency

DZone's Guide to

EnumSet Efficiency

See how you can incorporate EnumSets into your code. In this example, we walk through implementing and combining roles with EnumSets.

· Java Zone
Free Resource

What every Java engineer should know about microservices: Reactive Microservices Architecture.  Brought to you in partnership with Lightbend.

EnumSet is a specialized set that only takes Enum elements. Look at the signature of this class.

public abstract class EnumSet<E extends Enum<E>>
extends AbstractSet<E>
implements Cloneable, Serializable

The beauty of EnumSet is that we can perform any collection operation like (add, remove, etc. ) on Enum. As EnumSet is a collection, it provides some Static methods by which we can do union, range check, none, etc. operations.

So by using EnumSet, we can dynamically add Enum elements, remove an element, or perform union operations.

So where can we use EnumSet? Well, one case is role and operation implementation. Not only that, say from the UI, we can manipulate it and create a new role with a new operation set.

To be specific, say I have three types of roles: Self, Friend, and Guest.

As you can guess, the owner of the profile gets the Self role, friends of the owner get the Friend role, and any unknown people get the Guest role. Now our Owner wants to create a new role for a few guests — a role where they can perform Friend operations as well as guest operations.

Implementation

Obviously, we can consider Bit representation for operations and check if a particular operation is valid for a role or not — the same way we can create a new role. I don’t go for that implementation, though. That will be very hard to maintain. Rather I will try to solve it through an EnumSet.

1. I create an Operation Enum where I store all the possible Operations.

package com.example.enumtest;
public enum Operation {
    VIEW_PROFILE,EDIT_PROFILE,DELETE_PROFILE,VIEW_ALBUM,EDIT_ALBUM,DELETE_ALBUM,COMMENT,RATE_PROFILE
}

2. Then I create a Role Enum and associate permitted operations with those roles.

package com.example.enumtest;
import java.util.EnumSet;
import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils.Collections;
public enum Role {
    SELF(EnumSet.of(Operation.VIEW_PROFILE,Operation.VIEW_ALBUM,Operation.EDIT_PROFILE,Operation.EDIT_ALBUM,Operation.DELETE_PROFILE,
            Operation.DELETE_ALBUM,Operation.COMMENT)),
    FRIEND(EnumSet.of(Operation.VIEW_ALBUM,Operation.VIEW_PROFILE,Operation.COMMENT)),
    GUEST(EnumSet.of(Operation.VIEW_PROFILE,Operation.RATE_PROFILE));

    EnumSet<Operation> operationSet;
    Role(EnumSet<Operation> operationSet){
        this.operationSet=operationSet;
    }
}

Look in my Role Constructor — I pass EnumSet<Operation>, and when we define the Enum element, I called a Static method on EnumSet. That will perform a union operation.

3. Now, each role has its set of operations, and we can easily print them by traversing operationSet, easily checking whether an operation is permitted:

operationSet.contains(operation)


4. Now I want to add a new Role that can perform both Friend and Guest operations.

package com.example.enumtest;
import java.util.EnumSet;
public class ProfileManager {
    public static void printOperationForCustomRole(EnumSet<Role>roles)
    {
        EnumSet<Operation> mergeSet = EnumSet.noneOf(Operation.class);
        for(Role role: roles)
        {
            for(Operation op : role.operationSet)
            mergeSet.add(op);
        }

        System.out.println(mergeSet);
    }
    public static void main(String[] args) {
        ProfileManager.printOperationForCustomRole(EnumSet.of(Role.FRIEND,Role.GUEST));
    }
}

Look how easily we can do it. Just pass Role.Friend and Role.Guest in the printOperationForCustomRole() role method and create a new Operation set that holds all the operations of Friend and Guest.

And for our output:

[VIEW_PROFILE, VIEW_ALBUM, COMMENT, RATE_PROFILE]


Microservices for Java, explained. Revitalize your legacy systems (and your career) with Reactive Microservices Architecture, a free O'Reilly book. Brought to you in partnership with Lightbend.

Topics:
enums ,java ,enumset ,roles

Published at DZone with permission of Shamik Mitra, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}