This is an automated email from the ASF dual-hosted git repository.

jamesbognar pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/juneau.git


The following commit(s) were added to refs/heads/master by this push:
     new 8487956  Context API refactoring.
8487956 is described below

commit 8487956c0e451f40792e710b98877fdabe4df483
Author: JamesBognar <[email protected]>
AuthorDate: Sun Aug 29 13:44:34 2021 -0400

    Context API refactoring.
---
 .../java/org/apache/juneau/rest/RestGuardList.java |  9 ++++++
 .../org/apache/juneau/rest/RestMatcherList.java    | 33 +++++++++++++++++++++-
 .../java/org/apache/juneau/rest/RestOpContext.java | 30 +++++++++-----------
 3 files changed, 55 insertions(+), 17 deletions(-)

diff --git 
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestGuardList.java
 
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestGuardList.java
index b62bf3d..6a65370 100644
--- 
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestGuardList.java
+++ 
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestGuardList.java
@@ -129,4 +129,13 @@ public class RestGuardList {
        public RestGuard[] asArray() {
                return entries.toArray(new RestGuard[entries.size()]);
        }
+
+       /**
+        * Returns the entries in this list.
+        *
+        * @return An unmodifiable list of entries in this list.
+        */
+       public List<RestGuard> getEntries() {
+               return entries;
+       }
 }
diff --git 
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestMatcherList.java
 
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestMatcherList.java
index 508baf1..b0f9b35 100644
--- 
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestMatcherList.java
+++ 
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestMatcherList.java
@@ -27,6 +27,8 @@ import org.apache.juneau.cp.*;
 public class RestMatcherList {
 
        private final List<RestMatcher> entries;
+       private final List<RestMatcher> optionalEntries;
+       private final List<RestMatcher> requiredEntries;
 
        /**
         * Static creator.
@@ -50,6 +52,8 @@ public class RestMatcherList {
                                .map(x -> instantiate(x, builder.beanStore))
                                .collect(toList())
                );
+               optionalEntries = unmodifiableList(entries.stream().filter(x -> 
! x.required()).collect(toList()));
+               requiredEntries = unmodifiableList(entries.stream().filter(x -> 
x.required()).collect(toList()));
        }
 
        /**
@@ -124,9 +128,36 @@ public class RestMatcherList {
        /**
         * Returns the entries in this list.
         *
-        * @return The entries in this list.
+        * @return An unmodifiable list of entries in this list.
         */
        public List<RestMatcher> getEntries() {
                return entries;
        }
+
+       /**
+        * Returns the entries in this list that are specified as optional.
+        *
+        * @return An unmodifiable list of entries in this list that are 
specified as optional.
+        */
+       public List<RestMatcher> getOptionalEntries() {
+               return optionalEntries;
+       }
+
+       /**
+        * Returns the entries in this list that are specified as required.
+        *
+        * @return An unmodifiable list of entries in this list that are 
specified as required.
+        */
+       public List<RestMatcher> getRequiredEntries() {
+               return requiredEntries;
+       }
+
+       /**
+        * Returns <jk>true</jk> if this list is empty.
+        *
+        * @return <jk>true</jk> if this list is empty.
+        */
+       public boolean isEmpty() {
+               return entries.isEmpty();
+       }
 }
diff --git 
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestOpContext.java
 
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestOpContext.java
index 393f66a..864cc33 100644
--- 
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestOpContext.java
+++ 
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestOpContext.java
@@ -78,9 +78,8 @@ public class RestOpContext extends BeanContext implements 
Comparable<RestOpConte
        private final String httpMethod;
        private final UrlPathMatcher[] pathMatchers;
        private final RestOpArg[] opArgs;
-       private final RestGuard[] guards;
-       private final RestMatcher[] optionalMatchers;
-       private final RestMatcher[] requiredMatchers;
+       private final RestGuardList guards;
+       private final RestMatcherList matchers;
        private final RestConverter[] converters;
        private final RestContext context;
        private final Method method;
@@ -165,12 +164,10 @@ public class RestOpContext extends BeanContext implements 
Comparable<RestOpConte
                        converters = createConverters(r, cp, bs).asArray();
                        bs.addBean(RestConverter[].class, converters);
 
-                       guards = createGuards(r, builder, bs).asArray();
-                       bs.addBean(RestGuard[].class, guards);
+                       guards = createGuards(r, builder, bs);
+                       bs.addBean(RestGuardList.class, guards);
 
-                       RestMatcherList matchers = createMatchers(r, builder, 
bs);
-                       requiredMatchers = 
matchers.getEntries().stream().filter(x -> 
x.required()).toArray(RestMatcher[]::new);
-                       optionalMatchers = 
matchers.getEntries().stream().filter(x -> ! 
x.required()).toArray(RestMatcher[]::new);
+                       matchers = createMatchers(r, builder, bs);
 
                        pathMatchers = createPathMatchers(r, cp, builder, 
bs).asArray();
                        bs.addBean(UrlPathMatcher[].class, pathMatchers);
@@ -1217,7 +1214,7 @@ public class RestOpContext extends BeanContext implements 
Comparable<RestOpConte
                if (pm == null)
                        return 0;
 
-               if (requiredMatchers.length == 0 && optionalMatchers.length == 
0) {
+               if (matchers.isEmpty()) {
                        call.urlPathMatch(pm);  // Cache so we don't have to 
recalculate.
                        return 2;
                }
@@ -1226,12 +1223,12 @@ public class RestOpContext extends BeanContext 
implements Comparable<RestOpConte
                        HttpServletRequest req = call.getRequest();
 
                        // If the method implements matchers, test them.
-                       for (RestMatcher m : requiredMatchers)
+                       for (RestMatcher m :  matchers.getRequiredEntries())
                                if (! m.matches(req))
                                        return 1;
-                       if (optionalMatchers.length > 0) {
+                       if (! matchers.getOptionalEntries().isEmpty()) {
                                boolean matches = false;
-                               for (RestMatcher m : optionalMatchers)
+                               for (RestMatcher m : 
matchers.getOptionalEntries())
                                        matches |= m.matches(req);
                                if (! matches)
                                        return 1;
@@ -1275,7 +1272,7 @@ public class RestOpContext extends BeanContext implements 
Comparable<RestOpConte
 
                try {
 
-                       for (RestGuard guard : guards)
+                       for (RestGuard guard : guards.getEntries())
                                if (! guard.guard(req, res))
                                        return;
 
@@ -1348,15 +1345,16 @@ public class RestOpContext extends BeanContext 
implements Comparable<RestOpConte
                if (c != 0)
                        return c;
 
-               c = compare(o.requiredMatchers.length, requiredMatchers.length);
+               c = compare(o.matchers.getRequiredEntries().size(), 
matchers.getRequiredEntries().size());
                if (c != 0)
                        return c;
 
-               c = compare(o.optionalMatchers.length, optionalMatchers.length);
+               c = compare(o.matchers.getOptionalEntries().size(), 
matchers.getOptionalEntries().size());
                if (c != 0)
                        return c;
 
-               c = compare(o.guards.length, guards.length);
+               c = compare(o.guards.getEntries().size(), 
guards.getEntries().size());
+
                if (c != 0)
                        return c;
 

Reply via email to