DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Languages
  4. EnumSet Efficiency

EnumSet Efficiency

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

Shamik Mitra user avatar by
Shamik Mitra
·
Oct. 29, 16 · Tutorial
Like (13)
Save
Tweet
Share
16.05K Views

Join the DZone community and get the full member experience.

Join For Free

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]


Efficiency (statistics) Element Implementation Pass (software) Self (programming language) Profile (engineering)

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

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • The 31 Flavors of Data Lineage and Why Vanilla Doesn’t Cut It
  • Core Machine Learning Metrics
  • How to Quickly Build an Audio Editor With UI
  • Why Every Fintech Company Needs DevOps

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: