EnumSet to manage permissions

on waitingforcode.com

EnumSet to manage permissions

You're doing Java/C#/JavaScript and doing it great? But you're tired because of always facing the same problems. I was like that 4 years ago. I changed then to the data engineering field and it solved my existential problems :) If you want to follow my path, I prepared a course that will help you with that! Join the class!
Enumerations storage in Java can be achieved through classical collections, such as lists or queues. However, one specific class is adapted to manage only enums - EnumSet.

In this article we'll focus on this kind of enums storage. At the first part we'll present the major features of EnumSet. After that we'll implement it as a mechanism controlling permission bit mask.

EnumSet in Java

Typically, EnumSet is an implementation of java.util.Set interface adapted to working with enums. Only entries coming from one enumeration are allowed. The entries are read in the order of declaration. So be aware - if you change the declaration order and use the functions like range(), your application can misbehave.

Methods defined in EnumSet are mainly factory methods used to initialize EnumSet according to situation:

  • when empty EnumSet is needed, we can use noneOf method
  • for the cases when we need only some entries of given enum, we can use as well one from of() variations as already mentioned range() methods
  • when we need to copy given enum to EnumSet, we should opt for the use of allOf() method

EnumSet provides a lot of advantages over classical enums. Because it implements Iterable, Collection and Set interfaces, it contains a lot of useful behaviour defined inside them. For example, we can remove previously set elements with removeAll() method or simply check if EnumSet has one enum entry (contains()).

EnumSet and permission mask

To see one simple case of EnumSet use, we will use it as the main component for permissions system. Test cases presented in further code show the main features of EnumSet:

public class EnumSetTest {

  private enum Permissions {
      WRITE_ARTICLE, READ_ARTICLE, DELETE_ARTICLE,
      WRITE_CATEGORY, READ_CATEGORY, DELETE_CATEGORY;
  }

  private EnumSet<Permissions> adminPermissions = EnumSet.allOf(Permissions.class);

  private EnumSet<Permissions> writerPermissions = EnumSet.of(Permissions.WRITE_ARTICLE, 
    Permissions.WRITE_CATEGORY);

  private EnumSet<Permissions> readerPermissions = EnumSet.of(Permissions.READ_ARTICLE, 
    Permissions.READ_CATEGORY);

  private EnumSet<Permissions> noPermissions = EnumSet.noneOf(Permissions.class);

  @Test
  public void testIfHasAllPermissions() {
    assertThat(adminPermissions.containsAll(
      EnumSet.of(Permissions.WRITE_ARTICLE, Permissions.READ_ARTICLE, 
      Permissions.DELETE_ARTICLE))).isTrue();
  }

  @Test
  public void testIfCanWriteArticle() {
    assertThat(writerPermissions.contains(Permissions.WRITE_ARTICLE)).isTrue();
  }

  @Test
  public void testIfCantWriteArticle() {
    assertThat(readerPermissions.contains(Permissions.WRITE_ARTICLE)).isFalse();
  }

  @Test
  public void testSomebodyWithoutPermissions() {
    assertThat(noPermissions.contains(Permissions.WRITE_ARTICLE)).isFalse();
  }

  @Test
  public void testPermissionsEvolution() {
    EnumSet<Permissions> basicPermissions = EnumSet.noneOf(Permissions.class);
    assertThat(basicPermissions).hasSize(0);

    basicPermissions.add(Permissions.WRITE_ARTICLE);
    basicPermissions.add(Permissions.DELETE_ARTICLE);
    assertThat(basicPermissions.contains(Permissions.DELETE_ARTICLE)).isTrue();
    assertThat(basicPermissions.contains(Permissions.WRITE_ARTICLE)).isTrue();

    basicPermissions.remove(Permissions.DELETE_ARTICLE);
    basicPermissions.add(Permissions.WRITE_CATEGORY);
    assertThat(basicPermissions.contains(Permissions.DELETE_ARTICLE)).isFalse();
    assertThat(basicPermissions.contains(Permissions.WRITE_ARTICLE)).isTrue();

    basicPermissions.removeAll(Arrays.asList(Permissions.WRITE_CATEGORY, Permissions.WRITE_ARTICLE));
    assertThat(basicPermissions.isEmpty()).isTrue();
  }

  @Test
  public void testOnlyArticlePermissionsConstruction() {
    EnumSet<Permissions> onlyArticlePermissions = EnumSet.range(Permissions.WRITE_ARTICLE, 
    Permissions.DELETE_ARTICLE);

    assertThat(onlyArticlePermissions).hasSize(3);
    assertThat(onlyArticlePermissions)
      .containsOnly(Permissions.WRITE_ARTICLE, Permissions.READ_ARTICLE, 
    Permissions.DELETE_ARTICLE);
  }

}

In this short article we could discover the features of EnumSet. At the begin we saw main methods defined in this class. We learned that almost all of them are factory methods used to construct EnumSet instances. After that we were passing to practical case showing the use of EnumSet as main part of ACL mechanism.

Share on: