001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
002:
003: This file is part of the db4o open source object database.
004:
005: db4o is free software; you can redistribute it and/or modify it under
006: the terms of version 2 of the GNU General Public License as published
007: by the Free Software Foundation and as clarified by db4objects' GPL
008: interpretation policy, available at
009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011: Suite 350, San Mateo, CA 94403, USA.
012:
013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: for more details.
017:
018: You should have received a copy of the GNU General Public License along
019: with this program; if not, write to the Free Software Foundation, Inc.,
020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
021: package com.db4o.collections.facades;
022:
023: import java.util.*;
024:
025: import com.db4o.foundation.*;
026: import com.db4o.internal.collections.*;
027:
028: /**
029: * @exclude
030: */
031: public class FastList implements java.util.List {
032:
033: private PersistentList _persistentList;
034:
035: private transient FastListCache _cache;
036:
037: public FastList() {
038:
039: }
040:
041: public FastList(PersistentList persistentList) {
042: _persistentList = persistentList;
043: ensureInitFastListCache();
044: }
045:
046: private void ensureInitFastListCache() {
047: if (_cache == null) {
048: _cache = new FastListCache(size());
049: }
050: }
051:
052: public boolean add(Object o) {
053: ensureInitFastListCache();
054: _cache.add(o);
055: return _persistentList.add(o);
056: }
057:
058: public void add(int index, Object element) {
059: validateIndex(index);
060: ensureInitFastListCache();
061: _cache.add(index, element);
062: _persistentList.add(index, element);
063: }
064:
065: public boolean addAll(final Collection c) {
066: ensureInitFastListCache();
067: _cache.addAll(c);
068: return _persistentList.addAll(new JdkCollectionIterable4(c));
069: }
070:
071: public boolean addAll(int index, Collection c) {
072: validateIndex(index);
073: ensureInitFastListCache();
074: _cache.addAll(index, c);
075: return _persistentList.addAll(index,
076: new JdkCollectionIterable4(c));
077: }
078:
079: public void clear() {
080: ensureInitFastListCache();
081: _cache.clear();
082: _persistentList.clear();
083: }
084:
085: public boolean contains(Object o) {
086: ensureInitFastListCache();
087: if (_cache.contains(o)) {
088: return true;
089: }
090: return _persistentList.contains(o);
091: }
092:
093: public boolean containsAll(Collection c) {
094: ensureInitFastListCache();
095: boolean ret = _cache.containsAll(c);
096: if (ret) {
097: return true;
098: }
099: return _persistentList
100: .containsAll(new JdkCollectionIterable4(c));
101: }
102:
103: public Object get(int index) {
104: ensureInitFastListCache();
105: CachedObject co = _cache.get(index);
106: if (co != CachedObject.NONE) {
107: return co.obj;
108: }
109: return _persistentList.get(index);
110: }
111:
112: public int indexOf(Object o) {
113: int index = _cache.indexOf(o);
114: if (index != -1) {
115: return index;
116: }
117: return _persistentList.indexOf(o);
118: }
119:
120: public boolean isEmpty() {
121: return _persistentList.isEmpty();
122: }
123:
124: public Iterator iterator() {
125: return new Iterator4JdkIterator(_persistentList.iterator());
126: }
127:
128: public int lastIndexOf(Object o) {
129: return _persistentList.lastIndexOf(o);
130: }
131:
132: public ListIterator listIterator() {
133: throw new UnsupportedOperationException();
134: }
135:
136: public ListIterator listIterator(int index) {
137: throw new UnsupportedOperationException();
138: }
139:
140: public boolean remove(Object o) {
141: ensureInitFastListCache();
142: _cache.remove(o);
143: return _persistentList.remove(o);
144: }
145:
146: public Object remove(int index) {
147: ensureInitFastListCache();
148: _cache.remove(index);
149: return _persistentList.remove(index);
150: }
151:
152: public boolean removeAll(Collection c) {
153: ensureInitFastListCache();
154: _cache.removeAll(c);
155: return _persistentList.removeAll(new JdkCollectionIterable4(c));
156: }
157:
158: public boolean retainAll(Collection c) {
159: ensureInitFastListCache();
160: _cache.retainAll(c);
161: return _persistentList.retainAll(new JdkCollectionIterable4(c));
162: }
163:
164: public Object set(int index, Object element) {
165: validateIndex(index);
166: ensureInitFastListCache();
167: _cache.set(index, element);
168: return _persistentList.set(index, element);
169: }
170:
171: public int size() {
172: return _persistentList.size();
173: }
174:
175: public List subList(int fromIndex, int toIndex) {
176: throw new UnsupportedOperationException();
177: }
178:
179: public Object[] toArray() {
180: return _persistentList.toArray();
181: }
182:
183: public Object[] toArray(Object[] a) {
184: return _persistentList.toArray(a);
185: }
186:
187: private void validateIndex(int index) {
188: if (index < 0 || index > size()) {
189: throw new IndexOutOfBoundsException();
190: }
191: }
192:
193: }
|