KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > common > PCList


1
2 /*
3  * Copyright (c) 1998 - 2005 Versant Corporation
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Versant Corporation - initial API and implementation
11  */

12 package com.versant.core.common;
13
14 import com.versant.core.jdo.JDOListIterator;
15
16 import java.util.*;
17
18 /**
19  * This is an non-mutable list implementation used for Query results.
20  */

21 public class PCList implements List {
22     /** The underlying array used for storing the data. */
23     private final Object JavaDoc[] m_baseArray;
24
25     private final int startIndex;
26     private final int endIndex;
27
28     private final int calculatedSize;
29
30     public PCList(Object JavaDoc[] m_baseArray) {
31         this.m_baseArray = m_baseArray;
32         startIndex = 0;
33         endIndex = m_baseArray.length;
34         calculatedSize = m_baseArray.length;
35     }
36
37     public PCList(Object JavaDoc[] m_baseArray, int startIndex, int endIndex) {
38         if (startIndex > endIndex) throw BindingSupportImpl.getInstance().indexOutOfBounds("The startIndex is greater than end index");
39         if (m_baseArray.length < endIndex) throw BindingSupportImpl.getInstance().indexOutOfBounds("The endIndex to big");
40         if (startIndex < 0) throw BindingSupportImpl.getInstance().indexOutOfBounds("The startIndex is negative");
41
42         this.m_baseArray = m_baseArray;
43         this.startIndex = startIndex;
44         this.endIndex = endIndex;
45
46         calculatedSize = endIndex - startIndex;
47     }
48
49     public final boolean add(Object JavaDoc value) {
50         throw BindingSupportImpl.getInstance().unsupportedOperation("");
51     }
52
53     public void add(int index, Object JavaDoc value) {
54         throw BindingSupportImpl.getInstance().unsupportedOperation("");
55     }
56
57     public final Object JavaDoc get(int index) {
58         if (index < calculatedSize) {
59             return m_baseArray[index + startIndex];
60         } else {
61             throw BindingSupportImpl.getInstance().indexOutOfBounds("Invalid index value");
62         }
63     }
64
65     public final Object JavaDoc set(int index, Object JavaDoc value) {
66         throw BindingSupportImpl.getInstance().unsupportedOperation("");
67     }
68
69     public final Iterator iterator() {
70         return new InternalListIter(0);
71     }
72
73     public Object JavaDoc[] toArray() {
74         final Object JavaDoc[] values = m_baseArray;
75         int size = size();
76         Object JavaDoc[] a = new Object JavaDoc[size];
77
78         for (int i = startIndex; i < size; i++) {
79             a[i] = values[i];
80         }
81
82         return a;
83     }
84
85     public Object JavaDoc[] toArray(Object JavaDoc a[]) {
86         final Object JavaDoc[] values = m_baseArray;
87         int size = size();
88
89         if (size > a.length) {
90             a = (Object JavaDoc[])java.lang.reflect.Array.newInstance(
91                     a.getClass().getComponentType(), size);
92         }
93
94         for (int i = startIndex; i < size; i++) {
95             a[i] = values[i];
96         }
97
98         if (a.length > size) a[size] = null;
99         return a;
100     }
101
102     public boolean contains(Object JavaDoc o) {
103         final Object JavaDoc[] values = m_baseArray;
104         final int n = size();
105         for (int i = startIndex; i < n; i++) {
106             if (values[i].equals(o)) return true;
107         }
108         return false;
109     }
110
111     public int indexOf(Object JavaDoc o) {
112         final Object JavaDoc[] values = m_baseArray;
113         final int n = size();
114         for (int i = startIndex; i < n; i++) {
115             if (values[i].equals(o)) return i;
116         }
117         return -1;
118     }
119
120     public ListIterator listIterator(int index) {
121         return new InternalListIter(index + startIndex);
122     }
123
124     public boolean retainAll(Collection c) {
125         throw BindingSupportImpl.getInstance().unsupportedOperation("");
126     }
127
128     public boolean containsAll(Collection c) {
129         for (Iterator iterator = c.iterator(); iterator.hasNext();) {
130             if (!contains(iterator.next())) return false;
131         }
132         return true;
133     }
134
135     public boolean addAll(int index, Collection c) {
136         throw BindingSupportImpl.getInstance().unsupportedOperation("");
137     }
138
139     public boolean addAll(Collection c) {
140         throw BindingSupportImpl.getInstance().unsupportedOperation("");
141     }
142
143     public int lastIndexOf(Object JavaDoc o) {
144         //we don't contain null's
145
if (o == null) return -1;
146
147         final Object JavaDoc[] values = m_baseArray;
148         for (int i = (endIndex - 1); i >= startIndex; i--) {
149             if (values[i].equals(o)) return i;
150         }
151         return -1;
152     }
153
154     public boolean isEmpty() {
155         return (size() == 0);
156     }
157
158     public boolean removeAll(Collection c) {
159         throw BindingSupportImpl.getInstance().unsupportedOperation("");
160     }
161
162     public List subList(int fromIndex, int toIndex) {
163         return new PCList(m_baseArray, startIndex + fromIndex, startIndex + toIndex);
164     }
165
166     public int hashCode() {
167         int hashCode = 1;
168
169         final Object JavaDoc[] values = m_baseArray;
170         final int n = size();
171         for (int i = startIndex; i < n; i++) {
172             hashCode = 31 * hashCode + values[i].hashCode();
173         }
174         return hashCode;
175     }
176
177     public ListIterator listIterator() {
178         return new InternalListIter(startIndex);
179     }
180
181     public void clear() {
182         throw BindingSupportImpl.getInstance().unsupportedOperation("");
183     }
184
185     public int size() {
186         return calculatedSize;
187     }
188
189     public Object JavaDoc remove(int index) {
190         throw BindingSupportImpl.getInstance().unsupportedOperation("");
191     }
192
193     public boolean remove(Object JavaDoc o) {
194         throw BindingSupportImpl.getInstance().unsupportedOperation("");
195     }
196
197     public boolean equals(Object JavaDoc o) {
198         if (o == this)
199             return true;
200         if (!(o instanceof List))
201             return false;
202
203         ListIterator e1 = listIterator();
204         ListIterator e2 = ((List) o).listIterator();
205         while(e1.hasNext() && e2.hasNext()) {
206             Object JavaDoc o1 = e1.next();
207             Object JavaDoc o2 = e2.next();
208             if (!(o1==null ? o2==null : o1.equals(o2)))
209                 return false;
210         }
211         return !(e1.hasNext() || e2.hasNext());
212     }
213
214     private class InternalListIter implements ListIterator, JDOListIterator {
215         private int nextIndex;
216         private boolean closed;
217
218         public InternalListIter(int nextIndex) {
219             this.nextIndex = nextIndex;
220         }
221
222         public boolean hasNext() {
223             if (closed) return false;
224             return nextIndex < calculatedSize;
225         }
226
227         public void add(Object JavaDoc o) {
228             throw BindingSupportImpl.getInstance().unsupportedOperation("");
229         }
230
231         public Object JavaDoc next() {
232             if (closed) {
233                 throw BindingSupportImpl.getInstance().noSuchElement("");
234             }
235             if (nextIndex >= calculatedSize) {
236                 throw BindingSupportImpl.getInstance().noSuchElement("Iterated past end of Collection with size '"
237                         + calculatedSize + "'");
238             }
239             try {
240                 return m_baseArray[nextIndex++];
241             } catch (ArrayIndexOutOfBoundsException JavaDoc e) {
242                 throw BindingSupportImpl.getInstance().noSuchElement("Iterated past end of Collection with size '"
243                         + calculatedSize + "'");
244             }
245         }
246
247         public void remove() {
248             throw BindingSupportImpl.getInstance().unsupportedOperation("");
249         }
250
251         public int previousIndex() {
252             if (closed) {
253                 throw BindingSupportImpl.getInstance().noSuchElement("");
254             }
255             return nextIndex - 1;
256         }
257
258         public void set(Object JavaDoc o) {
259             throw BindingSupportImpl.getInstance().unsupportedOperation("");
260         }
261
262         public Object JavaDoc previous() {
263             if (closed) {
264                 throw BindingSupportImpl.getInstance().noSuchElement("");
265             }
266             return m_baseArray[nextIndex-- - 1];
267         }
268
269         public boolean hasPrevious() {
270             if (closed) return false;
271             return nextIndex > 0;
272         }
273
274         public int nextIndex() {
275             if (closed) {
276                 throw BindingSupportImpl.getInstance().noSuchElement("");
277             }
278             return nextIndex;
279         }
280
281         public void close() {
282             closed = true;
283         }
284     }
285
286     public String JavaDoc toString() {
287         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
288         buf.append("[");
289
290         Iterator i = iterator();
291         boolean hasNext = i.hasNext();
292         while (hasNext) {
293             Object JavaDoc o = i.next();
294             buf.append(o == this ? "(this Collection)" : String.valueOf(o));
295             hasNext = i.hasNext();
296             if (hasNext)
297                 buf.append(", ");
298         }
299
300         buf.append("]");
301         return buf.toString();
302     }
303 }
304
305
Popular Tags