abstractlist.java
来自「纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统」· Java 代码 · 共 935 行 · 第 1/3 页
JAVA
935 行
/** The original list. */
final AbstractList backingList;
/** The index of the first element of the sublist. */
final int offset;
/** The size of the sublist. */
int size;
/**
* Construct the sublist.
*
* @param backing the list this comes from
* @param fromIndex the lower bound, inclusive
* @param toIndex the upper bound, exclusive
*/
SubList(AbstractList backing, int fromIndex, int toIndex)
{
backingList = backing;
modCount = backing.modCount;
offset = fromIndex;
size = toIndex - fromIndex;
}
/**
* This method checks the two modCount fields to ensure that there has
* not been a concurrent modification, returning if all is okay.
*
* @throws ConcurrentModificationException if the backing list has been
* modified externally to this sublist
*/
// This can be inlined. Package visible, for use by iterator.
void checkMod()
{
if (modCount != backingList.modCount)
throw new ConcurrentModificationException();
}
/**
* This method checks that a value is between 0 and size (inclusive). If
* it is not, an exception is thrown.
*
* @param index the value to check
* @throws IndexOutOfBoundsException if the value is out of range
*/
// This will get inlined, since it is private.
private void checkBoundsInclusive(int index)
{
if (index < 0 || index > size)
throw new IndexOutOfBoundsException("Index: " + index + ", Size:"
+ size);
}
/**
* This method checks that a value is between 0 (inclusive) and size
* (exclusive). If it is not, an exception is thrown.
*
* @param index the value to check
* @throws IndexOutOfBoundsException if the value is out of range
*/
// This will get inlined, since it is private.
private void checkBoundsExclusive(int index)
{
if (index < 0 || index >= size)
throw new IndexOutOfBoundsException("Index: " + index + ", Size:"
+ size);
}
/**
* Specified by AbstractList.subList to return the private field size.
*
* @return the sublist size
*/
public int size()
{
checkMod();
return size;
}
/**
* Specified by AbstractList.subList to delegate to the backing list.
*
* @param index the location to modify
* @param o the new value
* @return the old value
*/
public Object set(int index, Object o)
{
checkMod();
checkBoundsExclusive(index);
return backingList.set(index + offset, o);
}
/**
* Specified by AbstractList.subList to delegate to the backing list.
*
* @param index the location to get from
* @return the object at that location
*/
public Object get(int index)
{
checkMod();
checkBoundsExclusive(index);
return backingList.get(index + offset);
}
/**
* Specified by AbstractList.subList to delegate to the backing list.
*
* @param index the index to insert at
* @param o the object to add
*/
public void add(int index, Object o)
{
checkMod();
checkBoundsInclusive(index);
backingList.add(index + offset, o);
size++;
modCount = backingList.modCount;
}
/**
* Specified by AbstractList.subList to delegate to the backing list.
*
* @param index the index to remove
* @return the removed object
*/
public Object remove(int index)
{
checkMod();
checkBoundsExclusive(index);
Object o = backingList.remove(index + offset);
size--;
modCount = backingList.modCount;
return o;
}
/**
* Specified by AbstractList.subList to delegate to the backing list.
* This does no bounds checking, as it assumes it will only be called
* by trusted code like clear() which has already checked the bounds.
*
* @param fromIndex the lower bound, inclusive
* @param toIndex the upper bound, exclusive
*/
protected void removeRange(int fromIndex, int toIndex)
{
checkMod();
backingList.removeRange(offset + fromIndex, offset + toIndex);
size -= toIndex - fromIndex;
modCount = backingList.modCount;
}
/**
* Specified by AbstractList.subList to delegate to the backing list.
*
* @param index the location to insert at
* @param c the collection to insert
* @return true if this list was modified, in other words, c is non-empty
*/
public boolean addAll(int index, Collection c)
{
checkMod();
checkBoundsInclusive(index);
int csize = c.size();
boolean result = backingList.addAll(offset + index, c);
size += csize;
modCount = backingList.modCount;
return result;
}
/**
* Specified by AbstractList.subList to return addAll(size, c).
*
* @param c the collection to insert
* @return true if this list was modified, in other words, c is non-empty
*/
public boolean addAll(Collection c)
{
return addAll(size, c);
}
/**
* Specified by AbstractList.subList to return listIterator().
*
* @return an iterator over the sublist
*/
public Iterator iterator()
{
return listIterator();
}
/**
* Specified by AbstractList.subList to return a wrapper around the
* backing list's iterator.
*
* @param index the start location of the iterator
* @return a list iterator over the sublist
*/
public ListIterator listIterator(final int index)
{
checkMod();
checkBoundsInclusive(index);
return new ListIterator()
{
private final ListIterator i = backingList.listIterator(index + offset);
private int position = index;
public boolean hasNext()
{
checkMod();
return position < size;
}
public boolean hasPrevious()
{
checkMod();
return position > 0;
}
public Object next()
{
if (position == size)
throw new NoSuchElementException();
position++;
return i.next();
}
public Object previous()
{
if (position == 0)
throw new NoSuchElementException();
position--;
return i.previous();
}
public int nextIndex()
{
return i.nextIndex() - offset;
}
public int previousIndex()
{
return i.previousIndex() - offset;
}
public void remove()
{
i.remove();
size--;
position = nextIndex();
modCount = backingList.modCount;
}
public void set(Object o)
{
i.set(o);
}
public void add(Object o)
{
i.add(o);
size++;
position++;
modCount = backingList.modCount;
}
// Here is the reason why the various modCount fields are mostly
// ignored in this wrapper listIterator.
// If the backing listIterator is failfast, then the following holds:
// Using any other method on this list will call a corresponding
// method on the backing list *after* the backing listIterator
// is created, which will in turn cause a ConcurrentModException
// when this listIterator comes to use the backing one. So it is
// implicitly failfast.
// If the backing listIterator is NOT failfast, then the whole of
// this list isn't failfast, because the modCount field of the
// backing list is not valid. It would still be *possible* to
// make the iterator failfast wrt modifications of the sublist
// only, but somewhat pointless when the list can be changed under
// us.
// Either way, no explicit handling of modCount is needed.
// However modCount = backingList.modCount must be executed in add
// and remove, and size must also be updated in these two methods,
// since they do not go through the corresponding methods of the subList.
};
}
} // class SubList
/**
* This class is a RandomAccess version of SubList, as required by
* {@link AbstractList#subList(int, int)}.
*
* @author Eric Blake <ebb9@email.byu.edu>
*/
final class RandomAccessSubList extends SubList
implements RandomAccess
{
/**
* Construct the sublist.
*
* @param backing the list this comes from
* @param fromIndex the lower bound, inclusive
* @param toIndex the upper bound, exclusive
*/
RandomAccessSubList(AbstractList backing, int fromIndex, int toIndex)
{
super(backing, fromIndex, toIndex);
}
} // class RandomAccessSubList
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?