View Javadoc

1   /*
2    * Copyright 2015 Data Archiving and Networked Services (an institute of
3    * Koninklijke Nederlandse Akademie van Wetenschappen), King's College London,
4    * Georg-August-Universitaet Goettingen Stiftung Oeffentlichen Rechts
5    *
6    * Licensed under the EUPL, Version 1.1 or – as soon they will be approved by
7    * the European Commission - subsequent versions of the EUPL (the "Licence");
8    * You may not use this work except in compliance with the Licence.
9    * You may obtain a copy of the Licence at:
10   *
11   * https://joinup.ec.europa.eu/software/page/eupl
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the Licence is distributed on an "AS IS" basis,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the Licence for the specific language governing
17   * permissions and limitations under the Licence.
18   */
19  
20  package eu.ehri.project.acl;
21  
22  import com.fasterxml.jackson.annotation.JsonCreator;
23  import com.fasterxml.jackson.annotation.JsonValue;
24  import com.google.common.collect.Sets;
25  
26  import java.util.Set;
27  
28  /**
29   * Convenience wrapper for the permission set data structure, which
30   * looks like:
31   * <p>
32   * <pre>
33   *     <code>
34   *  {
35   *      contentType -&gt; [perms...],
36   *      ...
37   *  }
38   *     </code>
39   * </pre>
40   */
41  public final class ItemPermissionSet {
42  
43      private final Set<PermissionType> set;
44  
45      @JsonCreator
46      public static ItemPermissionSet from(Set<PermissionType> set) {
47          return new ItemPermissionSet(set);
48      }
49  
50      private ItemPermissionSet(Set<PermissionType> permissionSet) {
51          set = Sets.immutableEnumSet(permissionSet);
52      }
53  
54      public boolean has(PermissionType permissionType) {
55          return set.contains(permissionType);
56      }
57  
58      @JsonValue
59      public Set<PermissionType> asSet() {
60          return set;
61      }
62  
63      @Override
64      public boolean equals(Object o) {
65          if (this == o) return true;
66          if (o == null || getClass() != o.getClass()) return false;
67  
68          ItemPermissionSet that = (ItemPermissionSet) o;
69  
70          return set.equals(that.set);
71      }
72  
73      @Override
74      public int hashCode() {
75          return set.hashCode();
76      }
77  
78      @Override
79      public String toString() {
80          return "<ItemPermissions: " + set + ">";
81      }
82  }