Skip to content

Commit

Permalink
Add support for methods added to interface List in JDK 21
Browse files Browse the repository at this point in the history
  • Loading branch information
centic9 committed Apr 22, 2024
1 parent f2a6a15 commit a454a0f
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.function.Consumer;
import java.util.function.Function;

Expand Down Expand Up @@ -75,4 +76,22 @@ public R next() {
public void forEach(Consumer<? super R> action) {
original.forEach(e -> action.accept(accessor.apply(e)));
}

@Override
public R getFirst() {
if (original.isEmpty()) {
throw new NoSuchElementException();
} else {
return accessor.apply(original.get(0));
}
}

@Override
public R getLast() {
if (original.isEmpty()) {
throw new NoSuchElementException();
} else {
return accessor.apply(original.get(original.size() - 1));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,20 @@ public ListIterator<E> listIterator(int index) {
public List<E> subList(int fromIndex, int toIndex) {
throw new UnsupportedOperationException("This operation is not supported");
}

// this is added in JDK 21, so not @Override here
@SuppressWarnings("UnusedReturnValue")
public List<E> reversed() {
throw new UnsupportedOperationException("This operation is not supported");
}

// this is added in JDK 21, so not @Override here
public E getFirst() {
throw new UnsupportedOperationException("This operation is not supported");
}

// this is added in JDK 21, so not @Override here
public E getLast() {
throw new UnsupportedOperationException("This operation is not supported");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.concurrent.atomic.AtomicLong;

public class ObjectAccessorListTest extends AbstractUnsupportedCollectionTest<ObjectAccessorList<Integer, Object>> {
Expand All @@ -22,6 +24,8 @@ public static void setUpClass() {
IGNORED_METHODS.add("isEmpty");
IGNORED_METHODS.add("iterator");
IGNORED_METHODS.add("forEach");
IGNORED_METHODS.add("getFirst");
IGNORED_METHODS.add("getLast");
}

@Override
Expand All @@ -48,6 +52,14 @@ public void testObjectAccess() {
assertEquals(3, list.size());
assertFalse(list.isEmpty());

assertThrows(IndexOutOfBoundsException.class,
() -> list.get(2363));

//noinspection CastCanBeRemovedNarrowingVariableType
assertEquals((Integer)obj1.hashCode(), ((UnsupportedList<Integer>)list).getFirst());
//noinspection CastCanBeRemovedNarrowingVariableType
assertEquals((Integer)obj3.hashCode(), ((UnsupportedList<Integer>)list).getLast());

Iterator<Integer> it = list.iterator();
assertTrue(it.hasNext());
assertEquals((Integer)obj1.hashCode(), it.next());
Expand Down Expand Up @@ -85,7 +97,15 @@ public void testObjectAccessWithChanges() {
assertEquals(2, list.size());
assertFalse(list.isEmpty());

Iterator<Integer> it = list.iterator();
assertThrows(IndexOutOfBoundsException.class,
() -> list.get(2363));

//noinspection CastCanBeRemovedNarrowingVariableType
assertEquals((Integer)obj1.hashCode(), ((UnsupportedList<Integer>)list).getFirst());
//noinspection CastCanBeRemovedNarrowingVariableType
assertEquals((Integer)obj3.hashCode(), ((UnsupportedList<Integer>)list).getLast());

Iterator<Integer> it = list.iterator();
assertTrue(it.hasNext());
assertEquals((Integer)obj1.hashCode(), it.next());
assertTrue(it.hasNext());
Expand Down Expand Up @@ -113,4 +133,22 @@ public void testObjectAccessWithChanges() {

assertTrue(list.isEmpty());
}

@Test
public void testEmpty() {
List<Integer> list = new ObjectAccessorList<>(Collections.emptyList(), Object::hashCode) {};
assertTrue(list.isEmpty());
//noinspection ConstantValue
assertEquals(0, list.size());
assertThrows(IndexOutOfBoundsException.class,
() -> list.get(0));
assertThrows(IndexOutOfBoundsException.class,
() -> list.get(2363));
//noinspection CastCanBeRemovedNarrowingVariableType
assertThrows(NoSuchElementException.class,
() -> ((UnsupportedList<Integer>)list).getFirst());
//noinspection CastCanBeRemovedNarrowingVariableType
assertThrows(NoSuchElementException.class,
() -> ((UnsupportedList<Integer>)list).getLast());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,11 @@ public void test() {
() -> coll.add(null));
assertThrows(UnsupportedOperationException.class,
() -> coll.get(0));
assertThrows(UnsupportedOperationException.class,
coll::reversed);
assertThrows(UnsupportedOperationException.class,
coll::getFirst);
assertThrows(UnsupportedOperationException.class,
coll::getLast);
}
}

0 comments on commit a454a0f

Please sign in to comment.