001: /*
002: * Copyright (c) 1998 - 2005 Versant Corporation
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * Versant Corporation - initial API and implementation
010: */
011: package com.versant.core.common;
012:
013: import com.versant.core.jdo.JDOListIterator;
014:
015: import java.util.*;
016:
017: /**
018: * This is an non-mutable list implementation used for Query results.
019: */
020: public class PCList implements List {
021: /** The underlying array used for storing the data. */
022: private final Object[] m_baseArray;
023:
024: private final int startIndex;
025: private final int endIndex;
026:
027: private final int calculatedSize;
028:
029: public PCList(Object[] m_baseArray) {
030: this .m_baseArray = m_baseArray;
031: startIndex = 0;
032: endIndex = m_baseArray.length;
033: calculatedSize = m_baseArray.length;
034: }
035:
036: public PCList(Object[] m_baseArray, int startIndex, int endIndex) {
037: if (startIndex > endIndex)
038: throw BindingSupportImpl.getInstance().indexOutOfBounds(
039: "The startIndex is greater than end index");
040: if (m_baseArray.length < endIndex)
041: throw BindingSupportImpl.getInstance().indexOutOfBounds(
042: "The endIndex to big");
043: if (startIndex < 0)
044: throw BindingSupportImpl.getInstance().indexOutOfBounds(
045: "The startIndex is negative");
046:
047: this .m_baseArray = m_baseArray;
048: this .startIndex = startIndex;
049: this .endIndex = endIndex;
050:
051: calculatedSize = endIndex - startIndex;
052: }
053:
054: public final boolean add(Object value) {
055: throw BindingSupportImpl.getInstance().unsupportedOperation("");
056: }
057:
058: public void add(int index, Object value) {
059: throw BindingSupportImpl.getInstance().unsupportedOperation("");
060: }
061:
062: public final Object get(int index) {
063: if (index < calculatedSize) {
064: return m_baseArray[index + startIndex];
065: } else {
066: throw BindingSupportImpl.getInstance().indexOutOfBounds(
067: "Invalid index value");
068: }
069: }
070:
071: public final Object set(int index, Object value) {
072: throw BindingSupportImpl.getInstance().unsupportedOperation("");
073: }
074:
075: public final Iterator iterator() {
076: return new InternalListIter(0);
077: }
078:
079: public Object[] toArray() {
080: final Object[] values = m_baseArray;
081: int size = size();
082: Object[] a = new Object[size];
083:
084: for (int i = startIndex; i < size; i++) {
085: a[i] = values[i];
086: }
087:
088: return a;
089: }
090:
091: public Object[] toArray(Object a[]) {
092: final Object[] values = m_baseArray;
093: int size = size();
094:
095: if (size > a.length) {
096: a = (Object[]) java.lang.reflect.Array.newInstance(a
097: .getClass().getComponentType(), size);
098: }
099:
100: for (int i = startIndex; i < size; i++) {
101: a[i] = values[i];
102: }
103:
104: if (a.length > size)
105: a[size] = null;
106: return a;
107: }
108:
109: public boolean contains(Object o) {
110: final Object[] values = m_baseArray;
111: final int n = size();
112: for (int i = startIndex; i < n; i++) {
113: if (values[i].equals(o))
114: return true;
115: }
116: return false;
117: }
118:
119: public int indexOf(Object o) {
120: final Object[] values = m_baseArray;
121: final int n = size();
122: for (int i = startIndex; i < n; i++) {
123: if (values[i].equals(o))
124: return i;
125: }
126: return -1;
127: }
128:
129: public ListIterator listIterator(int index) {
130: return new InternalListIter(index + startIndex);
131: }
132:
133: public boolean retainAll(Collection c) {
134: throw BindingSupportImpl.getInstance().unsupportedOperation("");
135: }
136:
137: public boolean containsAll(Collection c) {
138: for (Iterator iterator = c.iterator(); iterator.hasNext();) {
139: if (!contains(iterator.next()))
140: return false;
141: }
142: return true;
143: }
144:
145: public boolean addAll(int index, Collection c) {
146: throw BindingSupportImpl.getInstance().unsupportedOperation("");
147: }
148:
149: public boolean addAll(Collection c) {
150: throw BindingSupportImpl.getInstance().unsupportedOperation("");
151: }
152:
153: public int lastIndexOf(Object o) {
154: //we don't contain null's
155: if (o == null)
156: return -1;
157:
158: final Object[] values = m_baseArray;
159: for (int i = (endIndex - 1); i >= startIndex; i--) {
160: if (values[i].equals(o))
161: return i;
162: }
163: return -1;
164: }
165:
166: public boolean isEmpty() {
167: return (size() == 0);
168: }
169:
170: public boolean removeAll(Collection c) {
171: throw BindingSupportImpl.getInstance().unsupportedOperation("");
172: }
173:
174: public List subList(int fromIndex, int toIndex) {
175: return new PCList(m_baseArray, startIndex + fromIndex,
176: startIndex + toIndex);
177: }
178:
179: public int hashCode() {
180: int hashCode = 1;
181:
182: final Object[] values = m_baseArray;
183: final int n = size();
184: for (int i = startIndex; i < n; i++) {
185: hashCode = 31 * hashCode + values[i].hashCode();
186: }
187: return hashCode;
188: }
189:
190: public ListIterator listIterator() {
191: return new InternalListIter(startIndex);
192: }
193:
194: public void clear() {
195: throw BindingSupportImpl.getInstance().unsupportedOperation("");
196: }
197:
198: public int size() {
199: return calculatedSize;
200: }
201:
202: public Object remove(int index) {
203: throw BindingSupportImpl.getInstance().unsupportedOperation("");
204: }
205:
206: public boolean remove(Object o) {
207: throw BindingSupportImpl.getInstance().unsupportedOperation("");
208: }
209:
210: public boolean equals(Object o) {
211: if (o == this )
212: return true;
213: if (!(o instanceof List))
214: return false;
215:
216: ListIterator e1 = listIterator();
217: ListIterator e2 = ((List) o).listIterator();
218: while (e1.hasNext() && e2.hasNext()) {
219: Object o1 = e1.next();
220: Object o2 = e2.next();
221: if (!(o1 == null ? o2 == null : o1.equals(o2)))
222: return false;
223: }
224: return !(e1.hasNext() || e2.hasNext());
225: }
226:
227: private class InternalListIter implements ListIterator,
228: JDOListIterator {
229: private int nextIndex;
230: private boolean closed;
231:
232: public InternalListIter(int nextIndex) {
233: this .nextIndex = nextIndex;
234: }
235:
236: public boolean hasNext() {
237: if (closed)
238: return false;
239: return nextIndex < calculatedSize;
240: }
241:
242: public void add(Object o) {
243: throw BindingSupportImpl.getInstance()
244: .unsupportedOperation("");
245: }
246:
247: public Object next() {
248: if (closed) {
249: throw BindingSupportImpl.getInstance()
250: .noSuchElement("");
251: }
252: if (nextIndex >= calculatedSize) {
253: throw BindingSupportImpl.getInstance().noSuchElement(
254: "Iterated past end of Collection with size '"
255: + calculatedSize + "'");
256: }
257: try {
258: return m_baseArray[nextIndex++];
259: } catch (ArrayIndexOutOfBoundsException e) {
260: throw BindingSupportImpl.getInstance().noSuchElement(
261: "Iterated past end of Collection with size '"
262: + calculatedSize + "'");
263: }
264: }
265:
266: public void remove() {
267: throw BindingSupportImpl.getInstance()
268: .unsupportedOperation("");
269: }
270:
271: public int previousIndex() {
272: if (closed) {
273: throw BindingSupportImpl.getInstance()
274: .noSuchElement("");
275: }
276: return nextIndex - 1;
277: }
278:
279: public void set(Object o) {
280: throw BindingSupportImpl.getInstance()
281: .unsupportedOperation("");
282: }
283:
284: public Object previous() {
285: if (closed) {
286: throw BindingSupportImpl.getInstance()
287: .noSuchElement("");
288: }
289: return m_baseArray[nextIndex-- - 1];
290: }
291:
292: public boolean hasPrevious() {
293: if (closed)
294: return false;
295: return nextIndex > 0;
296: }
297:
298: public int nextIndex() {
299: if (closed) {
300: throw BindingSupportImpl.getInstance()
301: .noSuchElement("");
302: }
303: return nextIndex;
304: }
305:
306: public void close() {
307: closed = true;
308: }
309: }
310:
311: public String toString() {
312: StringBuffer buf = new StringBuffer();
313: buf.append("[");
314:
315: Iterator i = iterator();
316: boolean hasNext = i.hasNext();
317: while (hasNext) {
318: Object o = i.next();
319: buf.append(o == this ? "(this Collection)" : String
320: .valueOf(o));
321: hasNext = i.hasNext();
322: if (hasNext)
323: buf.append(", ");
324: }
325:
326: buf.append("]");
327: return buf.toString();
328: }
329: }
|