001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002-2006, GeoTools Project Managment Committee (PMC)
005: * (C) 2002, Refractions Reserach Inc.
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation;
010: * version 2.1 of the License.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: */
017: package org.geotools.graph.util;
018:
019: import java.util.Collection;
020: import java.util.Iterator;
021:
022: public class Stack implements Collection, Queue {
023: private static final int DEFAULT_SIZE = 10;
024:
025: /** underlying array **/
026: private Object[] m_values;
027:
028: /** queue pointer **/
029: private int m_sp;
030:
031: public Stack() {
032: this (DEFAULT_SIZE);
033: }
034:
035: public Stack(int size) {
036: m_values = new Object[size];
037: m_sp = 0;
038: }
039:
040: //TODO: document that enq methods do not check bounds
041: public void push(Object element) {
042: m_values[m_sp++] = element;
043: }
044:
045: public void pushAll(Collection elements) {
046: for (Iterator itr = elements.iterator(); itr.hasNext();) {
047: m_values[m_sp++] = itr.next();
048: }
049: }
050:
051: public Object pop() {
052: return (m_values[--m_sp]);
053: }
054:
055: public int size() {
056: return (m_sp);
057: }
058:
059: public void clear() {
060: m_sp = 0;
061: }
062:
063: public boolean isEmpty() {
064: return (m_sp == 0);
065: }
066:
067: public Object[] toArray() {
068: return (m_values);
069: }
070:
071: public boolean add(Object o) {
072: push(o);
073: return (true);
074: }
075:
076: public boolean contains(Object o) {
077: for (int i = 0; i < m_sp; i++) {
078: if (m_values[i].equals(o))
079: return (true);
080: }
081: return (false);
082: }
083:
084: public boolean remove(Object o) {
085: throw new UnsupportedOperationException("remove(Object)");
086: }
087:
088: public boolean addAll(Collection c) {
089: pushAll(c);
090: return (true);
091: }
092:
093: public boolean containsAll(Collection c) {
094: for (Iterator itr = c.iterator(); itr.hasNext();) {
095: if (!contains(itr.next()))
096: return (false);
097: }
098: return (true);
099: }
100:
101: public boolean removeAll(Collection c) {
102: throw new UnsupportedOperationException("removeAll(Collection)");
103: }
104:
105: public boolean retainAll(Collection c) {
106: throw new UnsupportedOperationException("retainAll(Collection)");
107: }
108:
109: public Iterator iterator() {
110: return (new StackIterator());
111:
112: }
113:
114: public Object[] toArray(Object[] a) {
115: if (a.length < m_sp) {
116: a = (Object[]) java.lang.reflect.Array.newInstance(a
117: .getClass().getComponentType(), m_sp);
118: }
119:
120: for (int i = 0; i < m_sp; i++) {
121: a[i] = m_values[i];
122: }
123:
124: if (a.length > m_sp) {
125: a[m_sp] = null;
126: }
127:
128: return (a);
129: }
130:
131: //queue implementation
132:
133: public void enq(Object object) {
134: push(object);
135: }
136:
137: public Object deq() {
138: return (pop());
139: }
140:
141: public class StackIterator implements Iterator {
142: int m_index = 0;
143:
144: private StackIterator() {
145: }
146:
147: public void remove() {
148: throw new UnsupportedOperationException("remove()");
149: }
150:
151: public boolean hasNext() {
152: return (m_index < m_sp);
153: }
154:
155: public Object next() {
156: return (m_values[m_index++]);
157: }
158: }
159: }
|