| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| ArrayStack |
|
| 2.6;2.6 |
| 1 | /* | |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one or more | |
| 3 | * contributor license agreements. See the NOTICE file distributed with | |
| 4 | * this work for additional information regarding copyright ownership. | |
| 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 | |
| 6 | * (the "License"); you may not use this file except in compliance with | |
| 7 | * the License. You may obtain a copy of the License at | |
| 8 | * | |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 | |
| 10 | * | |
| 11 | * Unless required by applicable law or agreed to in writing, software | |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, | |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 14 | * See the License for the specific language governing permissions and | |
| 15 | * limitations under the License. | |
| 16 | */ | |
| 17 | package org.apache.commons.collections; | |
| 18 | ||
| 19 | import java.util.ArrayList; | |
| 20 | import java.util.EmptyStackException; | |
| 21 | ||
| 22 | /** | |
| 23 | * An implementation of the {@link java.util.Stack} API that is based on an | |
| 24 | * <code>ArrayList</code> instead of a <code>Vector</code>, so it is not | |
| 25 | * synchronized to protect against multi-threaded access. The implementation | |
| 26 | * is therefore operates faster in environments where you do not need to | |
| 27 | * worry about multiple thread contention. | |
| 28 | * <p> | |
| 29 | * The removal order of an <code>ArrayStack</code> is based on insertion | |
| 30 | * order: The most recently added element is removed first. The iteration | |
| 31 | * order is <i>not</i> the same as the removal order. The iterator returns | |
| 32 | * elements from the bottom up, whereas the {@link #remove()} method removes | |
| 33 | * them from the top down. | |
| 34 | * <p> | |
| 35 | * Unlike <code>Stack</code>, <code>ArrayStack</code> accepts null entries. | |
| 36 | * <p> | |
| 37 | * <strong>Note:</strong> this class should be bytecode-identical to the | |
| 38 | * version in commons collections. This is required to allow backwards | |
| 39 | * compability with both previous versions of BeanUtils and also allow | |
| 40 | * coexistance with both collections 2.1 and 3.0. | |
| 41 | * | |
| 42 | * @see java.util.Stack | |
| 43 | * @since Commons Collections 1.0 | |
| 44 | * @version $Revision: 555824 $ $Date: 2007-07-12 20:27:15 -0400 (Thu, 12 Jul 2007) $ | |
| 45 | * | |
| 46 | * @author Craig R. McClanahan | |
| 47 | * @author Paul Jack | |
| 48 | * @author Stephen Colebourne | |
| 49 | */ | |
| 50 | public class ArrayStack extends ArrayList implements Buffer { | |
| 51 | ||
| 52 | /** Ensure serialization compatibility */ | |
| 53 | private static final long serialVersionUID = 2130079159931574599L; | |
| 54 | ||
| 55 | /** | |
| 56 | * Constructs a new empty <code>ArrayStack</code>. The initial size | |
| 57 | * is controlled by <code>ArrayList</code> and is currently 10. | |
| 58 | */ | |
| 59 | public ArrayStack() { | |
| 60 | 0 | super(); |
| 61 | 0 | } |
| 62 | ||
| 63 | /** | |
| 64 | * Constructs a new empty <code>ArrayStack</code> with an initial size. | |
| 65 | * | |
| 66 | * @param initialSize the initial size to use | |
| 67 | * @throws IllegalArgumentException if the specified initial size | |
| 68 | * is negative | |
| 69 | */ | |
| 70 | public ArrayStack(int initialSize) { | |
| 71 | 0 | super(initialSize); |
| 72 | 0 | } |
| 73 | ||
| 74 | /** | |
| 75 | * Return <code>true</code> if this stack is currently empty. | |
| 76 | * <p> | |
| 77 | * This method exists for compatibility with <code>java.util.Stack</code>. | |
| 78 | * New users of this class should use <code>isEmpty</code> instead. | |
| 79 | * | |
| 80 | * @return true if the stack is currently empty | |
| 81 | */ | |
| 82 | public boolean empty() { | |
| 83 | 0 | return isEmpty(); |
| 84 | } | |
| 85 | ||
| 86 | /** | |
| 87 | * Returns the top item off of this stack without removing it. | |
| 88 | * | |
| 89 | * @return the top item on the stack | |
| 90 | * @throws EmptyStackException if the stack is empty | |
| 91 | */ | |
| 92 | public Object peek() throws EmptyStackException { | |
| 93 | 0 | int n = size(); |
| 94 | 0 | if (n <= 0) { |
| 95 | 0 | throw new EmptyStackException(); |
| 96 | } else { | |
| 97 | 0 | return get(n - 1); |
| 98 | } | |
| 99 | } | |
| 100 | ||
| 101 | /** | |
| 102 | * Returns the n'th item down (zero-relative) from the top of this | |
| 103 | * stack without removing it. | |
| 104 | * | |
| 105 | * @param n the number of items down to go | |
| 106 | * @return the n'th item on the stack, zero relative | |
| 107 | * @throws EmptyStackException if there are not enough items on the | |
| 108 | * stack to satisfy this request | |
| 109 | */ | |
| 110 | public Object peek(int n) throws EmptyStackException { | |
| 111 | 0 | int m = (size() - n) - 1; |
| 112 | 0 | if (m < 0) { |
| 113 | 0 | throw new EmptyStackException(); |
| 114 | } else { | |
| 115 | 0 | return get(m); |
| 116 | } | |
| 117 | } | |
| 118 | ||
| 119 | /** | |
| 120 | * Pops the top item off of this stack and return it. | |
| 121 | * | |
| 122 | * @return the top item on the stack | |
| 123 | * @throws EmptyStackException if the stack is empty | |
| 124 | */ | |
| 125 | public Object pop() throws EmptyStackException { | |
| 126 | 0 | int n = size(); |
| 127 | 0 | if (n <= 0) { |
| 128 | 0 | throw new EmptyStackException(); |
| 129 | } else { | |
| 130 | 0 | return remove(n - 1); |
| 131 | } | |
| 132 | } | |
| 133 | ||
| 134 | /** | |
| 135 | * Pushes a new item onto the top of this stack. The pushed item is also | |
| 136 | * returned. This is equivalent to calling <code>add</code>. | |
| 137 | * | |
| 138 | * @param item the item to be added | |
| 139 | * @return the item just pushed | |
| 140 | */ | |
| 141 | public Object push(Object item) { | |
| 142 | 0 | add(item); |
| 143 | 0 | return item; |
| 144 | } | |
| 145 | ||
| 146 | /** | |
| 147 | * Returns the one-based position of the distance from the top that the | |
| 148 | * specified object exists on this stack, where the top-most element is | |
| 149 | * considered to be at distance <code>1</code>. If the object is not | |
| 150 | * present on the stack, return <code>-1</code> instead. The | |
| 151 | * <code>equals()</code> method is used to compare to the items | |
| 152 | * in this stack. | |
| 153 | * | |
| 154 | * @param object the object to be searched for | |
| 155 | * @return the 1-based depth into the stack of the object, or -1 if not found | |
| 156 | */ | |
| 157 | public int search(Object object) { | |
| 158 | 0 | int i = size() - 1; // Current index |
| 159 | 0 | int n = 1; // Current distance |
| 160 | 0 | while (i >= 0) { |
| 161 | 0 | Object current = get(i); |
| 162 | 0 | if ((object == null && current == null) || |
| 163 | (object != null && object.equals(current))) { | |
| 164 | 0 | return n; |
| 165 | } | |
| 166 | 0 | i--; |
| 167 | 0 | n++; |
| 168 | 0 | } |
| 169 | 0 | return -1; |
| 170 | } | |
| 171 | ||
| 172 | /** | |
| 173 | * Returns the element on the top of the stack. | |
| 174 | * | |
| 175 | * @return the element on the top of the stack | |
| 176 | * @throws BufferUnderflowException if the stack is empty | |
| 177 | */ | |
| 178 | public Object get() { | |
| 179 | 0 | int size = size(); |
| 180 | 0 | if (size == 0) { |
| 181 | 0 | throw new BufferUnderflowException(); |
| 182 | } | |
| 183 | 0 | return get(size - 1); |
| 184 | } | |
| 185 | ||
| 186 | /** | |
| 187 | * Removes the element on the top of the stack. | |
| 188 | * | |
| 189 | * @return the removed element | |
| 190 | * @throws BufferUnderflowException if the stack is empty | |
| 191 | */ | |
| 192 | public Object remove() { | |
| 193 | 0 | int size = size(); |
| 194 | 0 | if (size == 0) { |
| 195 | 0 | throw new BufferUnderflowException(); |
| 196 | } | |
| 197 | 0 | return remove(size - 1); |
| 198 | } | |
| 199 | ||
| 200 | } |