Skip to main content
summaryrefslogtreecommitdiffstats
blob: 0175ffcd75f6caeaa57538f5f8c4f4d26965125e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.framework.core.enums;

import java.util.HashMap;
import java.util.Map;

public enum PermissionEnum {

   NONE(5, "None"),
   READ(10, "Read"),
   WRITE(20, "Write"),
   FULLACCESS(30, "Full Access"),
   LOCK(40, "Lock"),
   DENY(65535, "Deny");

   // keeping this in sync with the number of permissions will ensure optimal memory usage
   private static final int COUNT = 4;

   private static final Map<Integer, PermissionEnum> rankToPermissionHash = new HashMap<Integer, PermissionEnum>(
         (int) (COUNT / .75) + 1, .75f);
   private static final Map<String, PermissionEnum> NameToPermissionHash = new HashMap<String, PermissionEnum>(
         (int) (COUNT / .75) + 1, .75f);
   private static final String[] NAME_ARRAY;

   static {
      NAME_ARRAY = new String[values().length];

      int i = 0;
      for (PermissionEnum permission : values()) {
         rankToPermissionHash.put(permission.getPermId(), permission);
         NameToPermissionHash.put(permission.getName(), permission);
         NAME_ARRAY[i++] = permission.getName();
      }
   }

   private int permissionId;
   private String name;
   public boolean add;

   PermissionEnum(int permissionId, String name) {
      this.permissionId = permissionId;
      this.name = name;
   }

   public int getRank() {
      return permissionId;
   }

   public String getName() {
      return name;
   }

   public static PermissionEnum getPermission(int permissionId) {
      return rankToPermissionHash.get(permissionId);
   }

   public static PermissionEnum getPermission(String name) {
      return NameToPermissionHash.get(name);
   }

   public boolean matches(PermissionEnum toMatch) {
      boolean hasPermission = false;

      if (toMatch == PermissionEnum.READ && this == PermissionEnum.LOCK) {
         hasPermission = true;
      } else if (this == null || this == PermissionEnum.LOCK) {
         hasPermission = false;
      } else {
         hasPermission = this.getRank() >= toMatch.getRank() && !this.equals(PermissionEnum.DENY);
      }
      return hasPermission;
   }

   public static String[] getPermissionNames() {
      return NAME_ARRAY;
   }

   public int getPermId() {
      return permissionId;
   }
}

Back to the top