KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hsqldb > lib > BaseList


1 /* Copyright (c) 2001-2005, The HSQL Development Group
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * Neither the name of the HSQL Development Group nor the names of its
15  * contributors may be used to endorse or promote products derived from this
16  * software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30
31
32 package org.hsqldb.lib;
33
34 import java.util.NoSuchElementException JavaDoc;
35
36 /**
37  * Abstract base for Lists
38  *
39  * @author fredt@users
40  * @version 1.7.2
41  * @since 1.7.0
42  */

43 abstract class BaseList {
44
45     protected int elementCount;
46
47     abstract Object JavaDoc get(int index);
48
49     abstract Object JavaDoc remove(int index);
50
51     abstract boolean add(Object JavaDoc o);
52
53     abstract int size();
54
55     public boolean contains(Object JavaDoc o) {
56         return find(o) == -1 ? false
57                              : true;
58     }
59
60     public boolean remove(Object JavaDoc o) {
61
62         int i = find(o);
63
64         if (i == -1) {
65             return false;
66         }
67
68         remove(i);
69
70         return true;
71     }
72
73     int find(Object JavaDoc o) {
74
75         for (int i = 0, size = size(); i < size; i++) {
76             Object JavaDoc current = get(i);
77
78             if (current == null) {
79                 if (o == null) {
80                     return i;
81                 }
82             } else if (current.equals(o)) {
83                 return i;
84             }
85         }
86
87         return -1;
88     }
89
90     public boolean addAll(Collection other) {
91
92         boolean result = false;
93         Iterator it = other.iterator();
94
95         while (it.hasNext()) {
96             result = true;
97
98             add(it.next());
99         }
100
101         return result;
102     }
103
104     public boolean isEmpty() {
105         return elementCount == 0;
106     }
107
108     /** Returns a string representation */
109     public String JavaDoc toString() {
110
111         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(32 + elementCount * 3);
112
113         sb.append("List : size=");
114         sb.append(elementCount);
115         sb.append(' ');
116         sb.append('{');
117
118         Iterator it = iterator();
119
120         while (it.hasNext()) {
121             sb.append(it.next());
122
123             if (it.hasNext()) {
124                 sb.append(',');
125                 sb.append(' ');
126             }
127         }
128
129         sb.append('}');
130
131         return sb.toString();
132     }
133
134     public Iterator iterator() {
135         return new BaseListIterator();
136     }
137
138     private class BaseListIterator implements Iterator {
139
140         int counter = 0;
141         boolean removed;
142
143         public boolean hasNext() {
144             return counter < elementCount;
145         }
146
147         public Object JavaDoc next() {
148
149             if (counter < elementCount) {
150                 removed = false;
151
152                 Object JavaDoc returnValue = get(counter);
153
154                 counter++;
155
156                 return returnValue;
157             }
158
159             throw new NoSuchElementException JavaDoc();
160         }
161
162         public int nextInt() {
163             throw new NoSuchElementException JavaDoc();
164         }
165
166         public long nextLong() {
167             throw new NoSuchElementException JavaDoc();
168         }
169
170         public void remove() {
171
172             if (removed) {
173                 throw new NoSuchElementException JavaDoc("Iterator");
174             }
175
176             removed = true;
177
178             if (counter != 0) {
179                 BaseList.this.remove(counter - 1);
180
181                 counter--; // above can throw, so decrement if successful
182

183                 return;
184             }
185
186             throw new NoSuchElementException JavaDoc();
187         }
188     }
189 }
190
Popular Tags