8266571: Sequenced Collections

Reviewed-by: alanb
This commit is contained in:
Stuart Marks 2023-04-25 15:19:08 +00:00
parent bad6aa68e4
commit 17ce0976e4
42 changed files with 7060 additions and 150 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2023, 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
@ -28,10 +28,9 @@ package java.util;
import java.util.function.UnaryOperator;
/**
* An ordered collection (also known as a <i>sequence</i>). The user of this
* interface has precise control over where in the list each element is
* inserted. The user can access elements by their integer index (position in
* the list), and search for elements in the list.<p>
* An ordered collection, where the user has precise control over where in the
* list each element is inserted. The user can access elements by their integer
* index (position in the list), and search for elements in the list.<p>
*
* Unlike sets, lists typically allow duplicate elements. More formally,
* lists typically allow pairs of elements {@code e1} and {@code e2}
@ -139,7 +138,7 @@ import java.util.function.UnaryOperator;
* @since 1.2
*/
public interface List<E> extends Collection<E> {
public interface List<E> extends SequencedCollection<E> {
// Query Operations
/**
@ -781,6 +780,126 @@ public interface List<E> extends Collection<E> {
}
}
// ========== SequencedCollection ==========
/**
* {@inheritDoc}
*
* @implSpec
* The implementation in this interface calls {@code add(0, e)}.
*
* @throws NullPointerException {@inheritDoc}
* @throws UnsupportedOperationException {@inheritDoc}
* @since 21
*/
default void addFirst(E e) {
this.add(0, e);
}
/**
* {@inheritDoc}
*
* @implSpec
* The implementation in this interface calls {@code add(e)}.
*
* @throws NullPointerException {@inheritDoc}
* @throws UnsupportedOperationException {@inheritDoc}
* @since 21
*/
default void addLast(E e) {
this.add(e);
}
/**
* {@inheritDoc}
*
* @implSpec
* If this List is not empty, the implementation in this interface returns the result
* of calling {@code get(0)}. Otherwise, it throws {@code NoSuchElementException}.
*
* @throws NoSuchElementException {@inheritDoc}
* @since 21
*/
default E getFirst() {
if (this.isEmpty()) {
throw new NoSuchElementException();
} else {
return this.get(0);
}
}
/**
* {@inheritDoc}
*
* @implSpec
* If this List is not empty, the implementation in this interface returns the result
* of calling {@code get(size() - 1)}. Otherwise, it throws {@code NoSuchElementException}.
*
* @throws NoSuchElementException {@inheritDoc}
* @since 21
*/
default E getLast() {
if (this.isEmpty()) {
throw new NoSuchElementException();
} else {
return this.get(this.size() - 1);
}
}
/**
* {@inheritDoc}
*
* @implSpec
* If this List is not empty, the implementation in this interface returns the result
* of calling {@code remove(0)}. Otherwise, it throws {@code NoSuchElementException}.
*
* @throws NoSuchElementException {@inheritDoc}
* @throws UnsupportedOperationException {@inheritDoc}
* @since 21
*/
default E removeFirst() {
if (this.isEmpty()) {
throw new NoSuchElementException();
} else {
return this.remove(0);
}
}
/**
* {@inheritDoc}
*
* @implSpec
* If this List is not empty, the implementation in this interface returns the result
* of calling {@code remove(size() - 1)}. Otherwise, it throws {@code NoSuchElementException}.
*
* @throws NoSuchElementException {@inheritDoc}
* @throws UnsupportedOperationException {@inheritDoc}
* @since 21
*/
default E removeLast() {
if (this.isEmpty()) {
throw new NoSuchElementException();
} else {
return this.remove(this.size() - 1);
}
}
/**
* {@inheritDoc}
*
* @implSpec
* The implementation in this interface returns an instance of a reverse-ordered
* List that delegates its operations to this List.
*
* @return a reverse-ordered view of this collection, as a {@code List}
* @since 21
*/
default List<E> reversed() {
return ReverseOrderListView.of(this, true); // we must assume it's modifiable
}
// ========== static methods ==========
/**
* Returns an unmodifiable list containing zero elements.
*