Author: ozeigermann
Date: Sun Feb 13 23:15:11 2005
New Revision: 153729

URL: http://svn.apache.org/viewcvs?view=rev&rev=153729
Log:
- Takes account of the explicitely unmodifiable list 
returned by DefaultRuleManager and does a wild mixture 
of caching and concatenating the list returned from 
DefaultRuleManager and its own list of supplementary actions.

- Inherits from FallbackRuleManager

Modified:
    
jakarta/commons/proper/digester/branches/digester2/src/java/org/apache/commons/digester2/SupplementaryRuleManager.java

Modified: 
jakarta/commons/proper/digester/branches/digester2/src/java/org/apache/commons/digester2/SupplementaryRuleManager.java
URL: 
http://svn.apache.org/viewcvs/jakarta/commons/proper/digester/branches/digester2/src/java/org/apache/commons/digester2/SupplementaryRuleManager.java?view=diff&r1=153728&r2=153729
==============================================================================
--- 
jakarta/commons/proper/digester/branches/digester2/src/java/org/apache/commons/digester2/SupplementaryRuleManager.java
 (original)
+++ 
jakarta/commons/proper/digester/branches/digester2/src/java/org/apache/commons/digester2/SupplementaryRuleManager.java
 Sun Feb 13 23:15:11 2005
@@ -19,17 +19,19 @@
 package org.apache.commons.digester2;
 
 
+import java.util.AbstractList;
+import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
-import java.util.ArrayList;
+import java.util.Map;
 
 
 /**
  * @see DefaultRuleManager
  */
 
-public class SupplementaryRuleManager extends DefaultRuleManager {
+public class SupplementaryRuleManager extends FallbackRuleManager {
 
     public static boolean matches(String path, String pathToMatch) {
         if (pathToMatch.charAt(0) == '/') {
@@ -57,39 +59,25 @@
         return false;
     }
     
-    protected final Action supplementaryAction;
-    protected final Action fallbackAction;
+    protected final List supplementaryActions;
+    protected final Map path2ActionsMap = new HashMap();
     
-    protected final List fallbackList = new ArrayList();
+    public SupplementaryRuleManager(List supplementaryActions) {
+        this.supplementaryActions = supplementaryActions;
+    }
     
-    public SupplementaryRuleManager(Action supplementaryAction) {
-        this(supplementaryAction, null);
+    public SupplementaryRuleManager(List supplementaryActions, List 
fallbackActions) {
+        super(fallbackActions);
+        this.supplementaryActions = supplementaryActions;
     }
     
-    public SupplementaryRuleManager(Action supplementaryAction, Action 
fallbackAction) {
-        
-        if (fallbackAction == null && supplementaryAction == null) {
-            throw new IllegalArgumentException(
-                    "Both parameters set to null makes no sense. Use 
DefaultRuleManager instead.");
-        }
-
-        this.supplementaryAction = supplementaryAction;
-        this.fallbackAction = fallbackAction;
-
-        if (fallbackAction != null) {
-            fallbackList.add(fallbackAction);
-        }
-
-        if (supplementaryAction != null) {
-            fallbackList.add(supplementaryAction);
-        }
+    public SupplementaryRuleManager() {
+        this(new ArrayList());
     }
     
     public SupplementaryRuleManager(SupplementaryRuleManager manager) {
-        this(manager.supplementaryAction, manager.fallbackAction);
-        this.namespaces = (HashMap) manager.namespaces.clone();
-        this.actions = (ArrayList) manager.actions.clone();
-        this.rules = (MultiHashMap) manager.rules.clone();
+        super(manager);
+        this.supplementaryActions = manager.supplementaryActions;
     }
     
     /**
@@ -99,31 +87,73 @@
         return new SupplementaryRuleManager(this);
     }
     
+    public void addRule(String pattern, Action action) throws 
InvalidRuleException {
+        super.addRule(pattern, action);
+        invalidateCache();
+    }
+
+    public void addFallbackAction(Action action) {
+        super.addFallbackAction(action);
+        invalidateCache();
+    }
+
+    public void addSupplementaryAction(Action action) {
+        supplementaryActions.add(action);
+        invalidateCache();
+    }
+
     /**
      * @see DefaultRuleManager#getMatchingActions(String)
      */
     public List getMatchingActions(String path) {
+        
+        List completeList = (List) path2ActionsMap.get(path);
+        if (completeList != null) {
+            return completeList;
+        } 
+        
         List actionList = super.getMatchingActions(path);
-        if (actionList == Collections.EMPTY_LIST) {
-            return fallbackList;
-        }
-        if (supplementaryAction != null) {
-            actionList.add(supplementaryAction);
+        if (supplementaryActions.size() != 0) {
+            if (actionList == Collections.EMPTY_LIST) {
+                completeList = 
Collections.unmodifiableList(supplementaryActions);
+            } else {
+                completeList = new ReadOnlyConcatList(actionList, 
supplementaryActions);
+            }
+        } else {
+            completeList = actionList;
         }
-        return actionList;
+
+        path2ActionsMap.put(path, completeList);
+        return completeList;
     }
 
-    /**
-     * @return Returns the fallbackAction.
-     */
-    public Action getFallbackAction() {
-        return fallbackAction;
+    protected void invalidateCache() {
+        path2ActionsMap.clear();
     }
+    
+    protected static class ReadOnlyConcatList extends AbstractList {
+
+        final List left;
+        final List right;
+        final int border;
+
+        ReadOnlyConcatList(List left, List right) {
+            this.left = left;
+            this.right = right;
+            this.border = left.size();
+        }
+
+        public Object get(int index) {
+            if (index >= border) {
+                return right.get(index - border);
+            } else {
+                return left.get(index);
+            }
+        }
+
+        public int size() {
+            return left.size() + right.size();
+        }
 
-    /**
-     * @return Returns the supplementaryAction.
-     */
-    public Action getSupplementaryAction() {
-        return supplementaryAction;
     }
-}
+}
\ No newline at end of file



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to