EnumSet to manage permissions

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:

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.

If you liked it, you should read:

The comments are moderated. I publish them when I answer, so don't worry if you don't see yours immediately :)

📚 Newsletter Get new posts, recommended reading and other exclusive information every week. SPAM free - no 3rd party ads, only the information about waitingforcode!