You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by sk...@apache.org on 2005/02/15 04:51:26 UTC

svn commit: r153890 - jakarta/commons/proper/digester/branches/digester2/src/java/org/apache/commons/digester2/Context.java

Author: skitching
Date: Mon Feb 14 19:51:25 2005
New Revision: 153890

URL: http://svn.apache.org/viewcvs?view=rev&rev=153890
Log:
Tweaked the peek methods to allow negative offsets, so that -1 can be used to
refer to the root element of the stack etc.

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

Modified: jakarta/commons/proper/digester/branches/digester2/src/java/org/apache/commons/digester2/Context.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/digester/branches/digester2/src/java/org/apache/commons/digester2/Context.java?view=diff&r1=153889&r2=153890
==============================================================================
--- jakarta/commons/proper/digester/branches/digester2/src/java/org/apache/commons/digester2/Context.java (original)
+++ jakarta/commons/proper/digester/branches/digester2/src/java/org/apache/commons/digester2/Context.java Mon Feb 14 19:51:25 2005
@@ -588,23 +588,25 @@
     }
 
     /**
-     * Return the n'th object down the stack, where 0 is the top element
-     * and [getStackSize()-1] is the bottom element.
+     * Return the specified object on the stack. Positive offsets are distances
+     * from the top object of the stack down towards the root (0 indicates the
+     * top object). Negative offsets are distances from the root object of the
+     * stack up towards the top (-1 indicates the root object).
      *
-     * @param n Index of the desired element, where 0 is the top of the stack,
-     *  1 is the next element down, and so on.
+     * @param n Index of the desired element.
      *
      * @throws EmptyStackException (a RuntimeException subclass) if the index
-     * is out-of-range. Note that all the Digester.parse methods will turn this
-     * into a (checked) DigestionException.
+     * is a positive number greater than the depth of the stack.
      *
      * @throws ArrayOutOfBoundsException (a RuntimeException subclass) if
-     * index < 0. Note that all the Digester.parse methods will turn this
-     * into a (checked) DigestionException.
+     * index is a negative number and stack.size() + offset is less than zero.
      */
-    public Object peek(int n)
+    public Object peek(int offset)
     throws EmptyStackException, IndexOutOfBoundsException {
-            return stack.peek(n);
+        if (offset < 0) {
+            offset = stack.size() + offset;
+        }
+        return stack.peek(offset);
     }
 
     /**
@@ -666,11 +668,11 @@
     }
 
     /**
-     * <p>Gets the top object from the stack with the given id.
-     * This method does not remove the object from the stack.</p>
+     * Gets the top object from the stack with the given id.
+     * This method does not remove the object from the stack.
      *
-     * <p><strong>Note:</strong> a stack is considered empty
-     * if no objects have been pushed onto it yet.</p>
+     * <strong>Note:</strong> a stack is considered empty
+     * if no objects have been pushed onto it yet.
      *
      * @param stackId identifies the stack to be peeked
      * @return the top <code>Object</code> on the stack.
@@ -683,23 +685,23 @@
                 log.debug("Stack '" + stackId + "' is empty");
             }
             throw new EmptyStackException();
-        } else {
-            return stack.peek();
         }
+
+        return stack.peek();
     }
 
     /**
-     * Returns an element from the stack with the given name. The element
-     * returned is the n'th object down the stack, where 0 is the top element
-     * and [getStackSize()-1] is the bottom element.
-     *
-     * <p><strong>Note:</strong> a stack is considered empty
-     * if no objects have been pushed onto it yet.</p>
+     * Returns an element from the specified stack. Positive offsets are distances
+     * from the top object of the stack down towards the root (0 indicates the
+     * top object). Negative offsets are distances from the root object of the
+     * stack up towards the top (-1 indicates the root object).
+     * <p>
+     * <strong>Note:</strong> a stack is considered empty
+     * if no objects have been pushed onto it yet.
      *
      * @param stackId identifies the stack to be peeked
      *
-     * @param n Index of the desired element, where 0 is the top of the stack,
-     *  1 is the next element down, and so on.
+     * @param offset Index of the desired element
      *
      * @throws EmptyStackException (a RuntimeException subclass) if the index
      * is out-of-range. Note that all the Digester.parse methods will turn this
@@ -709,7 +711,7 @@
      * index < 0. Note that all the Digester.parse methods will turn this
      * into a (checked) DigestionException.
      */
-    public Object peek(StackId stackId, int n)
+    public Object peek(StackId stackId, int offset)
     throws EmptyStackException, IndexOutOfBoundsException {
         ArrayStack stack = (ArrayStack) scratchStacks.get(stackId);
         if (stack == null ) {
@@ -717,14 +719,17 @@
                 log.debug("Stack '" + stackId + "' is empty");
             }
             throw new EmptyStackException();
-        } else {
-            return stack.peek(n);
         }
+
+        if (offset < 0) {
+            offset = stack.size() + offset;
+        }
+        return stack.peek(offset);
     }
 
     /**
-     * <p>Pops (gets and removes) the top object from the stack with the
-     * given name.</p>
+     * Pops (gets and removes) the top object from the stack with the
+     * given name.
      *
      * @param stackId identifies the stack from which the top value is to
      * be popped
@@ -741,13 +746,13 @@
                 log.debug("Stack '" + stackId + "' is empty");
             }
             throw new EmptyStackException();
-        } else {
-            Object result = stack.pop();
-            if (stack.isEmpty()) {
-                scratchStacks.remove(stackId);
-            }
-            return result;
         }
+
+        Object result = stack.pop();
+        if (stack.isEmpty()) {
+            scratchStacks.remove(stackId);
+        }
+        return result;
     }
 
     /**
@@ -859,12 +864,12 @@
     public SAXException createSAXException(String message) {
         return createSAXException(message, null);
     }
- 
+
     /**
-     * Verify that no Actions have misbehaved, leaving the stacks in a bad 
-     * state or anything. This should only be called at the end of a 
+     * Verify that no Actions have misbehaved, leaving the stacks in a bad
+     * state or anything. This should only be called at the end of a
      * successful parse. It's really a debug method intended particularly for
-     * use with unit tests but as it is pretty quick to check things we leave 
+     * use with unit tests but as it is pretty quick to check things we leave
      * it in at runtime, to ensure that any bad custom actions also get
      * reported.
      */
@@ -881,7 +886,7 @@
             }
             // remove last unused comma
             stacklist.setLength(stacklist.length()-1);
-            
+
             throw new ParseException(
                 "An action has not executed correctly; an item has been"
                 + " left on stack(s) " + stacklist);



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org