== Why is this an issue?

Java 21 introduces the new Sequenced Collections API, which is applicable to all collections with a defined sequence on their elements, such as `LinkedList`, `TreeSet`, and others (see https://openjdk.org/jeps/431[JEP 431]).
For projects using Java 21 and onwards, this API should be used instead of workaround implementations that were necessary prior to Java 21.

This rule identifies instances where a workaround is used to add or remove the first or last element of a collection where the `addFirst`, `addLast`, `removeFirst` or `removeLast` method in the `java.util.SequencedCollection` class should have been used instead.

== How to fix it

Replace the reported expression with its functional equivalent call to the `addFirst`, `addLast`, `removeFirst` or `removeLast` method in the `java.util.SequencedCollection`
class. Please note that the examples provided do not exhaustively cover all possible workarounds.

=== Code examples

==== Noncompliant code example

[source,java,diff-id=1,diff-type=noncompliant]
----
void push(List<String> list, String element) {
  list.add(list.size()-1, element); // Noncompliant
}
----

==== Compliant solution

[source,java,diff-id=1,diff-type=compliant]
----
void push(List<String> list, String element) {
  list.addLast(element); // Compliant
}
----

==== Noncompliant code example

[source,java,diff-id=2,diff-type=noncompliant]
----
void pop(List<String> list) {
  list.remove(list.size()-1); // Noncompliant
}
----

[source,java,diff-id=5,diff-type=noncompliant]
----
void pop(SortedSet<String> set) {
  set.remove(set.last()); // Noncompliant
}
----

==== Compliant solution

[source,java,diff-id=2,diff-type=compliant]
----
void pop(List<String> list) {
  list.removeLast(); // Compliant
}
----

[source,java,diff-id=5,diff-type=compliant]
----
void pop(SortedSet<String> set) {
  set.removeLast(); // Compliant
}
----

==== Noncompliant code example

[source,java,diff-id=3,diff-type=noncompliant]
----
void pushHead(List<String> list, String element) {
  list.add(0, element); // Noncompliant
}
----

==== Compliant solution

[source,java,diff-id=3,diff-type=compliant]
----
void pushHead(List<String> list, String element) {
  list.addFirst(element); // Compliant
}
----

==== Noncompliant code example

[source,java,diff-id=4,diff-type=noncompliant]
----
void popHead(List<String> list) {
  list.remove(0); // Noncompliant
}
----

[source,java,diff-id=6,diff-type=noncompliant]
----
void popHead(SortedSet<String> set) {
  set.remove(set.first()); // Noncompliant
}
----

==== Compliant solution

[source,java,diff-id=4,diff-type=compliant]
----
void popHead(List<String> list) {
  list.removeFirst(); // Compliant
}
----

[source,java,diff-id=6,diff-type=compliant]
----
void popHead(SortedSet<String> set) {
  set.removeFirst(); // Compliant
}
----

== Resources

* https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/SequencedCollection.html[Interface SequencedCollection - Java SE 21 API Documentation]
* https://openjdk.org/jeps/431[JEP 431: Sequenced Collections]
* https://docs.oracle.com/en/java/javase/21/core/creating-sequenced-collections-sets-and-maps.html#GUID-DCFE1D88-A0F5-47DE-A816-AEDA50B97523[Creating Sequenced Collections, Sets, and Maps]
