DZone
Java Zone
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
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > 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 · Java Zone · Tutorial
Like (13)
Save
Tweet
15.80K 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

  • Remote Debugging and Developer Observability
  • What's the Difference Between Static Class vs. Singleton Patterns in C#?
  • Event-Driven Hello World Program
  • Five Tips to Fasten Your Skewed Joins in Apache Spark

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • 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:

DZone.com is powered by 

AnswerHub logo