001: /*
002: * Primitive Collections for Java.
003: * Copyright (C) 2003 Søren Bak
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019: package bak.pcj.list;
020:
021: import bak.pcj.CharCollection;
022:
023: /**
024: * This class represents an array implemenation of stacks of
025: * char values.
026: *
027: * @see java.util.ArrayList
028: *
029: * @author Søren Bak
030: * @version 1.1 2003/3/3
031: * @since 1.0
032: */
033: public class CharArrayStack extends CharArrayList implements CharStack {
034:
035: /**
036: * Creates a new array stack with capacity 10 and a relative
037: * growth factor of 1.0.
038: *
039: * @see #CharArrayStack(int,double)
040: */
041: public CharArrayStack() {
042: super ();
043: }
044:
045: /**
046: * Creates a new array stack with the same elements as a
047: * specified collection. The elements of the specified collection
048: * are pushed in the collection's iteration order.
049: *
050: * @param c
051: * the collection whose elements to add to the new
052: * stack.
053: *
054: * @throws NullPointerException
055: * if <tt>c</tt> is <tt>null</tt>.
056: */
057: public CharArrayStack(CharCollection c) {
058: super (c);
059: }
060:
061: /**
062: * Creates a new array stack with the same elements as a
063: * specified array. The elements of the specified array
064: * are pushed in the order of the array.
065: *
066: * @param a
067: * the array whose elements to add to the new
068: * stack.
069: *
070: * @throws NullPointerException
071: * if <tt>a</tt> is <tt>null</tt>.
072: *
073: * @since 1.1
074: */
075: public CharArrayStack(char[] a) {
076: super (a);
077: }
078:
079: /**
080: * Creates a new array stack with a specified capacity and a
081: * relative growth factor of 1.0.
082: *
083: * @param capacity
084: * the initial capacity of the stack.
085: *
086: * @see #CharArrayStack(int,double)
087: *
088: * @throws IllegalArgumentException
089: * if <tt>capacity</tt> is negative.
090: */
091: public CharArrayStack(int capacity) {
092: super (capacity);
093: }
094:
095: /**
096: * Creates a new array stack with a specified capacity and
097: * relative growth factor.
098: *
099: * <p>The array capacity increases to <tt>capacity()*(1+growthFactor)</tt>.
100: * This strategy is good for avoiding many capacity increases, but
101: * the amount of wasted memory is approximately the size of the stack.
102: *
103: * @param capacity
104: * the initial capacity of the stack.
105: *
106: * @param growthFactor
107: * the relative amount with which to increase the
108: * the capacity when a capacity increase is needed.
109: *
110: * @throws IllegalArgumentException
111: * if <tt>capacity</tt> is negative;
112: * if <tt>growthFactor</tt> is negative.
113: */
114: public CharArrayStack(int capacity, double growthFactor) {
115: super (capacity, growthFactor);
116: }
117:
118: /**
119: * Creates a new array stack with a specified capacity and
120: * absolute growth factor.
121: *
122: * <p>The array capacity increases to <tt>capacity()+growthChunk</tt>.
123: * This strategy is good for avoiding wasting memory. However, an
124: * overhead is potentially introduced by frequent capacity increases.
125: *
126: * @param capacity
127: * the initial capacity of the stack.
128: *
129: * @param growthChunk
130: * the absolute amount with which to increase the
131: * the capacity when a capacity increase is needed.
132: *
133: * @throws IllegalArgumentException
134: * if <tt>capacity</tt> is negative;
135: * if <tt>growthChunk</tt> is negative.
136: */
137: public CharArrayStack(int capacity, int growthChunk) {
138: super (capacity, growthChunk);
139: }
140:
141: public void push(char v) {
142: add(v);
143: }
144:
145: public char pop() {
146: return removeElementAt(size() - 1);
147: }
148:
149: public char peek() {
150: return get(size() - 1);
151: }
152:
153: }
|