Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
FastStack |
|
| 1.7;1.7 |
1 | //////////////////////////////////////////////////////////////////////////////// | |
2 | // checkstyle: Checks Java source code for adherence to a set of rules. | |
3 | // Copyright (C) 2001-2014 Oliver Burn | |
4 | // | |
5 | // This library is free software; you can redistribute it and/or | |
6 | // modify it under the terms of the GNU Lesser General Public | |
7 | // License as published by the Free Software Foundation; either | |
8 | // version 2.1 of the License, or (at your option) any later version. | |
9 | // | |
10 | // This library is distributed in the hope that it will be useful, | |
11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | // Lesser General Public License for more details. | |
14 | // | |
15 | // You should have received a copy of the GNU Lesser General Public | |
16 | // License along with this library; if not, write to the Free Software | |
17 | // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 | //////////////////////////////////////////////////////////////////////////////// | |
19 | package com.puppycrawl.tools.checkstyle.api; | |
20 | ||
21 | import com.google.common.collect.Lists; | |
22 | import java.util.Iterator; | |
23 | import java.util.List; | |
24 | ||
25 | /** | |
26 | * Simple implementation of a LIFO Stack that can be used instead of | |
27 | * {@link java.util.Vector} which is <tt>synchronized</tt>. | |
28 | * @author oliverb | |
29 | * @param <E> The type to hold. | |
30 | */ | |
31 | 897 | public class FastStack<E> implements Iterable<E> |
32 | { | |
33 | /** Hold the entries in the stack. */ | |
34 | 897 | private final List<E> mEntries = Lists.newArrayList(); |
35 | ||
36 | /** | |
37 | * Pushes the supplied element onto the stack. | |
38 | * @param aElement the element to push onto the stack. | |
39 | */ | |
40 | public void push(E aElement) | |
41 | { | |
42 | 13576 | mEntries.add(aElement); |
43 | 13576 | } |
44 | ||
45 | /** | |
46 | * Returns whether the stack is empty. | |
47 | * @return whether the stack is empty. | |
48 | */ | |
49 | public boolean isEmpty() | |
50 | { | |
51 | 104 | return mEntries.isEmpty(); |
52 | } | |
53 | ||
54 | /** | |
55 | * Returns the number of entries in the stack. | |
56 | * @return the number of entries in the stack. | |
57 | */ | |
58 | public int size() | |
59 | { | |
60 | 297 | return mEntries.size(); |
61 | } | |
62 | ||
63 | /** | |
64 | * Returns the entry at the top of the stack without removing it. | |
65 | * @return the top entry | |
66 | * @throws IllegalStateException if the stack is empty. | |
67 | */ | |
68 | public E peek() | |
69 | { | |
70 | 2097 | if (mEntries.isEmpty()) { |
71 | 1 | throw new IllegalStateException("FastStack is empty"); |
72 | } | |
73 | 2096 | return mEntries.get(mEntries.size() - 1); |
74 | } | |
75 | ||
76 | /** | |
77 | * Returns the entry at the top of the stack by removing it. | |
78 | * @return the top entry | |
79 | * @throws IllegalStateException if the stack is empty. | |
80 | */ | |
81 | public E pop() | |
82 | { | |
83 | 13493 | if (mEntries.isEmpty()) { |
84 | 1 | throw new IllegalStateException("FastStack is empty"); |
85 | } | |
86 | 13492 | return mEntries.remove(mEntries.size() - 1); |
87 | } | |
88 | ||
89 | /** | |
90 | * Return the element at the specified index. It does not remove the | |
91 | * element from the stack. | |
92 | * @param aIndex the index to return | |
93 | * @return the element at the index | |
94 | * @throws IllegalArgumentException if index out of range | |
95 | */ | |
96 | public E peek(int aIndex) | |
97 | { | |
98 | 544 | if ((aIndex < 0) || (aIndex >= mEntries.size())) { |
99 | 0 | throw new IllegalArgumentException("index out of range."); |
100 | } | |
101 | 544 | return mEntries.get(aIndex); |
102 | } | |
103 | ||
104 | /** | |
105 | * Returns if the stack contains the specified element. | |
106 | * @param aElement the element to find | |
107 | * @return whether the stack contains the entry | |
108 | */ | |
109 | public boolean contains(E aElement) | |
110 | { | |
111 | 9 | return mEntries.contains(aElement); |
112 | } | |
113 | ||
114 | /** | |
115 | * Clears the stack. | |
116 | */ | |
117 | public void clear() | |
118 | { | |
119 | 79 | mEntries.clear(); |
120 | 79 | } |
121 | ||
122 | /** | |
123 | * Returns an iterator that goes from the oldest element to the newest. | |
124 | * @return an iterator | |
125 | */ | |
126 | public Iterator<E> iterator() | |
127 | { | |
128 | 10947 | return mEntries.iterator(); |
129 | } | |
130 | ||
131 | /** | |
132 | * Factory method to create a new instance. | |
133 | * @param <T> the type of elements to hold in the stack. | |
134 | * @return a new instance of {@link FastStack} | |
135 | */ | |
136 | public static <T> FastStack<T> newInstance() | |
137 | { | |
138 | 891 | return new FastStack<T>(); |
139 | } | |
140 | } |