Author: angela
Date: Tue Apr 30 09:15:29 2019
New Revision: 1858411

URL: http://svn.apache.org/viewvc?rev=1858411&view=rev
Log:
OAK-8284 : Improvements to EntryPredicate

Modified:
    
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/security/authorization/permission/CompiledPermissionImpl.java
    
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/security/authorization/permission/EntryPredicate.java
    
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/authorization/permission/EntryPredicateTest.java
    
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/authorization/permission/PermissionEntryProviderImplTest.java

Modified: 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/security/authorization/permission/CompiledPermissionImpl.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/security/authorization/permission/CompiledPermissionImpl.java?rev=1858411&r1=1858410&r2=1858411&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/security/authorization/permission/CompiledPermissionImpl.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/security/authorization/permission/CompiledPermissionImpl.java
 Tue Apr 30 09:15:29 2019
@@ -160,7 +160,7 @@ final class CompiledPermissionImpl imple
         return new RepositoryPermission() {
             @Override
             public boolean isGranted(long repositoryPermissions) {
-                EntryPredicate predicate = new EntryPredicate();
+                EntryPredicate predicate = EntryPredicate.create();
                 return hasPermissions(getEntryIterator(predicate), predicate, 
repositoryPermissions, null);
             }
         };
@@ -284,7 +284,7 @@ final class CompiledPermissionImpl imple
 
     @Override
     public boolean isGranted(@NotNull String path, long permissions) {
-        EntryPredicate predicate = new EntryPredicate(path, 
Permissions.respectParentPermissions(permissions));
+        EntryPredicate predicate = EntryPredicate.create(path, 
Permissions.respectParentPermissions(permissions));
         return hasPermissions(getEntryIterator(predicate), predicate, 
permissions, path);
     }
 
@@ -302,7 +302,7 @@ final class CompiledPermissionImpl imple
     //------------------------------------------------------------< private 
>---
 
     private boolean internalIsGranted(@NotNull Tree tree, @Nullable 
PropertyState property, long permissions) {
-        EntryPredicate predicate = new EntryPredicate(tree, property, 
Permissions.respectParentPermissions(permissions));
+        EntryPredicate predicate = EntryPredicate.create(tree, property, 
Permissions.respectParentPermissions(permissions));
         return hasPermissions(getEntryIterator(predicate), predicate, 
permissions, tree.getPath());
     }
 
@@ -400,8 +400,8 @@ final class CompiledPermissionImpl imple
     @NotNull
     private PrivilegeBits getPrivilegeBits(@Nullable Tree tree) {
         EntryPredicate pred = (tree == null)
-                ? new EntryPredicate()
-                : new EntryPredicate(tree, null, false);
+                ? EntryPredicate.create()
+                : EntryPredicate.create(tree, null, false);
         Iterator<PermissionEntry> entries = getEntryIterator(pred);
 
         PrivilegeBits allowBits = PrivilegeBits.getInstance();
@@ -566,21 +566,21 @@ final class CompiledPermissionImpl imple
 
         @Override
         public boolean isGranted(long permissions) {
-            EntryPredicate predicate = new EntryPredicate(tree, null, 
Permissions.respectParentPermissions(permissions));
+            EntryPredicate predicate = EntryPredicate.create(tree, null, 
Permissions.respectParentPermissions(permissions));
             Iterator<PermissionEntry> it = getIterator(predicate);
             return hasPermissions(it, predicate, permissions, tree.getPath());
         }
 
         @Override
         public boolean isGranted(long permissions, @NotNull PropertyState 
property) {
-            EntryPredicate predicate = new EntryPredicate(tree, property, 
Permissions.respectParentPermissions(permissions));
+            EntryPredicate predicate = EntryPredicate.create(tree, property, 
Permissions.respectParentPermissions(permissions));
             Iterator<PermissionEntry> it = getIterator(predicate);
             return hasPermissions(it, predicate, permissions, tree.getPath());
         }
 
         //--------------------------------------------------------< private 
>---
         private Iterator<PermissionEntry> getIterator(@Nullable PropertyState 
property, long permissions) {
-            EntryPredicate predicate = new EntryPredicate(tree, property, 
Permissions.respectParentPermissions(permissions));
+            EntryPredicate predicate = EntryPredicate.create(tree, property, 
Permissions.respectParentPermissions(permissions));
             return getIterator(predicate);
         }
 

Modified: 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/security/authorization/permission/EntryPredicate.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/security/authorization/permission/EntryPredicate.java?rev=1858411&r1=1858410&r2=1858411&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/security/authorization/permission/EntryPredicate.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/security/authorization/permission/EntryPredicate.java
 Tue Apr 30 09:15:29 2019
@@ -27,67 +27,65 @@ import org.jetbrains.annotations.Nullabl
  * Predicate used to evaluation if a given {@code PermissionEntry} matches
  * the specified tree, property or path.
  */
-final class EntryPredicate implements Predicate<PermissionEntry> {
-
-    private final Tree tree;
-    private final PropertyState property;
-    private final String path;
-
-    private final String parentPath;
-    private final Tree parent;
-    private final boolean respectParent;
-
-    EntryPredicate() {
-        this(null, null, null, false);
-    }
-
-    EntryPredicate(@NotNull Tree tree, @Nullable PropertyState property, 
boolean respectParent) {
-        this(tree, property, tree.getPath(), respectParent);
-    }
-
-    EntryPredicate(@NotNull String path, boolean respectParent) {
-        this(null, null, path, respectParent);
-    }
-
-    private EntryPredicate(@Nullable Tree tree, @Nullable PropertyState 
property,
-                           @Nullable String path, boolean respectParent) {
-        this.tree = tree;
-        this.property = property;
-        this.path = path;
-
-        if (respectParent) {
-            parentPath = (path == null || "/".equals(path)) ? null : 
PathUtils.getParentPath(path);
-            parent = (tree == null || tree.isRoot()) ? null : tree.getParent();
-        } else {
-            parentPath = null;
-            parent = null;
-        }
-        this.respectParent = parent != null || parentPath != null;
-    }
+interface EntryPredicate extends Predicate<PermissionEntry> {
 
     @Nullable
-    String getPath() {
-        return path;
-    }
+    String getPath();
 
-    //----------------------------------------------------------< Predicate 
>---
-    @Override
-    public boolean apply(@Nullable PermissionEntry entry) {
-        return apply(entry, true);
+    default boolean apply(@Nullable PermissionEntry entry) {
+        return entry != null && apply(entry, true);
     }
 
-    public boolean apply(@Nullable PermissionEntry entry, boolean 
respectParent) {
-        if (entry == null) {
-            return false;
-        }
-        respectParent &= this.respectParent;
-
-        if (tree != null) {
-            return entry.matches(tree, property) || (respectParent && parent 
!= null && entry.matches(parent, null));
-        } else if (path != null) {
-            return entry.matches(path) || (respectParent && parentPath != null 
&& entry.matches(parentPath));
-        } else {
-            return entry.matches();
-        }
+    boolean apply(@NotNull PermissionEntry entry, boolean respectParent);
+
+    static EntryPredicate create() {
+        return new EntryPredicate() {
+            @Nullable
+            @Override
+            public String getPath() {
+                return null;
+            }
+
+            @Override
+            public boolean apply(@NotNull PermissionEntry entry, boolean 
respectParent) {
+                return entry.matches();
+            }
+        };
+    }
+
+    static EntryPredicate create(@NotNull Tree tree, @Nullable PropertyState 
property, boolean respectParent) {
+        Tree parent = (!respectParent || tree.isRoot()) ? null : 
tree.getParent();
+        boolean rp = respectParent && parent != null;
+        return new EntryPredicate() {
+            @NotNull
+            @Override
+            public String getPath() {
+                return tree.getPath();
+            }
+
+            @Override
+            public boolean apply(@NotNull PermissionEntry entry, boolean 
respectParent) {
+                respectParent &= rp;
+                return entry.matches(tree, property) || (respectParent && 
entry.matches(parent, null));
+            }
+        };
+    }
+
+    static EntryPredicate create(@NotNull String path, boolean respectParent) {
+        String parentPath = (!respectParent || 
PathUtils.ROOT_PATH.equals(path)) ? null : PathUtils.getParentPath(path);
+        boolean rp = respectParent && parentPath != null;
+        return new EntryPredicate() {
+            @NotNull
+            @Override
+            public String getPath() {
+                return path;
+            }
+
+            @Override
+            public boolean apply(@NotNull PermissionEntry entry, boolean 
respectParent) {
+                respectParent &= rp;
+                return entry.matches(path) || (respectParent && 
entry.matches(parentPath));
+            }
+        };
     }
 }

Modified: 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/authorization/permission/EntryPredicateTest.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/authorization/permission/EntryPredicateTest.java?rev=1858411&r1=1858410&r2=1858411&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/authorization/permission/EntryPredicateTest.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/authorization/permission/EntryPredicateTest.java
 Tue Apr 30 09:15:29 2019
@@ -31,7 +31,6 @@ import static org.junit.Assert.assertNul
 import static org.junit.Assert.assertTrue;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.never;
-import static org.mockito.Mockito.spy;
 import static org.mockito.Mockito.times;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
@@ -55,14 +54,12 @@ public class EntryPredicateTest {
 
     @Test
     public void testPredicateRepositoryLevel() {
-        EntryPredicate pred = new EntryPredicate();
+        EntryPredicate pred = EntryPredicate.create();
         assertNull(pred.getPath());
 
         when(pattern.matches()).thenReturn(true);
 
         assertFalse(pred.apply(null));
-        assertFalse(pred.apply(null, true));
-        assertFalse(pred.apply(null, false));
 
         assertTrue(pred.apply(entry));
         assertTrue(pred.apply(entry, true));
@@ -73,7 +70,7 @@ public class EntryPredicateTest {
 
     @Test
     public void testPredicatePathRespectParent() {
-        EntryPredicate pred = new EntryPredicate(path, true);
+        EntryPredicate pred = EntryPredicate.create(path, true);
         assertEquals(path, pred.getPath());
 
         // pattern neither matches path nor parent path
@@ -89,8 +86,6 @@ public class EntryPredicateTest {
         when(pattern.matches(parentPath)).thenReturn(true);
 
         assertFalse(pred.apply(null));
-        assertFalse(pred.apply(null, true));
-        assertFalse(pred.apply(null, false));
 
         assertTrue(pred.apply(entry));
         assertTrue(pred.apply(entry, true));
@@ -118,7 +113,7 @@ public class EntryPredicateTest {
 
     @Test
     public void testPredicatePathDontRespectParent() {
-        EntryPredicate pred = new EntryPredicate(path, false);
+        EntryPredicate pred = EntryPredicate.create(path, false);
         assertEquals(path, pred.getPath());
 
         // pattern neither matches path nor parent path
@@ -134,8 +129,6 @@ public class EntryPredicateTest {
         when(pattern.matches(parentPath)).thenReturn(true);
 
         assertFalse(pred.apply(null));
-        assertFalse(pred.apply(null, true));
-        assertFalse(pred.apply(null, false));
 
         assertTrue(pred.apply(entry));
         assertTrue(pred.apply(entry, true));
@@ -168,7 +161,7 @@ public class EntryPredicateTest {
         PropertyState ps = mock(PropertyState.class);
         when(ps.getName()).thenReturn("property");
 
-        EntryPredicate pred = new EntryPredicate(tree, ps, true);
+        EntryPredicate pred = EntryPredicate.create(tree, ps, true);
         assertEquals(path, pred.getPath());
 
         // pattern neither matches path nor parent path
@@ -186,8 +179,6 @@ public class EntryPredicateTest {
         when(pattern.matches(parent, null)).thenReturn(true);
 
         assertFalse(pred.apply(null));
-        assertFalse(pred.apply(null, true));
-        assertFalse(pred.apply(null, false));
 
         assertTrue(pred.apply(entry));
         assertTrue(pred.apply(entry, true));
@@ -223,7 +214,7 @@ public class EntryPredicateTest {
         PropertyState ps = mock(PropertyState.class);
         when(ps.getName()).thenReturn("property");
 
-        EntryPredicate pred = new EntryPredicate(tree, ps,false);
+        EntryPredicate pred = EntryPredicate.create(tree, ps,false);
         assertEquals(path, pred.getPath());
 
         // pattern neither matches path nor parent path
@@ -241,8 +232,6 @@ public class EntryPredicateTest {
         when(pattern.matches(parent, null)).thenReturn(true);
 
         assertFalse(pred.apply(null));
-        assertFalse(pred.apply(null, true));
-        assertFalse(pred.apply(null, false));
 
         assertTrue(pred.apply(entry));
         assertTrue(pred.apply(entry, true));
@@ -273,7 +262,7 @@ public class EntryPredicateTest {
 
     @Test
     public void testPredicateRootPath() {
-        EntryPredicate pred = new EntryPredicate(PathUtils.ROOT_PATH, true);
+        EntryPredicate pred = EntryPredicate.create(PathUtils.ROOT_PATH, true);
         assertEquals(PathUtils.ROOT_PATH, pred.getPath());
 
         // pattern doesn't match path
@@ -295,7 +284,7 @@ public class EntryPredicateTest {
 
     @Test
     public void testPredicateRootPathDontRespectParent() {
-        EntryPredicate pred = new EntryPredicate(PathUtils.ROOT_PATH, false);
+        EntryPredicate pred = EntryPredicate.create(PathUtils.ROOT_PATH, 
false);
         assertEquals(PathUtils.ROOT_PATH, pred.getPath());
 
         // pattern doesn't match path
@@ -320,7 +309,7 @@ public class EntryPredicateTest {
         Tree tree = mockTree(PathUtils.ROOT_PATH, null);
         when(tree.isRoot()).thenReturn(true);
 
-        EntryPredicate pred = new EntryPredicate(tree, null,true);
+        EntryPredicate pred = EntryPredicate.create(tree, null,true);
         assertEquals(PathUtils.ROOT_PATH, pred.getPath());
 
         // pattern doesn't match path
@@ -346,7 +335,7 @@ public class EntryPredicateTest {
         Tree tree = mockTree(PathUtils.ROOT_PATH, null);
         when(tree.isRoot()).thenReturn(true);
 
-        EntryPredicate pred = new EntryPredicate(tree, null,false);
+        EntryPredicate pred = EntryPredicate.create(tree, null,false);
         assertEquals(PathUtils.ROOT_PATH, pred.getPath());
 
         // pattern doesn't match path

Modified: 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/authorization/permission/PermissionEntryProviderImplTest.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/authorization/permission/PermissionEntryProviderImplTest.java?rev=1858411&r1=1858410&r2=1858411&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/authorization/permission/PermissionEntryProviderImplTest.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/authorization/permission/PermissionEntryProviderImplTest.java
 Tue Apr 30 09:15:29 2019
@@ -58,7 +58,7 @@ public class PermissionEntryProviderImpl
         // test that PermissionEntryProviderImpl.noExistingNames nevertheless 
is
         // properly set
         assertFalse(getNoExistingNames(provider));
-        assertNotSame(Collections.emptyIterator(), 
provider.getEntryIterator(new EntryPredicate()));
+        assertNotSame(Collections.emptyIterator(), 
provider.getEntryIterator(EntryPredicate.create()));
     }
 
     /**
@@ -79,7 +79,7 @@ public class PermissionEntryProviderImpl
         PermissionEntryProviderImpl provider = new 
PermissionEntryProviderImpl(store, principalNames, 
ConfigurationParameters.EMPTY);
         assertFalse(getNoExistingNames(provider));
 
-        assertNotSame(Collections.emptyIterator(), 
provider.getEntryIterator(new EntryPredicate()));
+        assertNotSame(Collections.emptyIterator(), 
provider.getEntryIterator(EntryPredicate.create()));
     }
 
     /**


Reply via email to