Skip to content

Commit 3dc7be3

Browse files
committed
Replace Right Enum with a enum-like class which allow creating new Right and provide an optimized Set and Map for them
1 parent 07bfd16 commit 3dc7be3

19 files changed

+1360
-247
lines changed

xwiki-platform-core/xwiki-platform-security/src/main/java/org/xwiki/security/AccessLevel.java

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,12 @@
2222
*/
2323
package org.xwiki.security;
2424

25-
import java.util.TreeSet;
25+
import java.lang.ref.ReferenceQueue;
26+
import java.lang.ref.WeakReference;
2627
import java.util.Collection;
2728
import java.util.Collections;
2829
import java.util.SortedSet;
29-
30-
import java.lang.ref.WeakReference;
31-
import java.lang.ref.ReferenceQueue;
30+
import java.util.TreeSet;
3231

3332
/**
3433
* Represents access level.
@@ -50,37 +49,25 @@
5049
*/
5150
public class AccessLevel implements RightCacheEntry, Cloneable, Comparable<AccessLevel>
5251
{
52+
/** Read-only flag/mask. */
53+
private static final int RO_MASK = 1 << 31;
54+
5355
/**
5456
* The default access levels.
5557
*/
56-
public static final AccessLevel DEFAULT_ACCESS_LEVEL;
58+
private static AccessLevel defaultAccessLevel = null;
5759

58-
/** Read-only flag/mask. */
59-
private static final int RO_MASK = 1 << 31;
60+
/**
61+
* The default access levels size. Check to update defaultAccessLevel if a new Right is added.
62+
*/
63+
public static int defaultAccessLevelSize;
6064

6165
/** Pool of existing instances. */
6266
private static SortedSet<ALWeakRef> pool = new TreeSet<ALWeakRef>();
6367

6468
/** Reference queue for removing cleared references. */
6569
private static ReferenceQueue<AccessLevel> refQueue = new ReferenceQueue<AccessLevel>();
6670

67-
static {
68-
DEFAULT_ACCESS_LEVEL = new AccessLevel()
69-
{
70-
{
71-
allow(Right.VIEW);
72-
allow(Right.EDIT);
73-
allow(Right.COMMENT);
74-
allow(Right.LOGIN);
75-
allow(Right.REGISTER);
76-
deny(Right.DELETE);
77-
deny(Right.ADMIN);
78-
deny(Right.PROGRAM);
79-
deny(Right.ILLEGAL);
80-
}
81-
} .getExistingInstance();
82-
}
83-
8471
/**
8572
* We use two consecutive bits to store an access level. The
8673
* high order bit indicate if the state of the level is
@@ -91,6 +78,18 @@ public class AccessLevel implements RightCacheEntry, Cloneable, Comparable<Acces
9178
* to the bits corresponding to the specific level.
9279
*/
9380
private int levels;
81+
82+
public static AccessLevel getDefaultAccessLevel()
83+
{
84+
if (defaultAccessLevel == null || Right.size() != defaultAccessLevelSize) {
85+
defaultAccessLevel = new AccessLevel();
86+
for (Right right : Right.values()) {
87+
defaultAccessLevel.set(right, right.getDefaultState());
88+
}
89+
defaultAccessLevel = defaultAccessLevel.getExistingInstance();
90+
}
91+
return defaultAccessLevel;
92+
}
9493

9594
/**
9695
* Weak reference for storing instances in a pool.
@@ -185,7 +184,7 @@ public AccessLevel getExistingInstance()
185184
*/
186185
private int shift(int value, Right right)
187186
{
188-
return value << (2 * right.getValue());
187+
return value << (2 * right.ordinal());
189188
}
190189

191190
/**

0 commit comments

Comments
 (0)