< prev index next >
src/java.base/share/classes/java/util/AbstractList.java
Print this page
@@ -21,11 +21,17 @@
* 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;
+package javany.util;
+
+import java.util.ConcurrentModificationException;
+import java.util.NoSuchElementException;
+import java.util.RandomAccess;
+
+import javany.util.function.*;
/**
* This class provides a skeletal implementation of the {@link List}
* interface to minimize the effort required to implement this interface
* backed by a "random access" data store (such as an array). For sequential
@@ -66,11 +72,11 @@
* @author Josh Bloch
* @author Neal Gafter
* @since 1.2
*/
-public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E> {
+public abstract class AbstractList<any E> extends AbstractCollection<E> implements List<E> {
/**
* Sole constructor. (For invocation by subclass constructors, typically
* implicit.)
*/
protected AbstractList() {
@@ -178,22 +184,35 @@
*
* @throws ClassCastException {@inheritDoc}
* @throws NullPointerException {@inheritDoc}
*/
public int indexOf(Object o) {
+ __WhereVal(E) {
+ if (o == null) {
+ return -1;
+ }
+ ListIterator<E> it = listIterator();
+ Function<E, Object> box = Any.converter();
+ while (it.hasNext())
+ if (o.equals(box.apply(it.next()))) // boxing
+ return it.previousIndex();
+ return -1;
+ }
+ __WhereRef(E) {
ListIterator<E> it = listIterator();
- if (o==null) {
+ if (o == null) {
while (it.hasNext())
- if (it.next()==null)
+ if (it.next() == null)
return it.previousIndex();
} else {
while (it.hasNext())
if (o.equals(it.next()))
return it.previousIndex();
}
return -1;
}
+ }
/**
* {@inheritDoc}
*
* @implSpec
@@ -204,22 +223,35 @@
*
* @throws ClassCastException {@inheritDoc}
* @throws NullPointerException {@inheritDoc}
*/
public int lastIndexOf(Object o) {
+ __WhereVal(E) {
+ if (o == null) {
+ return -1;
+ }
ListIterator<E> it = listIterator(size());
- if (o==null) {
+ Function<E, Object> box = Any.converter();
while (it.hasPrevious())
- if (it.previous()==null)
+ if (o.equals(box.apply(it.previous()))) // boxing
+ return it.nextIndex();
+ return -1;
+ }
+ __WhereRef(E) {
+ 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
/**
@@ -262,11 +294,13 @@
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
public boolean addAll(int index, Collection<? extends E> c) {
rangeCheckForAdd(index);
boolean modified = false;
- for (E e : c) {
+ Iterator<? extends E> it = c.iterator();
+ while (it.hasNext()) {
+ E e = it.next();
add(index++, e);
modified = true;
}
return modified;
}
@@ -520,22 +554,21 @@
* otherwise it returns {@code true} when the iterations complete.
*
* @param o the object to be compared for equality with this list
* @return {@code true} if the specified object is equal to this list
*/
+ @SuppressWarnings("unchecked")
public boolean equals(Object o) {
if (o == this)
return true;
- if (!(o instanceof List))
+ if (!(o instanceof List<E>))
return false;
ListIterator<E> e1 = listIterator();
- ListIterator<?> e2 = ((List<?>) o).listIterator();
+ ListIterator<? extends E> e2 = ((List<? extends E>) o).listIterator();
while (e1.hasNext() && e2.hasNext()) {
- E o1 = e1.next();
- Object o2 = e2.next();
- if (!(o1==null ? o2==null : o1.equals(o2)))
+ if (!Any.equals(e1.next(), e2.next()))
return false;
}
return !(e1.hasNext() || e2.hasNext());
}
@@ -549,12 +582,15 @@
*
* @return the hash code value for this list
*/
public int hashCode() {
int hashCode = 1;
- for (E e : this)
- hashCode = 31*hashCode + (e==null ? 0 : e.hashCode());
+ Iterator<E> it = iterator();
+ while (it.hasNext()) {
+ E e = it.next();
+ hashCode = 31 * hashCode + Any.hashCode(e);
+ }
return hashCode;
}
/**
* Removes from this list all of the elements whose index is between
@@ -623,11 +659,11 @@
private String outOfBoundsMsg(int index) {
return "Index: "+index+", Size: "+size();
}
}
-class SubList<E> extends AbstractList<E> {
+class SubList<any E> extends AbstractList<E> {
private final AbstractList<E> l;
private final int offset;
private int size;
SubList(AbstractList<E> list, int fromIndex, int toIndex) {
@@ -783,11 +819,11 @@
if (this.modCount != l.modCount)
throw new ConcurrentModificationException();
}
}
-class RandomAccessSubList<E> extends SubList<E> implements RandomAccess {
+class RandomAccessSubList<any E> extends SubList<E> implements RandomAccess {
RandomAccessSubList(AbstractList<E> list, int fromIndex, int toIndex) {
super(list, fromIndex, toIndex);
}
public List<E> subList(int fromIndex, int toIndex) {
< prev index next >