package li.earth.urchin.twic.collections; import java.util.Collection; import java.util.List; import java.util.ListIterator; import java.util.AbstractList; public abstract class AbstractBigList extends AbstractList implements BigList { public abstract long bigSize(); public int size() { return saturatingNarrow(bigSize()); // javadoc says this is right } public abstract E get(long index); public E get(int index) { return get((long)index); } public E set(long index, E obj) { throw new UnsupportedOperationException(); } public E set(int index, E obj) { return set((long)index, obj); } public void add(long index, E obj) { throw new UnsupportedOperationException(); } public void add(int index, E obj) { add((long)index, obj); } public E remove(long index) { throw new UnsupportedOperationException(); } public E remove(int index) { return remove((long)index); } public boolean addAll(long index, Collection c) { for (E obj: c) { add(index, obj); ++index; } return true; } public boolean addAll(int index, Collection c) { return addAll((long)index, c); } public long bigIndexOf(Object obj) { long index = 0L; for (E elem: this) { if ((obj == null) ? (elem == null) : obj.equals(elem)) return index; ++index; } return -1L; } public int indexOf(Object obj) { return narrow(bigIndexOf(obj)); } public long lastBigIndexOf(Object obj) { BigListIterator it = listIterator(bigSize()); while (it.hasPrevious()) { E elem = it.previous(); if ((obj == null) ? (elem == null) : obj.equals(elem)) return it.nextBigIndex(); } return -1L; } public int lastIndexOf(Object obj) { return narrow(lastBigIndexOf(obj)); } public BigListIterator listIterator() { return listIterator(0L); } public BigListIterator listIterator(int index) { return listIterator((long)index); } public BigListIterator listIterator(long index) { return new GeneralBigListIterator(this, index); } public BigList subList(long fromIndex, long toIndex) { return new GeneralBigSubList(this, fromIndex, toIndex); } public static final int narrow(long l) { if ((l > Integer.MAX_VALUE) || (l < Integer.MIN_VALUE)) throw new IndexOutOfBoundsException("long index too large to narrow to int: " + l); else return (int)l; } public static final int saturatingNarrow(long l) { if (l > Integer.MAX_VALUE) return Integer.MAX_VALUE; else return (int)l; } }