simplified compareTo() with code contributed by Chris Nokleberg
authormaxcooper <maxcooper>
Mon, 6 Jan 2003 05:01:57 +0000 (05:01 +0000)
committermaxcooper <maxcooper>
Mon, 6 Jan 2003 05:01:57 +0000 (05:01 +0000)
src/share/org/securityfilter/filter/URLPattern.java

index 33d3dd7..48d2304 100644 (file)
@@ -1,7 +1,7 @@
 /*
- * $Header: /cvsroot/securityfilter/securityfilter/src/share/org/securityfilter/filter/URLPattern.java,v 1.4 2003/01/06 00:17:25 maxcooper Exp $
- * $Revision: 1.4 $
- * $Date: 2003/01/06 00:17:25 $
+ * $Header: /cvsroot/securityfilter/securityfilter/src/share/org/securityfilter/filter/URLPattern.java,v 1.5 2003/01/06 05:01:57 maxcooper Exp $
+ * $Revision: 1.5 $
+ * $Date: 2003/01/06 05:01:57 $
  *
  * ====================================================================
  * The SecurityFilter Software License, Version 1.1
@@ -67,7 +67,7 @@ import org.securityfilter.config.WebResourceCollection;
  * Also supports sorting according to the Servlet Spec v2.3.
  *
  * @author Max Cooper (max@maxcooper.com)
- * @version $Revision: 1.4 $ $Date: 2003/01/06 00:17:25 $
+ * @version $Revision: 1.5 $ $Date: 2003/01/06 05:01:57 $
  */
 public class URLPattern implements Comparable {
    /**
@@ -295,50 +295,51 @@ public class URLPattern implements Comparable {
    }
 
    /**
-    * Compares this URLPattern to another to support sorting.<p>
+    * Compares this URLPattern to obj to support sorting.<p>
     *
-    * The sort order is dictated by the servlet spec. EXACT_TYPE patterns are first,
-    * followed by PATH_TYPE patterns, followed by EXTENTION_TYPE patterns. Ordering
-    * among PATH_TYPE patterns is determined by path length, with the longer path
-    * coming first. If the path lengths are the same, or both are EXACT_TYPE or
-    * EXTENSION_TYPE patterns, ordering is determined by the order in which the
-    * pattern appeared in the config file.
+    * The sort order is dictated by the servlet spec. The ordering by type is:
+    *    EXACT_TYPE
+    *    PATH_TYPE
+    *    EXTENTION_TYPE
+    *    DEFAULT_TYPE
+    * Ordering among PATH_TYPE patterns is determined by path length, with the
+    * longer path coming first. If the path lengths are the same, or both patterns
+    * are of the same type other than PATH_TYPE, ordering is determined by the order
+    * in which the pattern appeared in the config file.
     *
-    * @param another another URLPattern to compare to
+    * Thanks to Chris Nokleberg for contributing code for this method.
+    *
+    * @param obj another URLPattern to compare to
     *
     * @return a negative integer, zero, or a positive integer as this object is
     * less than, equal to, or greater than the specified object.
     *
-    * @exception ClassCastException thrown if o is not a URLPattern instance
+    * @exception ClassCastException thrown if obj is not a URLPattern instance
     */
-   public int compareTo(Object another) throws ClassCastException {
-      URLPattern otherPattern = (URLPattern) another;
-      // if the patterns are equivalent, ordering priority is equal
-      if (this.equals(otherPattern)) {
+   public int compareTo(Object obj) throws ClassCastException {
+      URLPattern other = (URLPattern) obj;
+      // return 0 if the other pattern is equivalent to this one
+      if (this.equals(other)) {
          return 0;
-      } else {
-         int otherPatternType = otherPattern.getPatternType();
-         // if the compiledPattern types are the same
-         if (patternType == otherPatternType) {
-            // if the type is PATH_TYPE
-            if (patternType == URLPattern.PATH_TYPE) {
-               int otherPathLength = otherPattern.getPathLength();
-               // if path lengths are different, the compiledPattern with longer path length should be first
-               if (pathLength != otherPathLength) {
-                  return (otherPathLength - pathLength);
-               // path length are the same, the compiledPattern with the smaller order should be first
-               } else {
-                  return (order - otherPattern.getOrder());
+      }
+      int c = patternType - other.patternType;
+      if (c == 0) {
+         switch (patternType) {
+            case PATH_TYPE:
+               c = other.pathLength - pathLength;
+               if (c != 0) {
+                  break;
                }
-            // for EXACT_TYPE or EXTENSION_TYPE, the compiledPattern with the smaller order should be first
-            } else {
-               return (order - otherPattern.getOrder());
-            }
-         } else {
-            // compiledPattern types are not the same, order should be EXACT_TYPE, PATH_TYPE, EXTENSION_TYPE
-            return (patternType - otherPatternType);
+               /* fall through */
+            case EXACT_TYPE:
+               /* fall through */
+            case EXTENSION_TYPE:
+               /* fall through */
+            case DEFAULT_TYPE:
+               c = order - other.order;
          }
       }
+      return c;
    }
 }