📄 persistentlistimpl.java
字号:
public int indexOf(Object o) {
ListIterator e = listIterator();
if (o==null) {
while (e.hasNext())
if (e.next()==null)
return e.previousIndex();
} else {
while (e.hasNext())
if (o.equals(e.next()))
return e.previousIndex();
}
return -1;
}
public int lastIndexOf(Object o) {
ListIterator e = listIterator(size());
if (o==null) {
while (e.hasPrevious())
if (e.previous()==null)
return e.nextIndex();
} else {
while (e.hasPrevious())
if (o.equals(e.previous()))
return e.nextIndex();
}
return -1;
}
public boolean addAll(int index, Collection c) {
boolean modified = false;
Iterator e = c.iterator();
while (e.hasNext()) {
add(index++, e.next());
modified = true;
}
return modified;
}
public Iterator iterator() {
return new Itr();
}
public ListIterator listIterator() {
return listIterator(0);
}
public ListIterator listIterator(int index) {
if (index<0 || index>size())
throw new IndexOutOfBoundsException("Index: "+index);
return new ListItr(index);
}
private class Itr extends TreePosition implements PersistentIterator, Iterator {
/**
* Index of element to be returned by subsequent call to next.
*/
int cursor = 0;
/**
* Index of element returned by most recent call to next or
* previous. Reset to -1 if this element is deleted by a call
* to remove.
*/
int lastRet = -1;
/**
* The modCount value that the iterator believes that the backing
* List should have. If this expectation is violated, the iterator
* has detected concurrent modification.
*/
int expectedModCount = modCount;
public boolean hasNext() {
return cursor != size();
}
public int nextOid() {
checkForComodification();
try {
int oid = getRawPosition(this, cursor).getOid();
lastRet = cursor++;
return oid;
} catch(IndexOutOfBoundsException e) {
checkForComodification();
throw new NoSuchElementException();
}
}
public Object next() {
checkForComodification();
try {
Object next = getPosition(this, cursor);
lastRet = cursor++;
return next;
} catch(IndexOutOfBoundsException e) {
checkForComodification();
throw new NoSuchElementException();
}
}
public void remove() {
if (lastRet == -1) {
throw new IllegalStateException();
}
checkForComodification();
try {
PersistentListImpl.this.remove(lastRet);
if (lastRet < cursor) {
cursor--;
}
page = null;
lastRet = -1;
expectedModCount = modCount;
} catch(IndexOutOfBoundsException e) {
throw new ConcurrentModificationException();
}
}
final void checkForComodification() {
if (modCount != expectedModCount) {
throw new ConcurrentModificationException();
}
}
}
private class ListItr extends Itr implements ListIterator {
ListItr(int index) {
cursor = index;
}
public boolean hasPrevious() {
return cursor != 0;
}
public Object previous() {
checkForComodification();
try {
int i = cursor - 1;
Object previous = getPosition(this, i);
lastRet = cursor = i;
return previous;
} catch(IndexOutOfBoundsException e) {
checkForComodification();
throw new NoSuchElementException();
}
}
public int nextIndex() {
return cursor;
}
public int previousIndex() {
return cursor-1;
}
public void set(Object o) {
if (lastRet == -1) {
throw new IllegalStateException();
}
checkForComodification();
try {
PersistentListImpl.this.set(lastRet, o);
expectedModCount = modCount;
} catch(IndexOutOfBoundsException e) {
throw new ConcurrentModificationException();
}
}
public void add(Object o) {
checkForComodification();
try {
PersistentListImpl.this.add(cursor++, o);
lastRet = -1;
page = null;
expectedModCount = modCount;
} catch(IndexOutOfBoundsException e) {
throw new ConcurrentModificationException();
}
}
}
public List subList(int fromIndex, int toIndex) {
return new SubList(this, fromIndex, toIndex);
}
protected void removeRange(int fromIndex, int toIndex) {
while (fromIndex < toIndex) {
remove(fromIndex);
toIndex -= 1;
}
}
public boolean containsAll(Collection c) {
Iterator i = c.iterator();
while (i.hasNext()) {
if (!contains(i.next()))
return false;
}
return true;
}
public boolean addAll(Collection c) {
boolean modified = false;
Iterator i = c.iterator();
while (i.hasNext()) {
modified |= add(i.next());
}
return modified;
}
public boolean retainAll(Collection c) {
ArrayList toBeRemoved = new ArrayList();
Iterator i = iterator();
while (i.hasNext()) {
Object o = i.next();
if (!c.contains(o)) {
toBeRemoved.add(o);
}
}
int n = toBeRemoved.size();
for (int j = 0; j < n; j++) {
remove(toBeRemoved.get(j));
}
return n != 0;
}
public boolean removeAll(Collection c) {
boolean modified = false;
Iterator i = c.iterator();
while (i.hasNext()) {
modified |= remove(i.next());
}
return modified;
}
public boolean remove(Object o) {
Iterator e = iterator();
if (o==null) {
while (e.hasNext()) {
if (e.next()==null) {
e.remove();
return true;
}
}
} else {
while (e.hasNext()) {
if (o.equals(e.next())) {
e.remove();
return true;
}
}
}
return false;
}
public Iterator select(Class cls, String predicate) {
Query query = new QueryImpl(getStorage());
return query.select(cls, iterator(), predicate);
}
PersistentListImpl() {}
PersistentListImpl(Storage storage) {
super(storage);
root = new ListPage(storage);
}
}
class SubList extends AbstractList {
private PersistentListImpl l;
private int offset;
private int size;
private int expectedModCount;
SubList(PersistentListImpl list, int fromIndex, int toIndex) {
if (fromIndex < 0) {
throw new IndexOutOfBoundsException("fromIndex = " + fromIndex);
}
if (toIndex > list.size()) {
throw new IndexOutOfBoundsException("toIndex = " + toIndex);
}
if (fromIndex > toIndex) {
throw new IllegalArgumentException("fromIndex(" + fromIndex +
") > toIndex(" + toIndex + ")");
}
l = list;
offset = fromIndex;
size = toIndex - fromIndex;
expectedModCount = l.modCount;
}
public Object set(int index, Object element) {
rangeCheck(index);
checkForComodification();
return l.set(index+offset, element);
}
public Object get(int index) {
rangeCheck(index);
checkForComodification();
return l.get(index+offset);
}
public int size() {
checkForComodification();
return size;
}
public void add(int index, Object element) {
if (index<0 || index>size) {
throw new IndexOutOfBoundsException();
}
checkForComodification();
l.add(index+offset, element);
expectedModCount = l.modCount;
size++;
modCount++;
}
public Object remove(int index) {
rangeCheck(index);
checkForComodification();
Object result = l.remove(index+offset);
expectedModCount = l.modCount;
size--;
modCount++;
return result;
}
protected void removeRange(int fromIndex, int toIndex) {
checkForComodification();
l.removeRange(fromIndex+offset, toIndex+offset);
expectedModCount = l.modCount;
size -= (toIndex-fromIndex);
modCount++;
}
public boolean addAll(Collection c) {
return addAll(size, c);
}
public boolean addAll(int index, Collection c) {
if (index<0 || index>size) {
throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size);
}
int cSize = c.size();
if (cSize==0) {
return false;
}
checkForComodification();
l.addAll(offset+index, c);
expectedModCount = l.modCount;
size += cSize;
modCount++;
return true;
}
public Iterator iterator() {
return listIterator();
}
public ListIterator listIterator(final int index) {
checkForComodification();
if (index<0 || index>size) {
throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size);
}
return new ListIterator() {
private ListIterator i = l.listIterator(index+offset);
public boolean hasNext() {
return nextIndex() < size;
}
public Object next() {
if (hasNext()) {
return i.next();
} else {
throw new NoSuchElementException();
}
}
public boolean hasPrevious() {
return previousIndex() >= 0;
}
public Object previous() {
if (hasPrevious()) {
return i.previous();
} else {
throw new NoSuchElementException();
}
}
public int nextIndex() {
return i.nextIndex() - offset;
}
public int previousIndex() {
return i.previousIndex() - offset;
}
public void remove() {
i.remove();
expectedModCount = l.modCount;
size--;
modCount++;
}
public void set(Object o) {
i.set(o);
}
public void add(Object o) {
i.add(o);
expectedModCount = l.modCount;
size++;
modCount++;
}
};
}
public List subList(int fromIndex, int toIndex) {
return new SubList(l, offset+fromIndex, offset+toIndex);
}
private void rangeCheck(int index) {
if (index<0 || index>=size) {
throw new IndexOutOfBoundsException("Index: "+index+
",Size: "+size);
}
}
private void checkForComodification() {
if (l.modCount != expectedModCount) {
throw new ConcurrentModificationException();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -