java.util.List<E>インタフェースを実装したクラスは,Javaアプリケーション開発でもしばしば使われるクラスだろう。それらの共通処理を実装したクラスがAbstractListクラスである。このクラスの中には,更にIteratorなどprivateな実装クラスが定義されているが,これらのソースコードは省略した。
コンストラクタはprotectedとなっていて,サブクラスからしか呼び出すことができない。
add(E e)メソッドはリストに要素を追加するものである。add(size(), e)という実装となっており,現在の要素数をsize()メソッドで取得して,そのインデックス位置に引数で与えた要素eを追加する。しかし,add(int index, E element)メソッドは,このクラスでは,UnsupportedOperationExceptionをスローする実装となっており,サブクラスでこのメソッドを実装する必要がある。また,get(int)メソッドは抽象メソッドとなっており,サブクラスでオーバーライドする必要がある。要素を削除するremove(int index)メソッドも UnsupportedOperationExceptionをスローする実装である。
indexOf(Object o)メソッドは引数で与えたオブジェクトのインデックスを返すものである。oがnullか否かにより処理を分岐しているが,どちらもiteratorを取得して,ループさせながら値が同じものを検索する。見つからなかった場合は,メソッドの最後の文で-1が返される。lastIndexOf(Object o)メソッドは,リストの末尾から検索する以外は処理内容はindexOf(Object o)と同じである。
clear()メソッドは,リストの要素をすべて削除するものである。removeRange(0, size())を呼び出して,インデックスが0からsize()の戻り値までの要素を削除する。
addAll(int index, Collection c)メソッドでは,引数で与えられたコレクションに含まれるすべての要素を,第1引数で与えられたインデックスから追加する。第2引数のコレクションを拡張for文でループさせて,インデックスをインクリメントしながら,その位置に要素を追加していく。
iterator()メソッドは,このクラスでインナークラスとして定義されているItrクラスのインスタンスを作成して返している。
listIterator()はリストの先頭からのListIteratorlistIterator()を返す。listIterator(0)を実行している。ここで呼び出されているlistIterator(final int index)メソッドでは,このクラスでインナークラスとして定義されているListItrクラスのインスタンスを作成している。
equals(Object o)メソッドは==演算子がtrueとなる同一オブジェクトの場合はすぐに判定できる。しかし,そうでない場合は二重ループで要素を一つずつチェックしてすべてが同じことを確認しているので,かなり重い処理になりそうだ。
removeRange(int fromIndex, int toIndex) メソッドではIteratorを用いて,第1引数のインデックスから第2引数のインデックスまで,繰り返し要素を取り出してremoveメソッドを呼び出す処理を繰り返している。
* Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package java.util;
import java.util.function.Consumer;
public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E> {
protected AbstractList() {
}
public boolean add(E e) {
add(size(), e);
return true;
}
public abstract E get(int index);
public E set(int index, E element) {
throw new UnsupportedOperationException();
}
public void add(int index, E element) {
throw new UnsupportedOperationException();
}
public E remove(int index) {
throw new UnsupportedOperationException();
}
// Search Operations
public int indexOf(Object o) {
ListIterator<E> it = listIterator();
if (o==null) {
while (it.hasNext())
if (it.next()==null)
return it.previousIndex();
} else {
while (it.hasNext())
if (o.equals(it.next()))
return it.previousIndex();
}
return -1;
}
public int lastIndexOf(Object o) {
ListIterator<E> it = listIterator(size());
if (o==null) {
while (it.hasPrevious())
if (it.previous()==null)
return it.nextIndex();
} else {
while (it.hasPrevious())
if (o.equals(it.previous()))
return it.nextIndex();
}
return -1;
}
// Bulk Operations
public void clear() {
removeRange(0, size());
}
public boolean addAll(int index, Collection<? extends E> c) {
rangeCheckForAdd(index);
boolean modified = false;
for (E e : c) {
add(index++, e);
modified = true;
}
return modified;
}
// Iterators
public Iterator<E> iterator() {
return new Itr();
}
public ListIterator<E> listIterator() {
return listIterator(0);
}
public ListIterator<E> listIterator(final int index) {
rangeCheckForAdd(index);
return new ListItr(index);
}
private class Itr implements Iterator<E> {
省略
}
private class ListItr extends Itr implements ListIterator<E> {
省略
}
public List<E> subList(int fromIndex, int toIndex) {
subListRangeCheck(fromIndex, toIndex, size());
return (this instanceof RandomAccess ?
new RandomAccessSubList<>(this, fromIndex, toIndex) :
new SubList<>(this, fromIndex, toIndex));
}
static void subListRangeCheck(int fromIndex, int toIndex, int size) {
if (fromIndex < 0)
throw new IndexOutOfBoundsException("fromIndex = " + fromIndex);
if (toIndex > size)
throw new IndexOutOfBoundsException("toIndex = " + toIndex);
if (fromIndex > toIndex)
throw new IllegalArgumentException("fromIndex(" + fromIndex +
") > toIndex(" + toIndex + ")");
}
// Comparison and hashing
public boolean equals(Object o) {
if (o == this)
return true;
if (!(o instanceof List))
return false;
ListIterator<E> e1 = listIterator();
ListIterator<?> e2 = ((List<?>) o).listIterator();
while (e1.hasNext() && e2.hasNext()) {
E o1 = e1.next();
Object o2 = e2.next();
if (!(o1==null ? o2==null : o1.equals(o2)))
return false;
}
return !(e1.hasNext() || e2.hasNext());
}
public int hashCode() {
int hashCode = 1;
for (E e : this)
hashCode = 31*hashCode + (e==null ? 0 : e.hashCode());
return hashCode;
}
protected void removeRange(int fromIndex, int toIndex) {
ListIterator<E> it = listIterator(fromIndex);
for (int i=0, n=toIndex-fromIndex; i<n; i++) {
it.next();
it.remove();
}
}
protected transient int modCount = 0;
private void rangeCheckForAdd(int index) {
if (index < 0 || index > size())
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
private String outOfBoundsMsg(int index) {
return "Index: "+index+", Size: "+size();
}
static final class RandomAccessSpliterator<E> implements Spliterator<E> {
省略
}
private static class SubList<E> extends AbstractList<E> {
省略
}
private static class RandomAccessSubList<E>
extends SubList<E> implements RandomAccess {
省略
}
}