001: /*
002: * ProjectionList.java
003: *
004: * Copyright (c) 2006,2007 Sun Microsystems, Inc. All Rights Reserved.
005: *
006: * See the file "LICENSE.txt" for information on usage and redistribution
007: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
008: */
009: package org.pnuts.lib;
010:
011: import java.util.*;
012: import org.pnuts.util.ComparableArrayList;
013:
014: class ProjectionList extends ComparableArrayList {
015:
016: private List x;
017:
018: protected ProjectionList(List x) {
019: this .x = x;
020: }
021:
022: protected Object project(Object obj) {
023: return obj;
024: }
025:
026: public int size() {
027: return x.size();
028: }
029:
030: public Object get(int idx) {
031: return project(x.get(idx));
032: }
033:
034: public Object set(int index, Object element) {
035: throw new UnsupportedOperationException();
036: }
037:
038: public int indexOf(Object o) {
039: int size = size();
040: if (o == null) {
041: for (int i = 0; i < size; i++) {
042: if (get(i) == null) {
043: return i;
044: }
045: }
046: } else {
047: for (int i = 0; i < size; i++) {
048: if (o.equals(get(i))) {
049: return i;
050: }
051: }
052: }
053: return -1;
054: }
055:
056: public int lastIndexOf(Object o) {
057: int size = size();
058: if (o == null) {
059: for (int i = size - 1; i >= 0; i--) {
060: if (get(i) == null) {
061: return i;
062: }
063: }
064: } else {
065: for (int i = size - 1; i >= 0; i--) {
066: if (o.equals(get(i))) {
067: return i;
068: }
069: }
070: }
071: return -1;
072: }
073:
074: public Iterator iterator() {
075: return new ProjectionIterator(x.iterator()) {
076: public Object project(Object obj) {
077: return ProjectionList.this .project(obj);
078: }
079: };
080: }
081:
082: public Object[] toArray() {
083: int i = 0;
084: Object[] array = new Object[size()];
085: for (Iterator it = iterator(); it.hasNext();) {
086: array[i++] = it.next();
087: }
088: return array;
089: }
090:
091: public Object[] toArray(Object[] array) {
092: if (array.length < size()) {
093: array = new Object[size()];
094: }
095: int i = 0;
096: for (Iterator it = iterator(); it.hasNext();) {
097: array[i++] = it.next();
098: }
099: return array;
100: }
101:
102: public Object clone() {
103: List s = new ArrayList();
104: for (Iterator it = iterator(); it.hasNext();) {
105: s.add(it.next());
106: }
107: return s;
108: }
109: }
|