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;
022:
023: import java.util.*;
024:
025: /**
026: * @exclude
027: */
028: public class SubArrayList4<E> extends AbstractList4<E> {
029:
030: private AbstractList4<E> _delegate;
031:
032: private int _fromIndex;
033:
034: private int _size;
035:
036: public SubArrayList4(AbstractList4<E> delegate, int fromIndex,
037: int toIndex) {
038: _delegate = delegate;
039: _fromIndex = fromIndex;
040: syncModCount();
041: setSize(toIndex - fromIndex);
042: }
043:
044: @Override
045: public void add(int index, E element) {
046: checkIndex(index, 0, size());
047: checkConcurrentModification();
048: _delegate.add(translatedIndex(index), element);
049: increaseSize(1);
050: syncModCount();
051: }
052:
053: @Override
054: public boolean addAll(int index, Collection<? extends E> collection) {
055: checkIndex(index, 0, size());
056: checkConcurrentModification();
057: boolean changed = _delegate.addAll(translatedIndex(index),
058: collection);
059: increaseSize(collection.size());
060: syncModCount();
061: return changed;
062: }
063:
064: @Override
065: public E get(int index) {
066: checkIndex(index, 0, size() - 1);
067: checkConcurrentModification();
068: return _delegate.get(translatedIndex(index));
069: }
070:
071: @Override
072: public E remove(int index) {
073: checkIndex(index, 0, size() - 1);
074: checkConcurrentModification();
075: E removed = _delegate.remove(translatedIndex(index));
076: decreaseSize(1);
077: syncModCount();
078: return removed;
079: }
080:
081: @Override
082: protected void removeRange(int fromIndex, int toIndex) {
083: if ((fromIndex < 0 || fromIndex >= size() || toIndex > size() || toIndex < fromIndex)) {
084: throw new IndexOutOfBoundsException();
085: }
086: if (fromIndex == toIndex) {
087: return;
088: }
089: _delegate.removeRange(fromIndex + _fromIndex, toIndex
090: + _fromIndex);
091: decreaseSize(toIndex - fromIndex);
092: syncModCount();
093: }
094:
095: @Override
096: public E set(int index, E element) {
097: checkIndex(index, 0, size() - 1);
098: checkConcurrentModification();
099: E replaced = _delegate.set(translatedIndex(index), element);
100: syncModCount();
101: return replaced;
102: }
103:
104: @Override
105: public int size() {
106: return _size;
107: }
108:
109: private void checkConcurrentModification() {
110: if (modCount != _delegate.modCount) {
111: throw new ConcurrentModificationException();
112: }
113: }
114:
115: private void syncModCount() {
116: modCount = _delegate.modCount;
117: }
118:
119: private int translatedIndex(int index) {
120: return index + _fromIndex;
121: }
122:
123: private void setSize(int count) {
124: _size = count;
125: }
126:
127: private void increaseSize(int count) {
128: _size += count;
129: }
130:
131: private void decreaseSize(int count) {
132: _size -= count;
133: }
134: }
|