001: /* Copyright (c) 2001-2005, The HSQL Development Group
002: * All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * Redistributions of source code must retain the above copyright notice, this
008: * list of conditions and the following disclaimer.
009: *
010: * Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * Neither the name of the HSQL Development Group nor the names of its
015: * contributors may be used to endorse or promote products derived from this
016: * software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
022: * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
026: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030:
031: package org.hsqldb.lib;
032:
033: import java.util.NoSuchElementException;
034:
035: /**
036: * Abstract base for Lists
037: *
038: * @author fredt@users
039: * @version 1.7.2
040: * @since 1.7.0
041: */
042: abstract class BaseList {
043:
044: protected int elementCount;
045:
046: abstract Object get(int index);
047:
048: abstract Object remove(int index);
049:
050: abstract boolean add(Object o);
051:
052: abstract int size();
053:
054: public boolean contains(Object o) {
055: return find(o) == -1 ? false : true;
056: }
057:
058: public boolean remove(Object o) {
059:
060: int i = find(o);
061:
062: if (i == -1) {
063: return false;
064: }
065:
066: remove(i);
067:
068: return true;
069: }
070:
071: int find(Object o) {
072:
073: for (int i = 0, size = size(); i < size; i++) {
074: Object current = get(i);
075:
076: if (current == null) {
077: if (o == null) {
078: return i;
079: }
080: } else if (current.equals(o)) {
081: return i;
082: }
083: }
084:
085: return -1;
086: }
087:
088: public boolean addAll(Collection other) {
089:
090: boolean result = false;
091: Iterator it = other.iterator();
092:
093: while (it.hasNext()) {
094: result = true;
095:
096: add(it.next());
097: }
098:
099: return result;
100: }
101:
102: public boolean isEmpty() {
103: return elementCount == 0;
104: }
105:
106: /** Returns a string representation */
107: public String toString() {
108:
109: StringBuffer sb = new StringBuffer(32 + elementCount * 3);
110:
111: sb.append("List : size=");
112: sb.append(elementCount);
113: sb.append(' ');
114: sb.append('{');
115:
116: Iterator it = iterator();
117:
118: while (it.hasNext()) {
119: sb.append(it.next());
120:
121: if (it.hasNext()) {
122: sb.append(',');
123: sb.append(' ');
124: }
125: }
126:
127: sb.append('}');
128:
129: return sb.toString();
130: }
131:
132: public Iterator iterator() {
133: return new BaseListIterator();
134: }
135:
136: private class BaseListIterator implements Iterator {
137:
138: int counter = 0;
139: boolean removed;
140:
141: public boolean hasNext() {
142: return counter < elementCount;
143: }
144:
145: public Object next() {
146:
147: if (counter < elementCount) {
148: removed = false;
149:
150: Object returnValue = get(counter);
151:
152: counter++;
153:
154: return returnValue;
155: }
156:
157: throw new NoSuchElementException();
158: }
159:
160: public int nextInt() {
161: throw new NoSuchElementException();
162: }
163:
164: public long nextLong() {
165: throw new NoSuchElementException();
166: }
167:
168: public void remove() {
169:
170: if (removed) {
171: throw new NoSuchElementException("Iterator");
172: }
173:
174: removed = true;
175:
176: if (counter != 0) {
177: BaseList.this .remove(counter - 1);
178:
179: counter--; // above can throw, so decrement if successful
180:
181: return;
182: }
183:
184: throw new NoSuchElementException();
185: }
186: }
187: }
|